diff --git a/Recursion/Next_Permutation/Next Permutation.c b/Recursion/Next_Permutation/Next Permutation.c new file mode 100644 index 00000000..db58f0c6 --- /dev/null +++ b/Recursion/Next_Permutation/Next Permutation.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 new file mode 100644 index 00000000..e3325fc5 --- /dev/null +++ b/Recursion/Next_Permutation/Readme.md @@ -0,0 +1,25 @@ +## 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 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. +Auxiliary Space: O(n!), for storing the permutations \ No newline at end of file