# Rearrange Array to minimize difference of sum of squares of odd and even index elements

• Last Updated : 31 Mar, 2022

Given an array arr[] of size N (multiple of 8) where the values in the array will be in the range [a, (a+8*N) -1] (a can be any positive integer), the task is to rearrange the array in a way such that the difference between the sum of squares at odd indices and sum of squares of the elements at even indices is the minimum among all possible rearrangements.

Note: If there are multiple rearrangements return any one of those.

Examples:

Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 1 2 4 3 7 8 6 5
Explanation:  The difference is 0 as 1 + 42 + 72 + 62 = 102 = 22 + 32 + 82 + 52

Input: arr[] = { 9, 11, 12, 15, 16, 13, 10, 14}
Output: 9 10 12 11 15 16 14 13
Explanation: The difference is 0 as 92 + 122 + 152 + 142 = 102 + 112 + 162 + 132 = 646

Approach: This problem can be solved based on the following mathematical observation:

For any positive integer S, ( S )2 + ( S+3 )2 – 4 = ( S+1 )2 + ( S+2 )2. As N is a multiple of 8 so it can be divided into N/8 groups where the difference of sum of squares of elements at odd and even indices for each group is 0.
For the first four elements keep the sum of squares at odd indices greater and four the next four just the opposite to keep the sum of squares of even indices more.  So this group of 8 elements will have difference 0. As the similar is done for all N/8 groups the overall difference will be 0.

The sequence of each group can be like: S, S+1, S+3, S+2, S+6, S+7, S+5, S+4

Follow the below steps to solve this problem:

• Divide the array into groups of size 8.
• Arrange elements in each group as derived from the observation.
• Return the rearranged array.

Below is the implementation of the above approach:

## C++

 `// C++ code to implement the approach` `#include ``using` `namespace` `std;` `// Function to find``// maximum element of the array``int` `maximum(``int` `arr[], ``int` `size)``{``    ``int` `ma = INT_MIN;``    ``for` `(``int` `i = 0; i < size; i++) {``        ``ma = max(ma, arr[i]);``    ``}``    ``return` `ma;``}` `// Function to find``// minimum element of the array``int` `minimum(``int` `arr[], ``int` `size)``{``    ``int` `mi = INT_MAX;``    ``for` `(``int` `i = 0; i < size; i++) {``        ``mi = min(mi, arr[i]);``    ``}``    ``return` `mi;``}` `// Function to print the array``void` `print_min(``int` `arr[], ``int` `size)``{``    ``int` `low = minimum(arr, size);``    ``int` `high = maximum(arr, size);` `    ``// using the fact that``    ``// s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.``    ``for` `(``int` `i = 0; i < size; i += 4) {` `        ``// Making the difference +4``        ``// for the odd indices``        ``if` `(i % 8 == 0) {``            ``arr[i] = low;``            ``arr[i + 2] = low + 3;``            ``arr[i + 1] = low + 1;``            ``arr[i + 3] = low + 2;``        ``}` `        ``// Making the difference -4 for``        ``// odd indices +4 - 4 = 0 (balanced)``        ``else` `{``            ``arr[i] = low + 2;``            ``arr[i + 2] = low + 1;``            ``arr[i + 1] = low + 3;``            ``arr[i + 3] = low;``        ``}``        ``low += 4;``    ``}` `    ``// Printing the array``    ``for` `(``int` `i = 0; i < size; i++) {``        ``cout << arr[i] << ``" "``;``    ``}``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };``    ``int` `N = ``sizeof``(arr) / (``sizeof``(``int``));` `    ``// Function call``    ``print_min(arr, N);``    ``return` `0;``}`

## Java

 `// JAVA code to implement the approach``import` `java.util.*;``class` `GFG``{` `  ``// Function to find``  ``// maximum element of the array``  ``public` `static` `int` `maximum(``int` `arr[], ``int` `size)``  ``{``    ``int` `ma = Integer.MIN_VALUE;``    ``for` `(``int` `i = ``0``; i < size; i++) {``      ``ma = Math.max(ma, arr[i]);``    ``}``    ``return` `ma;``  ``}` `  ``// Function to find``  ``// minimum element of the array``  ``public` `static` `int` `minimum(``int` `arr[], ``int` `size)``  ``{``    ``int` `mi = Integer.MAX_VALUE;``    ``for` `(``int` `i = ``0``; i < size; i++) {``      ``mi = Math.min(mi, arr[i]);``    ``}``    ``return` `mi;``  ``}` `  ``// Function to print the array``  ``public` `static` `void` `print_min(``int` `arr[], ``int` `size)``  ``{``    ``int` `low = minimum(arr, size);``    ``int` `high = maximum(arr, size);` `    ``// using the fact that``    ``// s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.``    ``for` `(``int` `i = ``0``; i < size; i += ``4``) {` `      ``// Making the difference +4``      ``// for the odd indices``      ``if` `(i % ``8` `== ``0``) {``        ``arr[i] = low;``        ``arr[i + ``2``] = low + ``3``;``        ``arr[i + ``1``] = low + ``1``;``        ``arr[i + ``3``] = low + ``2``;``      ``}` `      ``// Making the difference -4 for``      ``// odd indices +4 - 4 = 0 (balanced)``      ``else` `{``        ``arr[i] = low + ``2``;``        ``arr[i + ``2``] = low + ``1``;``        ``arr[i + ``1``] = low + ``3``;``        ``arr[i + ``3``] = low;``      ``}``      ``low += ``4``;``    ``}` `    ``// Printing the array``    ``for` `(``int` `i = ``0``; i < size; i++) {``      ``System.out.print(arr[i] + ``" "``);``    ``}``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `arr[] = { ``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8` `};``    ``int` `N = arr.length;` `    ``// Function call``    ``print_min(arr, N);``  ``}``}` `// This code is contributed by Taranpreet`

