Given an integer **N**, the task is to find a permutation of the integers from **1** to **N** such that is maximum.

**Examples:**

Input:N = 3

Output:3 1 2

Sum of the remainder values is (0 + 1 + 2) = 3

which is the maximum possible.

Input:N = 5

Output:5 1 2 3 4

**Approach:** As it is known that the maximum value of a number **X** after doing the **mod** with **Y** is **Y-1**. The permutation that will yield the maximum sum of the mosulus values will be **{N, 1, 2, 3, …., N – 1}**.

After evaluating the expression on the above array the output array will be **{0, 1, 2, 3, …., N – 1}** and this is the maximum value that can be obtained.

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 ` `vector<` `int` `> Findpermutation(` `int` `n) ` `{ ` ` ` `vector<` `int` `> a(n + 1); ` ` ` ` ` `// Put n at the first index 1 ` ` ` `a[1] = n; ` ` ` ` ` `// Put all the numbers from ` ` ` `// 2 to n sequentially ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `a[i] = i - 1; ` ` ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 8; ` ` ` ` ` `vector<` `int` `> v = Findpermutation(n); ` ` ` ` ` `// Display the permutation ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `cout << v[i] << ` `' '` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` `class` `GFG ` `{ ` ` ` `// Function to find the permutation ` `static` `int` `[] Findpermutation(` `int` `n) ` `{ ` ` ` `int` `[] a = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Put n at the first index 1 ` ` ` `a[` `1` `] = n; ` ` ` ` ` `// Put all the numbers from ` ` ` `// 2 to n sequentially ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `a[i] = i - ` `1` `; ` ` ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `8` `; ` ` ` ` ` `int` `[]v = Findpermutation(n); ` ` ` ` ` `// Display the permutation ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `System.out.print(v[i] + ` `" "` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the permutation ` `def` `Findpermutation(n) : ` ` ` ` ` `a ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `); ` ` ` ` ` `# Put n at the first index 1 ` ` ` `a[` `1` `] ` `=` `n; ` ` ` ` ` `# Put all the numbers from ` ` ` `# 2 to n sequentially ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `) : ` ` ` `a[i] ` `=` `i ` `-` `1` `; ` ` ` ` ` `return` `a; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `8` `; ` ` ` ` ` `v ` `=` `Findpermutation(n); ` ` ` ` ` `# Display the permutation ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `) : ` ` ` `print` `(v[i], end ` `=` `' '` `); ` ` ` `# 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 ` `static` `int` `[] Findpermutation(` `int` `n) ` `{ ` ` ` `int` `[] a = ` `new` `int` `[n + 1]; ` ` ` ` ` `// Put n at the first index 1 ` ` ` `a[1] = n; ` ` ` ` ` `// Put all the numbers from ` ` ` `// 2 to n sequentially ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `a[i] = i - 1; ` ` ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 8; ` ` ` ` ` `int` `[]v = Findpermutation(n); ` ` ` ` ` `// Display the permutation ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `Console.Write(v[i] + ` `" "` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

8 1 2 3 4 5 6 7

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the permutation of first N natural numbers such that sum of i % P
_{i}is maximum possible - Maximum frequency of a remainder modulo 2
^{i} - Program to find remainder without using modulo or % operator
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Find numbers that divide X and Y to produce the same remainder
- Find Quotient and Remainder of two integer without using division operators
- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Maximum sum of absolute difference of any permutation
- Find the permutation p from the array q such that q[i] = p[i+1] - p[i]
- Find the possible permutation of the bits of N
- Find the minimum permutation of A greater than B
- How to find Lexicographically previous permutation?
- Find smallest permutation of given number
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of given operations required to convert a permutation into an identity permutation
- Find the K-th Permutation Sequence of first N natural numbers
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find the good permutation of first N 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.