forked from PrafullRaj/Hactober2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNext Permutation.java
52 lines (51 loc) · 1.38 KB
/
Next Permutation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
The next permutation of an array of integers is the next lexicographically greater permutation. If such arrangement is not possible, the array must be rearranged as the lowest possible order (in ascending order).
Given an array of integers nums[], find the next permutation of nums.
*/
public void nextPermutation(int[] nums)
{
int ind1=-1;
int ind2=-1;
// find breaking point
for(int i=nums.length-2;i>=0;i--)
{
if(nums[i]<nums[i+1])
{
ind1=i;
break;
}
}
// if there is no breaking point
if(ind1==-1)
reverse(nums,0);
else
{ // find next greater element and swap with ind2
for(int i=nums.length-1;i>=0;i--)
{
if(nums[i]>nums[ind1]){
ind2=i;
break;
}
}
swap(nums,ind1,ind2);
// reverse the rest right half
reverse(nums,ind1+1);
}
}
void swap(int[] nums,int i,int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
void reverse(int[] nums,int start)
{
int i=start;
int j=nums.length-1;
while(i<j)
{
swap(nums,i,j);
i++;
j--;
}
}