 GFG App
Open App Browser
Continue

# 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 + A = 9 and A + A = 7 and A + A = 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 + A = 6 and A + A = 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++

 `// C++ code for the above approach:` `#include ` `#include ` `using` `namespace` `std;` `int` `main()` `{` `    ``int` `n = 6;`   `    ``// Initially the arrays contains` `    ``cout << ``"Given Array : "``;` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``cout << i + 1 << ``" "``;` `    ``vector<``int``> ans;`   `    ``for` `(``int` `i = n / 2; i > 0; i--) {` `        ``ans.push_back(i);` `    ``}`   `    ``for` `(``int` `i = n / 2 + 1; i <= n; i++) {` `        ``ans.push_back(i);` `    ``}`   `    ``cout << endl;` `    ``cout << ``"The Permutation that satisfies the given "` `            ``"condition : "``;` `    ``for` `(``int` `i = 0; i < ans.size(); i++)` `        ``cout << ans[i] << ``" "``;` `    ``return` `0;` `}`

## Java

 `// java code for the above approach:` `import` `java.util.*;`   `public` `class` `Main {` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `n = ``6``;`   `        ``// Initially the arrays contains` `        ``System.out.print(``"Given Array : "``);` `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``System.out.print(i + ``1` `+ ``" "``);`   `        ``List ans = ``new` `ArrayList();`   `        ``for` `(``int` `i = n / ``2``; i > ``0``; i--) {` `            ``ans.add(i);` `        ``}`   `        ``for` `(``int` `i = n / ``2` `+ ``1``; i <= n; i++) {` `            ``ans.add(i);` `        ``}`   `        ``System.out.println();` `        ``System.out.print(` `            ``"The Permutation that satisfies the given condition : "``);` `        ``for` `(``int` `i = ``0``; i < ans.size(); i++)` `            ``System.out.print(ans.get(i) + ``" "``);` `    ``}` `}`   `// This code is generated by Chetan Bargal`

## Python3

 `# Python code for the above approach:` `n ``=` `6`   `# Initially the arrays contains` `print``(``"Given Array : "``, end``=``"")` `for` `i ``in` `range``(n):` `    ``print``(i ``+` `1``, end``=``" "``)` `ans ``=` `[]`   `for` `i ``in` `range``(n ``/``/` `2``, ``0``, ``-``1``):` `    ``ans.append(i)`   `for` `i ``in` `range``(n ``/``/` `2` `+` `1``, n ``+` `1``):` `    ``ans.append(i)`   `print``(``"\nThe Permutation that satisfies the given condition : "``, end``=``"")` `for` `i ``in` `range``(``len``(ans)):` `    ``print``(ans[i], end``=``" "``)` `    `  `#This code is contributed by thebeginner (Akash Bankar)`

## C#

 `// C# code for the above approach`   `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG {` `    ``static` `void` `Main(``string``[] args)` `    ``{` `        ``int` `n = 6;` `        ``// Initially the arrays contains` `        ``Console.Write(``"Given Array : "``);` `        ``for` `(``int` `i = 0; i < n; i++)` `            ``Console.Write(i + 1 + ``" "``);` `          `  `          ``// ans list to store answer` `        ``List<``int``> ans = ``new` `List<``int``>();` `        `  `          ``// adding first half of original array` `          ``// in reverse order in answer list` `        ``for` `(``int` `i = n / 2; i > 0; i--) {` `            ``ans.Add(i);` `        ``}` `        `  `          ``// adding second half as it is` `        ``for` `(``int` `i = n / 2 + 1; i <= n; i++) {` `            ``ans.Add(i);` `        ``}` `        `  `          ``// Final ans` `        ``Console.WriteLine();` `        ``Console.Write(` `            ``"The Permutation that satisfies the given "` `            ``+ ``"condition : "``);` `        ``for` `(``int` `i = 0; i < ans.Count; i++)` `            ``Console.Write(ans[i] + ``" "``);` `    ``}` `}`

## Javascript

 `// JavaScript code for the above approach:`   `const n = 6;`   `// Initially the arrays contains` `console.log(``"Given Array : "``);` `let original = ``""``;`   `for` `(let i = 0; i < n; i++) {` `    ``original += (i+1);` `    ``original += ``" "``;` `}` `console.log(original);`   `const ans = [];`   `for` `(let i = Math.floor(n / 2); i > 0; i--) {` `    ``ans.push(i);` `}`   `for` `(let i = Math.floor(n / 2) + 1; i <= n; i++) {` `    ``ans.push(i);` `}`   `let res = ``""``;` `console.log(``"\nThe Permutation that satisfies the given condition : "``);` `for` `(let i = 0; i < ans.length; i++) {` `    ``res += ans[i];` `    ``res += ``" "``;` `}` `console.log(res);`

Output

```Given Array : 1 2 3 4 5 6
The Permutation that satisfies the given condition : 3 2 1 4 5 6 ```

Time Complexity: O(N)
Auxiliary Space: O(N), to store the permutation

My Personal Notes arrow_drop_up