Find permutation of numbers 1 to N having X local maxima (peaks) and Y local minima (valleys)
Given three integers N, A and B, the task is to find a permutation of pairwise distinct numbers from 1 to N that has exactly ‘A’ local minima’s and ‘B’ local maxima’s.
- A local minima is defined as the element which is less than both its neighbours.
- A local maxima is defined as the element which is greater than both its neighbours.
- The first and last element of the entire permutation can never be local minima or maxima.
If no such permutations exist print -1.
Input: N = 6 , A = 2 , B = 2
Output: 1, 3, 2, 5, 4, 6
2 local minima’s: 2 and 5
2 local maxima’s: 3 and 5
Input: N = 5 , A = 2 , B = 2
Naive Approach (Brute Force): In this approach, generate all permutations of 1 to N numbers and check each one individually. Follow the below steps, to solve this problem:
- Generate all the permutations of numbers from 1 to N and store them in an array.
- Traverse through each permutation, if the following permutation has exactly A local minima’s and B local maxima’s, print the permutation.
- If no such permutation exists, then print -1.
Below is the implementation of the above approach :
1 3 2 5 4 6
Time Complexity: O(N!)
Auxiliary Space: O(N!)
Efficient Approach (Greedy Method):
The above brute force method can be optimized using the Greedy Algorithm. Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. So, break the problem into different usable pieces according to the values of N, A, B. Follow the below steps to solve this problem:
- As the first and the last element of an entire permutation cannot be maxima or minima so the total number of maxima’s and minima’s must be less than or equal to N-2. So if (A + B > N -2), print -1.
- Also, there cannot be two consecutive minima’s or maxima’s. There must be a minima in between two consecutive maxima’s and there must be a maxima in between two consecutive minima’s. So the absolute difference between the total number of minima’s and maxima’s must be less than or equal to 1. So if the absolute difference between A and B exceeds 1 then print -1. We can easily visualize that from the image below.
- After the two corner cases are done, create two variables, minValue to store the minimum value of the permutation, and maxValue to store the maxValue. Now divide the problem into three different cases that are (A > B), (A < B) and (A=B). Now solve each case individually:
- If (A > B): Initialize the minValue as 1 and fill the array with the minValue starting from index 2 for A times. After each insertion increase the minValue by 1 as the values should be distinct. While filling, make sure to leave one index empty after each insertion as it is not possible for two minima’s to reside consecutively. After creating A minima’s fill the rest of the array in increasing order so that no new minima is created.
- If (B > A): Initialize the maxValue as N and fill the array with the maxValue starting from index 2 for B times. After each insertion decrease the maxValue by 1 as the values should be distinct. While filling, make sure to leave one index empty after each insertion as it is not possible for two maxima’s to reside consecutively. After creating B maxima’s fill the rest of the array in decreasing order so that no new maxima is created.
- If (A = B): then initialize two values minValue as 1 and maxValue as N. Fill the first element of the array with minValue and increase the minValue by 1. Then fill the even indices with maxValue and decrease maxValue by 1 and fill the odd indices with minValue and increase the minValue by 1 for A times. Now, fill the rest of the positions in increasing order.
Below is the implementation of the above approach:
4 6 3 5 2 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...