Given an array **A[]** consisting of **N** distinct integers, the task is to rearrange the given array such that the sum of every same-indexed non-empty subsets of size less than **N**, is not equal to their sum in the original array.**Examples:**

Input:A[] = {1000, 100, 10, 1}Output:100 10 1 1000Explanation:

Original Array A[] = {1000, 100, 10, 1}

Final Array B[] = {100, 10, 1, 1000}

Subsets of size 1:A[0] = 1000 B[0] = 100 A[1] = 100 B[1] = 10 A[2] = 10 B[2] = 1 A[3] = 1 B[3] = 1000Subsets of size 2:

{A[0], A[1]} = 1100 {B[0], B[1]} = 110 {A[0], A[2]} = 1010 {B[0], B[2]} = 101 {A[1], A[2]} = 110 {B[1], B[2]} = 11 ..... Similarly, all same-indexed subsets of size 2 have a different sum.Subsets of size 3:

{A[0], A[1], A[2]} = 1110 {B[0], B[1], B[2]} = 111 {A[0], A[2], A[3]} = 1011 {B[0], B[2], B[3]} = 1101 {A[1], A[2], A[3]} = 111 {B[1], B[2], B[3]} = 1011Therefore, no same-indexed subsets have equal sum.

Input:A[] = {1, 2, 3, 4, 5}Output:5 1 2 3 4

**Approach:**

The idea is to simply replace every array element except one, by a smaller element. Follow the steps below to solve the problem:

- Store the array elements in pairs of
**{A[i], i}**. - Sort the pairs in ascending order of the array elements
- Now, traverse the sorted order, and insert each element at the original index of its next greater element(i.e. at the index
**v[(i + 1) % n].second**). This ensures that every index except one, now has a smaller element than the previous value stored in it.

Proof:

Let S = { arr_{1}, arr_{2}, …, arr_{k}} be a subset.

Ifu does not belong to Sinitially, upon insertion ofuinto S, the sum of the subset changes.

Similarly, ifu belongs to S,letS’contains all the elements not present inS. This means thatu do not belong to S’. Then, by the same reasoning above, the sum of the subset S’ differs from its original sum.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to rearrange the array such ` `// that no same-indexed subset have sum ` `// equal to that in the original array ` `void` `printNewArray(vector<` `int` `> a, ` `int` `n) ` `{ ` ` ` `// Initialize a vector ` ` ` `vector<pair<` `int` `, ` `int` `> > v; ` ` ` ` ` `// Iterate the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `v.push_back({ a[i], i }); ` ` ` `} ` ` ` ` ` `// Sort the vector ` ` ` `sort(v.begin(), v.end()); ` ` ` ` ` `int` `ans[n]; ` ` ` ` ` `// Shift of elements to the ` ` ` `// index of its next cyclic element ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `ans[v[(i + 1) % n].second] ` ` ` `= v[i].first; ` ` ` `} ` ` ` ` ` `// Print the answer ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `cout << ans[i] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> a = { 4, 1, 2, 5, 3 }; ` ` ` ` ` `int` `n = a.size(); ` ` ` ` ` `printNewArray(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `class` `pair ` `{ ` ` ` `int` `first, second; ` ` ` ` ` `pair(` `int` `first, ` `int` `second) ` ` ` `{ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` `} ` ` ` `// Function to rearrange the array such ` `// that no same-indexed subset have sum ` `// equal to that in the original array ` `static` `void` `printNewArray(List<Integer> a, ` `int` `n) ` `{ ` ` ` ` ` `// Initialize a vector ` ` ` `List<pair> v = ` `new` `ArrayList<>(); ` ` ` ` ` `// Iterate the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `v.add(` `new` `pair(a.get(i), i)); ` ` ` `} ` ` ` ` ` `// Sort the vector ` ` ` `Collections.sort(v, (pair s1, pair s2) -> ` ` ` `{ ` ` ` `return` `s1.first - s2.first; ` ` ` `}); ` ` ` ` ` `int` `ans[] = ` `new` `int` `[n]; ` ` ` ` ` `// Shift of elements to the ` ` ` `// index of its next cyclic element ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `ans[v.get((i + ` `1` `) % n).second] = v.get(i).first; ` ` ` `} ` ` ` ` ` `// Print the answer ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `System.out.print(ans[i] + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `List<Integer> a = Arrays.asList(` `4` `, ` `1` `, ` `2` `, ` `5` `, ` `3` `); ` ` ` ` ` `int` `n = a.size(); ` ` ` `printNewArray(a, n); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

**Output:**

3 5 1 4 2

**Time Complexity: **O(N log N) **Auxiliary Space: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Print n smallest elements from given array in their original order
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Split array into K subsets to maximize their sum of maximums and minimums
- Searching in an array where adjacent differ by at most k
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Divide a big number into two parts that differ by k
- Rearrange array such that all even-indexed elements in the Array is even
- Count Derangements (Permutation such that no element appears in its original position)
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Rearrange an array such that 'arr[j]' becomes 'i' if 'arr[i]' is 'j' | Set 1
- Rearrange an array such that every odd indexed element is greater than it previous
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Rearrange array such that even positioned are greater than odd

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.