Open In App

# Find the Array Permutation having sum of elements at odd indices greater than sum of elements at even indices

Given an array arr[] consisting of N integers, the task is to find the permutation of array elements such that the sum of odd indices elements is greater than or equal to the sum of even indices elements.

Examples:

Input: arr[] = {1, 2, 3, 4}
Output: 1 4 2 3
Explanation:
Consider the permutation of the given array as {1, 4, 2, 3}.
Now, the sum of elements at odd indices = (4 + 3) = 7 and the sum of elements at even indices = (1 + 2) = 3.
As the sum at odd indices elements is greater than the sum of even indices element. Therefore, print the current permutation.

Input: arr[] = {123, 45, 67, 89, 60, 33}
Output: 33 123 45 89 60 67

Naive Approach: The simplest approach to solve the given problem is to generate all possible permutations of the given array and print that permutation of the array whose sum of odd indices elements is greater than or equal to the sum of even indices elements.

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

Efficient Approach: The above approach can also be optimized by sorting the given array and using Two Pointer Approach. Follow the steps below to solve the problem:

Below is the implementation of the above approach :

## C++

 // C++ program for the above approach #include using namespace std; // Function to find the permutation of// array elements such that the sum of// elements at odd ?indices is greater// than sum of elements at even indicesvoid rearrangeArray(int arr[], int n){    // Sort the given array    sort(arr, arr + n);     // Initialize the two pointers    int j = n - 1;    int i = 0;     // Traverse the array arr[]    for (int k = 0; k < n; k++) {         // Check if k is even        if (k % 2 == 0) {            cout << arr[i] << " ";             // Increment the value            // of i            i++;        }        else {            cout << arr[j] << " ";             // Decrement the value            // of j            j--;        }    }} // Driver Codeint main(){    int arr[] = { 123, 45, 67, 89, 60, 33 };    int N = sizeof(arr) / sizeof(arr[0]);    rearrangeArray(arr, N);     return 0;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{     // Function to find the permutation of// array elements such that the sum of// elements at odd ?indices is greater// than sum of elements at even indicesstatic void rearrangeArray(int arr[], int n){         // Sort the given array    Arrays.sort(arr);     // Initialize the two pointers    int j = n - 1;    int i = 0;     // Traverse the array arr[]    for(int k = 0; k < n; k++)    {                 // Check if k is even        if (k % 2 == 0)        {            System.out.print(arr[i] + " ");             // Increment the value            // of i            i++;        }        else        {            System.out.print(arr[j] + " ");             // Decrement the value            // of j            j--;        }    }} // Driver Codepublic static void main(String args[]){    int arr[] = { 123, 45, 67, 89, 60, 33 };    int N = arr.length;         rearrangeArray(arr, N);}} // This code is contributed by avijitmondal1998

## Python3

 # Python3 program for the above approach # Function to find the permutation of# array elements such that the sum of# elements at odd ?indices is greater# than sum of elements at even indicesdef rearrangeArray(arr, n):         # Sort the given array    arr = sorted(arr)     # Initialize the two pointers    j = n - 1    i = 0     # Traverse the array arr[]    for k in range(n):                 # Check if k is even        if (k % 2 == 0):            print(arr[i], end = " ")                         # Increment the value            # of i            i += 1        else:            print(arr[j], end = " ")             # Decrement the value            # of j            j -= 1 # Driver Codeif __name__ == '__main__':     arr = [ 123, 45, 67, 89, 60, 33 ]    N = len(arr)         rearrangeArray(arr, N) # This code is contributed by mohit kumar 29

## C#

 // C# program for the above approachusing System; class GFG{ // Function to find the permutation of// array elements such that the sum of// elements at odd ?indices is greater// than sum of elements at even indicesstatic void rearrangeArray(int[] arr, int n){         // Sort the given array    Array.Sort(arr);     // Initialize the two pointers    int j = n - 1;    int i = 0;     // Traverse the array arr[]    for(int k = 0; k < n; k++)    {                 // Check if k is even        if (k % 2 == 0)        {            Console.Write(arr[i] + " ");             // Increment the value            // of i            i++;        }        else        {            Console.Write(arr[j] + " ");             // Decrement the value            // of j            j--;        }    }} // Driver Codepublic static void Main(){    int[] arr = { 123, 45, 67, 89, 60, 33 };    int N = arr.Length;     rearrangeArray(arr, N);}} // This code is contributed by subham348

## Javascript



Output:

33 123 45 89 60 67

Time Complexity: O(N*log N)
Auxiliary Space: O(1) as it is using constant space for variables