Unique sum shuffle for Continuous numbers
Given a number N, the problem requires shuffling the numbers from 1 to N in a way that satisfies a condition, where the sum of the numbers in positions i and N – i + 1 are distinct for all i from 1 to N/2. The task is to output the shuffled numbers that satisfy this condition.
Examples:
Input: N = 6, A[] = {1, 2, 3, 4, 5, 6}
Output: Permutation that satisfies the condition is {3, 2, 1, 4, 5, 6}
Explanation: For all i, in the given array, the value A[i] + A[N – 1 – i] is Distinct i.e A[0] + A[5] = 9 and A[1] + A[4] = 7 and A[2] + A[3] = 5 which are distinct.Input: N = 4, A[] = {1, 2, 3, 4}
Output: Permutation that satisfies the condition is {2, 1, 3, 4}
Explanation: For all i, in the given array, the value A[i] + A[N – 1 – i] is Distinct i.e A[0] + A[3] = 6 and A[1] + A[2] = 4 which are distinct.
Approach: To solve the problem follow the below observation:
The observation is that if we reverse the first half portion, the permutation become such that for all i, [i]+[N] is Distinct for all Indexes.
Below are the steps for the above approach:
- Now create a vector say ans that will store the resultant permutation.
- Run a loop from i = N/2 to i > 0 and push i to vector ans.
- Run a loop from i = (N/2 + 1) to i ≤ N and push i to vector ans.
- Print the resultant vector ans.
Below is the code for the above approach:
C++
|
Given Array : 1 2 3 4 5 6 The Permuation that satisfies the given condition : 3 2 1 4 5 6
Time Complexity: O(N)
Auxiliary Space: O(N), to store the permutation
Given a number N, the problem requires shuffling the numbers from 1 to N in a way that satisfies a condition, where the sum of the numbers in positions i and N – i + 1 are distinct for all i from 1 to N/2. The task is to output the shuffled numbers that satisfy this condition.
Examples:
Input: N = 6, A[] = {1, 2, 3, 4, 5, 6}
Output: Permutation that satisfies the condition is {3, 2, 1, 4, 5, 6}
Explanation: For all i, in the given array, the value A[i] + A[N – 1 – i] is Distinct i.e A[0] + A[5] = 9 and A[1] + A[4] = 7 and A[2] + A[3] = 5 which are distinct.Input: N = 4, A[] = {1, 2, 3, 4}
Output: Permutation that satisfies the condition is {2, 1, 3, 4}
Explanation: For all i, in the given array, the value A[i] + A[N – 1 – i] is Distinct i.e A[0] + A[3] = 6 and A[1] + A[2] = 4 which are distinct.
Approach: To solve the problem follow the below observation:
The observation is that if we reverse the first half portion, the permutation become such that for all i, [i]+[N] is Distinct for all Indexes.
Below are the steps for the above approach:
- Now create a vector say ans that will store the resultant permutation.
- Run a loop from i = N/2 to i > 0 and push i to vector ans.
- Run a loop from i = (N/2 + 1) to i ≤ N and push i to vector ans.
- Print the resultant vector ans.
Below is the code for the above approach:
C++
|
Given Array : 1 2 3 4 5 6 The Permuation that satisfies the given condition : 3 2 1 4 5 6
Time Complexity: O(N)
Auxiliary Space: O(N), to store the permutation