Next Permutation

 

Given an array of integers arr[] representing a permutation, implement the next permutation that rearranges the numbers into the lexicographically next greater permutation. If no such permutation exists, rearrange the numbers into the lowest possible order (i.e., sorted in ascending order). 

Note:  A permutation of an array of integers refers to a specific arrangement of its elements in a sequence or linear order.

Examples:

Input: arr[] = [2, 4, 1, 7, 5, 0]
Output: [2, 4, 5, 0, 1, 7]
Explanation: The next permutation of the given array is [2, 4, 5, 0, 1, 7].
 
 class NextPermutation{
 static void nextPermutation(int[] arr){
 int n = arr.length;
 int pivot = -1;
 for(int i = n-2;i>=0;i++){
 if(arr[i] < arr[i+1]{
    pivot = i;
    break;
}
    
if(pivot == -1){
    reverse(arr,0,n-1);
    return;
}
for(int i = n-1;i>pivot;i--){
    if(arr[i] > arr[pivot]){
        swap(arr,i,pivot)
        break;
    }
}
reverse(arr,pivot+1,n-1)
}
 
private static void reverse(int[] arr,int start,int end){
while(start < end){
    swap(arr,start++,end--)
    }
}
 

Comments

Popular Posts