Professional Documents
Culture Documents
Problem Difficulty: 2
Problem Constraints: 1 <= T <= 100 <br>
1 <= N <= 1000 <br>
0 <= A<sub>i</sub> <= 9
Problem Description:
Given an array Arr[], Treat each element of the array as the digit and whole array
as the number. Implement the next permutation, which rearranges numbers into the
numerically next greater permutation of numbers.
Output Format: Print the Next Permutation for each number separated by a new Line.
Sample Output: 1 3 2
1 2 3
=====Solution=====
import java.util.*;
public class Main {
int t = scn.nextInt();
while ((t--) > 0) {
int n = scn.nextInt();
int[] arr = takeInput(n);
next_permutation(arr);
display(arr);
}
}
int p = 0;
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
p = i;
break;
}
}
int q = 0;
for (int i = nums.length - 1; i > p; i--) {
if (nums[i] > nums[p]) {
q = i;
break;
}
}
if (p == 0 && q == 0) {
reverse(nums, 0, nums.length - 1);
return;
}
if (p < nums.length - 1) {
reverse(nums, p + 1, nums.length - 1);
}
}
System.out.println();
}
}