Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool NextPermutation(int arr[], int sz) {
- int n = sz;
- int i = n - 2;
- while (i >= 0 && arr[i] >= arr[i + 1]) {
- i--;
- }
- if (i == -1) {
- return false;
- }
- int j = i + 1;
- while (j < n && arr[j] > arr[i]) {
- j++;
- }
- j--;
- swap(arr[i], arr[j]);
- int left = i + 1;
- int right = n - 1;
- while (left < right) {
- swap(arr[left], arr[right]);
- left++;
- right--;
- }
- return true;
- }
- int main() {
- int arr[] = {1, 2, 3};
- while (true) {
- for (int i = 0; i < 3; i++) {
- cout << arr[i] << " ";
- }
- cout << '\n';
- if (!NextPermutation(arr, 3)) {
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement