Next Permutation | Practice | GeeksforGeeks

Implement the next permutation, which rearranges the list of numbers into Lexicographically next greater permutation of list of numbers. If such arrangement is not possible, it must be rearranged to the lowest possible order i.e. sorted in an ascending order. You are given an list of numbers arr[ ] of size N.
Example 1:
Input: N = 6
arr = {1, 2, 3, 6, 5, 4}
Output: {1, 2, 4, 3, 5, 6}
Explaination: The next permutation of the 
given array is {1, 2, 4, 3, 5, 6}.
Example 2:
Input: N = 3
arr = {3, 2, 1}
Output: {1, 2, 3}
Explaination: As arr[] is the last 
permutation. So, the next permutation 
is the lowest one.
Your Task:
You do not need to read input or print anything. Your task is to complete the function nextPermutation() which takes N and arr[ ] as input parameters and returns a list of numbers containing the next permutation.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10000
0
skumar2009200111 hours ago
c++ sol in 0.01 sec
vector<int> nextPermutation(int N, vector<int> arr){       // code here       for(int i=N-1;i>0;i--){           if(arr[i]>arr[i-1]){               for(int j=N-1;j>=i;j--){                   if(arr[i-1]<arr[j]){                       swap(arr[i-1],arr[j]);                       break;                   }               }               sort(arr.begin()+i,arr.end());               return arr;               break;           }       }       sort(arr.begin(),arr.end());      return  arr;   }
0
aamitprasad6181 day ago
CPP
 
int ind1=-1;
        int ind2=-1;
        
        // int n=nums.size();
        
        for(int i=n-2;i>=0;i--){
            if(nums[i] < nums[i+1]){
                ind1=i;
                break;
            }
        }
        
        if(ind1<0){
            sort(nums.begin(),nums.end());
            return nums;
        }
        
        for(int i=n-1;i>=0;i--){
            if(nums[i]>nums[ind1]){
                ind2=i;
                break;
            }
        }
        
        swap(nums[ind1],nums[ind2]);
        
        reverse(nums.begin()+ind1+1,nums.end());
        
        return nums;
        
        
        
0
smondalqwerty1 day ago
vector<int> nextPermutation(int n, vector<int> arr)   {       int i;
       for (i = n - 1; i > 0; i--)       {           if (arr[i] > arr[i - 1])           {               break;           }       }       if (i!=0)       {       int min = arr[i], k=i;
       for (int j = i; j < n; j++)       {           if (arr[j] > arr[i - 1] && arr[j] < min)           {               k = j;               min = arr[j];           }       }       swap(arr[i-1],arr[k]);       }       sort(arr.begin()+i,arr.end());
       return arr;   }
0
debayanbiswas312 days ago
TC: O(N)
SC: O(1)
 
vector<int> nextPermutation(int n, vector<int> arr){
        // code here
        int index1=-1;
        int index2=-1;
        for(int i=n-1;i>=0;i--)
        {
            if(arr[i-1]<arr[i])
            {
                index1=i-1;
                break;
            }
        }
        if(index1==-1)
        {
            reverse(arr.begin(),arr.end());
            return arr;
        }
        int diff=INT_MAX;
        for(int i=index1+1;i<n;i++)
        {
            if(arr[i]<=arr[index1])
            {
                break;
            }
            if(arr[i]-arr[index1]<diff)
            {
                diff=arr[i]-arr[index1];
                index2=i;
            }
        }
        
        swap(arr[index1],arr[index2]);
        reverse(arr.begin()+index1+1,arr.end());
        return arr;
        
    
    }
0
ddttx776 days ago
	vector<int> rev(vector<int> v, int j)
    {
        vector<int> ans;
        for(int i=v.size()-1;i>=j;i--)
            ans.push_back(v[i]);
        return ans;
    }
    vector<int> nextPermutation(int N, vector<int> arr)
    {
        int i,j;
        for(i=N-2;i>=0;i--)
        {
            if(arr[i]<arr[i+1])
                break;
        }
        if(i<0)
            return rev(arr,0);
        else
        {
            for(j=N-1;j>i;j--)
            {
                if(arr[j]>arr[i])
                    break;
            }
            swap(arr[i],arr[j]);
            vector<int> temp=rev(arr,i+1);
            vector<int> ans;
            for(j=0;j<=i;j++)
                ans.push_back(arr[j]);
            for(j=0;j<temp.size();j++)
                ans.push_back(temp[j]);
            return ans;
        }
    }
0
jaydhurat6 days ago
class Solution{
   static List<Integer> nextPermutation(int n, int a[]){
       // code here
       List<Integer> list = new ArrayList<>();
       
       int idx1 = 0;
       int idx2 = 0;
       int j = 0;
       
       for(int i = n-1;i>=0;i--)
       {
           if(i+1<n && a[i]<a[i+1])
           {
               idx1 = i;
               break;
           }
          j=i;
       }
       if(j==0)
       {
           for(int i=n-1;i>=0;i--)
           
           {
               list.add(a[i]);
               
           }
           return list;
       }
       
       
       for(int i = n-1;i>=0;i--)
       {
           if(a[idx1]<a[i])
           {
               idx2 = i;
               break;
           }
       }
       int temp = a[idx1];
       a[idx1] = a[idx2];
       a[idx2] = temp;
       
       for(int i = 0;i<=idx1;i++)
       {
           list.add(a[i]);
       }
       for(int i=n-1;i>idx1;i--)
       {
           list.add(a[i]);
       }
       return list;
   }
}
0
tonroyjptt1 week ago
Java Solution
class Solution{   static void swap(int[] arr, int i, int j){       int temp = arr[i];       arr[i] = arr[j];       arr[j] = temp;   }      static void reverse(int[] arr, int i, int j){       while(i<j){           swap(arr, i++, j--);       }   }
   static List<Integer> nextPermutation(int N, int arr[]){       // code here       ArrayList<Integer> ans = new ArrayList<>();       int i = arr.length - 2;              while(i >= 0 && arr[i] >= arr[i+1]) i--;       if(i>=0){           int j = arr.length - 1;           while(arr[i] >= arr[j]) j--;           swap(arr, i, j);       }       reverse(arr, i+1, arr.length-1);              for(int k=0; k<N; k++){           ans.add(arr[k]);       }       return ans;   }}
0
mahesh_phutane1 week ago
Java Solution
class Solution{
    static void swap(int[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    static void reverse(int[] arr,int i,int j){
        while(i<j){
            swap(arr,i++,j--);
        }
    }
    static List<Integer> nextPermutation(int N, int arr[]){
        int idx = -1;
        List<Integer> ans = new ArrayList<>();
        for(int i = N-1;i>0;i--){
            if(arr[i]>arr[i-1]){
                idx = i;
                break;
            }
        }
        if(idx==-1){
            Arrays.sort(arr);
        }else{
            int idx2 = idx;
            for(int i = idx+1;i<N;i++){
                if(arr[i]>arr[idx-1]&&arr[i]<arr[idx]){
                    idx2 = i;
                }
            }
            swap(arr,idx-1,idx2);
            reverse(arr,idx,N-1);
        }
        for(int i = 0;i<N;i++){
            ans.add(arr[i]);
        }
        return ans;
    }
}
0
prachiacharya20011 week ago
 void swap(vector<int>&arr,int i,int j){       int temp=arr[i];       arr[i]=arr[j];       arr[j]=temp;   }   void reverse(int i,int j,vector<int>&arr){       while(i<j) swap(arr,i++,j--);   }   vector<int> nextPermutation(int N, vector<int> arr){       // code here       if(arr.size()==NULL||arr.size()<=1) return arr;       int i=arr.size()-2;       while(i>=0&& arr[i]>=arr[i+1]) i--;       if(i>=0){           int j=arr.size()-1;           while(arr[j]<=arr[i]) j--;           swap(arr,i,j);       }       reverse(i+1,arr.size()-1,arr);       return arr;
0
raushanrakesh0692 weeks ago
Java Script Solution
 
class Solution {   nextPermutation(arr,n){        const swap = (i, j) =>       [arr[i],arr[j]] = [arr[j],arr[i]]
   let len = n - 1, i   for (i = len - 1; arr[i] >= arr[i+1];) i--   let j = i + 1, k = len   while (j < k) swap(j++,k--)   if (i >= 0) {       for (j = i + 1; arr[i] >= arr[j];) j++       swap(i,j)   }      return arr;   }}  
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.