# Find the permutation of first N natural numbers such that sum of i % P_{i} 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 % P _{i}** 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

28

## Recommended Posts:

- Find the good permutation of first N natural numbers
- Find permutation of first N natural numbers that satisfies the given condition
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Increasing permutation of first N natural numbers
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Find permutation with maximum remainder Sum
- Find sum of N-th group of Natural Numbers
- Program to find sum of first n natural numbers
- Find if given number is sum of first n natural numbers
- Find m-th summation of first n natural numbers.
- Find the average of first N natural numbers
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find the count of natural Hexadecimal numbers of size N
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.