-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermutation.cpp
More file actions
62 lines (50 loc) · 1.23 KB
/
NextPermutation.cpp
File metadata and controls
62 lines (50 loc) · 1.23 KB
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
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
// Function to find the next permutation
void nextPermutation(vector<int>& arr)
{
int n = arr.size(), i, j;
// Find for the pivot element.
// A pivot is the first element from
// end of sequencewhich doesn't follow
// property of non-increasing suffix
for (i = n - 2; i >= 0; i--) {
if (arr[i] < arr[i + 1]) {
break;
}
}
// Check if pivot is not found
if (i < 0) {
reverse(arr.begin(), arr.end());
}
// if pivot is found
else {
// Find for the successor of pivot in suffix
for (j = n - 1; j > i; j--) {
if (arr[j] > arr[i]) {
break;
}
}
// Swap the pivot and successor
swap(arr[i], arr[j]);
// Minimise the suffix part
reverse(arr.begin() + i + 1, arr.end());
}
}
// Driver code
int main()
{
// Given input array
vector<int> arr = { 1, 2, 3, 6, 5, 4 };
// Function call
nextPermutation(arr);
// Printing the answer
for (auto i : arr) {
cout << i << " ";
}
return 0;
}
Output
1 2 4 3 5 6
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1)