Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 25 Sep, 2022
View Discussion
Improve Article
Save Article

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 <bits/stdc++.h>
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 indices
void 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 Code
int 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 approach
import 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 indices
static 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 Code
public 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 indices
def 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 Code
if __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 approach
using 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 indices
static 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 Code
public 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




<script>
 
// JavaScript 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 indices
function rearrangeArray(arr, n)
{
     
    // Sort the given array
    arr.sort((a, b) => a - b);
 
    // Initialize the two pointers
    let j = n - 1;
    let i = 0;
 
    // Traverse the array arr[]
    for(let k = 0; k < n; k++)
    {
         
        // Check if k is even
        if (k % 2 == 0)
        {
            document.write(arr[i] + " ");
 
            // Increment the value
            // of i
            i++;
        }
        else
        {
            document.write(arr[j] + " ");
 
            // Decrement the value
            // of j
            j--;
        }
    }
}
 
// Driver Code
 
    let arr = [ 123, 45, 67, 89, 60, 33 ];
    let N = arr.length;
     
    rearrangeArray(arr, N);
 
// This code is contributed by sanjoy_62.
</script>

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!