Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFind the permutation of first N natural numbers such that sum of...

Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible

Given a number N. The task is to find the permutation P of first N natural numbers such that sum of i % Pi is maximum possible. The task is to find the maximum possible sum not it’s permutation.

Examples: 

Input: N = 5 
Output: 10 
Possible permutation is 2 3 4 5 1. 
Modulus values will be {1, 2, 3, 4, 0}. 
1 + 2 + 3 + 4 + 0 = 10

Input: N = 8 
Output: 28 
 

Approach: Maximum possible sum is (N * (N – 1)) / 2 and it is formed by the permutation 2, 3, 4, 5, ….. N, 1

Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the permutation of
// the first N natural numbers such that
// the sum of (i % Pi) is maximum possible
// and return the maximum sum
int Max_Sum(int n)
{
    return (n * (n - 1)) / 2;
}
 
// Driver code
int main()
{
    int n = 8;
 
    // Function call
    cout << Max_Sum(n);
 
    return 0;
}


Java




// Java implementation of the approach
import java.io.*;
public class GFG
{
 
// Function to find the permutation of
// the first N natural numbers such that
// the sum of (i % Pi) is maximum possible
// and return the maximum sum
static int Max_Sum(int n)
{
    return (n * (n - 1)) / 2;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 8;
 
    // Function call
    System.out.println(Max_Sum(n));
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 implementation of the approach
 
# Function to find the permutation of
# the first N natural numbers such that
# the sum of (i % Pi) is maximum possible
# and return the maximum sum
def Max_Sum(n) :
     
    return (n * (n - 1)) // 2;
 
# Driver code
if __name__ == "__main__" :
     
    n = 8;
 
    # Function call
    print(Max_Sum(n));
     
# This code is contributed by AnkitRai01


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to find the permutation of
// the first N natural numbers such that
// the sum of (i % Pi) is maximum possible
// and return the maximum sum
static int Max_Sum(int n)
{
    return (n * (n - 1)) / 2;
}
 
// Driver code
public static void Main (String[] args)
{
    int n = 8;
 
    // Function call
    Console.WriteLine(Max_Sum(n));
}
}
 
// This code is contributed by Princi Singh


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to find the permutation of
// the first N natural numbers such that
// the sum of (i % Pi) is maximum possible
// and return the maximum sum
function Max_Sum(n)
{
    return parseInt((n * (n - 1)) / 2);
}
 
// Driver code
let n = 8;
 
// Function call
document.write(Max_Sum(n));
 
// This code is contributed by rishavmahato348
 
</script>


Output: 

28

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments