-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1508 from Dipanita45/Dipan4
Updated file name
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <stdio.h> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |