category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Tags: implementation constructive

31. Next Permutation

image

Picture source

NOTE

what is next permutation

class Solution {
    public:
    void nextPermutation(vector<int>& arr) {
        int n = arr.size();
        int i = n - 2;
        for (; i >= 0 && arr[i + 1] <= arr[i]; i--) { }
        if (i < 0)
            reverse(arr.begin(), arr.end());

        else {
            int j = i + 1;
            int k = -1;
            for (int j = i + 1; j < n; j++)
                if (arr[i] < arr[j])
                    k = j;
            if (k >= 0)
                swap(arr[i], arr[k]);
            reverse(arr.begin() + i + 1, arr.end());
        }
    }
};