From 6b294307f2e497e8f3aa9a1a6536b1741c1432b5 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:18:29 +0530 Subject: [PATCH 1/2] Made the modification #1120 --- Recursion/Next Permutation/Program.c | 73 ----------------------- Recursion/Next Permutation/Program_next.c | 72 ++++++++++++++++++++++ Recursion/Next Permutation/Readme.md | 2 +- 3 files changed, 73 insertions(+), 74 deletions(-) delete mode 100644 Recursion/Next Permutation/Program.c create mode 100644 Recursion/Next Permutation/Program_next.c 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/Program_next.c b/Recursion/Next Permutation/Program_next.c new file mode 100644 index 00000000..db58f0c6 --- /dev/null +++ b/Recursion/Next Permutation/Program_next.c @@ -0,0 +1,72 @@ +#include + +void swap(int* a, int* b) { + int temp = *a; + *a = *b; + *b = temp; +} + +void reverse(int* nums, int start, int end) { + while (start < end) { + swap(&nums[start], &nums[end]); + start++; + end--; + } +} + +void nextPermutation(int* nums, int numsSize) { + if (numsSize <= 1) return; + + // Step 1: Find the largest index k such that nums[k] < nums[k + 1] + int k = -1; + for (int i = numsSize - 2; i >= 0; i--) { + if (nums[i] < nums[i + 1]) { + k = i; + break; + } + } + + // If no such index exists, the permutation is the last permutation + if (k == -1) { + reverse(nums, 0, numsSize - 1); + return; + } + + // Step 2: Find the largest index l greater than k such that nums[k] < nums[l] + int l = -1; + for (int i = numsSize - 1; i > k; i--) { + if (nums[i] > nums[k]) { + l = i; + break; + } + } + + // Step 3: Swap the value of nums[k] with that of nums[l] + swap(&nums[k], &nums[l]); + + // Step 4: Reverse the sequence from nums[k + 1] to the end + reverse(nums, k + 1, numsSize - 1); +} + +// Helper function to print the array +void printArray(int* nums, int numsSize) { + for (int i = 0; i < numsSize; i++) { + printf("%d ", nums[i]); + } + printf("\n"); +} + +int main() { + int nums[] = {1, 2, 3}; + int numsSize = sizeof(nums) / sizeof(nums[0]); + + printf("Original array: "); + printArray(nums, numsSize); + + nextPermutation(nums, numsSize); + + printf("Next permutation: "); + printArray(nums, numsSize); + + return 0; +} \ No newline at end of file diff --git a/Recursion/Next Permutation/Readme.md b/Recursion/Next Permutation/Readme.md index 758c5bf1..e3325fc5 100644 --- a/Recursion/Next Permutation/Readme.md +++ b/Recursion/Next Permutation/Readme.md @@ -18,7 +18,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. +Explanation: As arr[] is the last permutation. So, the next permutation is the least 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. From 4d1716e7324560066d75c17c6c26ca78bc60e039 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:04:08 +0530 Subject: [PATCH 2/2] Updated --- .../Program_next.c => Next_Permutation/Next Permutation.c} | 0 Recursion/{Next Permutation => Next_Permutation}/Readme.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Recursion/{Next Permutation/Program_next.c => Next_Permutation/Next Permutation.c} (100%) rename Recursion/{Next Permutation => Next_Permutation}/Readme.md (100%) diff --git a/Recursion/Next Permutation/Program_next.c b/Recursion/Next_Permutation/Next Permutation.c similarity index 100% rename from Recursion/Next Permutation/Program_next.c rename to Recursion/Next_Permutation/Next Permutation.c diff --git a/Recursion/Next Permutation/Readme.md b/Recursion/Next_Permutation/Readme.md similarity index 100% rename from Recursion/Next Permutation/Readme.md rename to Recursion/Next_Permutation/Readme.md