diff --git a/Recursion/Next Permutation/Program.c b/Recursion/Next Permutation/Program.c deleted file mode 100644 index 835a9cf3..00000000 --- a/Recursion/Next Permutation/Program.c +++ /dev/null @@ -1,73 +0,0 @@ -// C++ Program to find the next permutation by generating -// all permutations - -#include -using namespace std; - -// Generates all permutations -void permutations(vector>& result, vector& curr, - int idx); - -// Function to get the next permutation -void nextPermutation(vector& arr) { - - // Generate all permutations and store in res - vector> result; - permutations(result, arr, 0); - sort(result.begin(), result.end()); - - // Traverse through res and find the next permutation - for (int i = 0; i < result.size(); i++) { - - // Found a match - if (result[i] == arr) { - - // Store the next - if (i < result.size() - 1) - arr = result[i + 1]; - - // If the given permutation is the last - if (i == result.size() - 1) - arr = result[0]; - - break; - } - } -} - -// Function to generate all possible permutations -void permutations(vector>& res, vector& arr, - int idx) { - - // Base case: if idx reaches the end of the array - if (idx == arr.size() - 1) { - res.push_back(arr); - return; - } - - // Permutations made by swapping each element - // starting from index idx - for (int i = idx; i < arr.size(); i++) { - - // Swapping - swap(arr[idx], arr[i]); - - // Recursive call to create permutations - // for the next element - permutations(res, arr, idx + 1); - - // Backtracking - swap(arr[idx], arr[i]); - } -} - -int main() { - vector arr = { 2, 4, 1, 7, 5, 0 }; - nextPermutation(arr); - - for (int i = 0; i < arr.size(); i++) { - cout << arr[i] << " "; - } - - return 0; -} \ No newline at end of file diff --git a/Recursion/Next Permutation/Readme.md b/Recursion/Next Permutation/Readme.md deleted file mode 100644 index 758c5bf1..00000000 --- a/Recursion/Next Permutation/Readme.md +++ /dev/null @@ -1,25 +0,0 @@ -## Next Permutation - -# Problem Statement -Given an array arr[] of size n, the task is to print the lexicographically next greater permutation of the given array. If there does not exist any greater permutation, then find the lexicographically smallest permutation of the given array. - -# Approach -The very basic idea that comes to our mind is that we would first generate all possible permutations of a given array and sort them. Once sorted, we locate the current permutation within this list. The next permutation is simply the next arrangement in the sorted order. If the current arrangement is the last in the list then display the first permutation (smallest permutation). - -# Example -If we give any of the above (except the last) as input, we need to find the next one in sequence. If we give last as input, we need to return the first one. - -Examples: - -Input: arr = {2, 4, 1, 7, 5, 0} -Output: { 2, 4, 5, 0, 1, 7 } -Explanation: The next permutation of the given array is 2 4 5 0 1 7 - - -Input: arr = {3, 2, 1} -Output: {1, 2, 3} -Explanation: As arr[] is the last permutation. So, the next permutation is the lowest one. - -# Time and Space Complexity -Time Complexity: O(n!*n*log(n!)), n represents the number of elements present in the input sequence represent all possible permutation. -Auxiliary Space: O(n!), for storing the permutations \ No newline at end of file