## Python3

 `# Python code to implement the approach``INT_MIN ``=` `-``2147483647` `-` `1``INT_MAX ``=` `2147483647` `# Function to find``# maximum element of the array``def` `maximum(arr, size):``    ``ma ``=` `INT_MIN``    ``for` `i ``in` `range``(size):``        ``ma ``=` `max``(ma, arr[i])``    ` `    ``return` `ma` `# Function to find``# minimum element of the array``def` `minimum(arr, size):``    ``mi ``=` `INT_MAX``    ``for` `i ``in` `range``(size):``        ``mi ``=` `min``(mi, arr[i])``   ` `    ``return` `mi` `# Function to print the array``def` `print_min(arr, size):``    ``low ``=` `minimum(arr, size)``    ``high ``=` `maximum(arr, size)` `    ``# using the fact that``    ``# s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.``    ``for` `i ``in` `range``(``0``,size,``4``):` `        ``# Making the difference +4``        ``# for the odd indices``        ``if` `(i ``%` `8` `=``=` `0``):``            ``arr[i] ``=` `low``            ``arr[i ``+` `2``] ``=` `low ``+` `3``            ``arr[i ``+` `1``] ``=` `low ``+` `1``            ``arr[i ``+` `3``] ``=` `low ``+` `2` `        ``# Making the difference -4 for``        ``# odd indices +4 - 4 = 0 (balanced)``        ``else``:``            ``arr[i] ``=` `low ``+` `2``            ``arr[i ``+` `2``] ``=` `low ``+` `1``            ``arr[i ``+` `1``] ``=` `low ``+` `3``            ``arr[i ``+` `3``] ``=` `low``        ` `        ``low ``+``=` `4` `    ``# Printing the array``    ``for` `i ``in` `range``(size):``        ``print``(arr[i],end``=``" "``)`  `# Driver code` `arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``]``N ``=` `len``(arr)` `# Function call``print_min(arr, N)` `# This code is contributed by shinjanpatra`

## C#

 `// C# code to implement the approach``using` `System;``class` `GFG {` `  ``// Function to find``  ``// maximum element of the array``  ``static` `int` `maximum(``int``[] arr, ``int` `size)``  ``{``    ``int` `ma = Int32.MinValue;``    ``for` `(``int` `i = 0; i < size; i++) {``      ``ma = Math.Max(ma, arr[i]);``    ``}``    ``return` `ma;``  ``}` `  ``// Function to find``  ``// minimum element of the array``  ``static` `int` `minimum(``int``[] arr, ``int` `size)``  ``{``    ``int` `mi = Int32.MaxValue;``    ``for` `(``int` `i = 0; i < size; i++) {``      ``mi = Math.Min(mi, arr[i]);``    ``}``    ``return` `mi;``  ``}` `  ``// Function to print the array``  ``static` `void` `print_min(``int``[] arr, ``int` `size)``  ``{``    ``int` `low = minimum(arr, size);``    ``int` `high = maximum(arr, size);` `    ``// using the fact that``    ``// s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.``    ``for` `(``int` `i = 0; i < size; i += 4) {` `      ``// Making the difference +4``      ``// for the odd indices``      ``if` `(i % 8 == 0) {``        ``arr[i] = low;``        ``arr[i + 2] = low + 3;``        ``arr[i + 1] = low + 1;``        ``arr[i + 3] = low + 2;``      ``}` `      ``// Making the difference -4 for``      ``// odd indices +4 - 4 = 0 (balanced)``      ``else` `{``        ``arr[i] = low + 2;``        ``arr[i + 2] = low + 1;``        ``arr[i + 1] = low + 3;``        ``arr[i + 3] = low;``      ``}``      ``low += 4;``    ``}` `    ``// Printing the array``    ``for` `(``int` `i = 0; i < size; i++) {``      ``Console.Write(arr[i] + ``" "``);``    ``}``  ``}` `  ``// Driver code``  ``public` `static` `void` `Main()``  ``{``    ``int``[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };``    ``int` `N = arr.Length;` `    ``// Function call``    ``print_min(arr, N);``  ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`1 2 4 3 7 8 6 5 `

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up