Q-9 Next Permutation
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)
SOLUTION :
// { Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-- > 0){
int N = Integer.parseInt(in.readLine());
String a[] = in.readLine().trim().split("\\s+");
int arr[] = new int[N];
for(int i = 0;i < N;i++)
arr[i] = Integer.parseInt(a[i]);
Solution ob = new Solution();
List<Integer> ans = new ArrayList<Integer>();
ans = ob.nextPermutation(N, arr);
StringBuilder out = new StringBuilder();
for(int i = 0;i < N;i++)
out.append(ans.get(i) + " ");
System.out.println(out);
}
}
}// } Driver Code Ends
// User function Template for Java
class Solution{
static List<Integer> nextPermutation(int N, int arr[]){
// code here
ArrayList<Integer> a= new ArrayList<>();
int idx=-1;
for(int i=N-2;i>=0;i--)
{
if(arr[i]<arr[i+1])
{
idx=i;
break;
}
}
if(idx!=-1)
{
int minindex=idx+1;
for(int j=idx+1;j<N;j++)
{
if(arr[j]>arr[idx] && arr[j]<arr[minindex])
minindex=j;
}
int temp =arr[idx];
arr[idx]= arr[minindex];
arr[minindex]=temp;
}
int i=idx+1;
int j=N-1;
while(i<=j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
for(int k=0;k<N;k++)
{
a.add(arr[k]);
}
return a;
}
}
Company Tags : FactSet Hike Amazon MakeMyTrip Qualcomm Infosys Microsoft Google Salesforce Flipkart
QUESTION LINK : https://practice.geeksforgeeks.org/problems/next-permutation5226/1#
QUESTION LINK : https://practice.geeksforgeeks.org/problems/next-permutation5226/1#
Post a Comment
0 Comments