forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NextPermutation.cpp
53 lines (46 loc) · 951 Bytes
/
NextPermutation.cpp
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
53
/**
Given an array, fing the next lexicographical largest permutation
**/
#include <bits/stdc++.h>
using namespace std;
vector<int> nextPermutation(vector<int> &A) {
int k = A.size()-1;
while(A[k-1] >= A[k] && k > 0)
k--;
if(k == 0)
reverse(A.begin(), A.end());
else {
int t = A[k-1];
int m = k;
for(int i=k+1; i<A.size(); i++)
if(A[i] > t && A[i] < A[k])
m = i;
A[k-1] = A[m];
A[m] = t;
sort(A.begin()+k, A.end());
}
return A;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; i++)
cin >> arr[i];
nextPermutation(arr);
cout << "The next permutation of the array is : \n";
for(int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << '\n';
return 0;
}
/**
Input :
6
5 3 4 9 7 6
Output :
6
5 3 4 9 7 6
Time Complexity : O(nlogn)
Space Complexity : O(n)
**/