Related Articles

# Find permutation with maximum remainder Sum

• Last Updated : 03 May, 2021

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

## Java

 // Java implementation of the approachimport java.util.*;class GFG{ // Function to find the permutationstatic int[] Findpermutation(int n){    int [] a = new int[n + 1];     // Put n at the first index 1    a = n;     // Put all the numbers from    // 2 to n sequentially    for (int i = 2; i <= n; i++)        a[i] = i - 1;     return a;} // Driver codepublic 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

## Python3

 # Python3 implementation of the approach # Function to find the permutationdef Findpermutation(n) :     a =  * (n + 1);     # Put n at the first index 1    a = n;     # Put all the numbers from    # 2 to n sequentially    for i in range(2, n + 1) :        a[i] = i - 1;     return a; # Driver codeif __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

## C#

 // C# implementation of the approachusing System; class GFG{ // Function to find the permutationstatic int[] Findpermutation(int n){    int [] a = new int[n + 1];     // Put n at the first index 1    a = n;     // Put all the numbers from    // 2 to n sequentially    for (int i = 2; i <= n; i++)        a[i] = i - 1;     return a;} // Driver codepublic 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

## Javascript

 
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up