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++
#include <bits/stdc++.h>
using namespace std;
void rearrangeArray( int arr[], int n)
{
sort(arr, arr + n);
int j = n - 1;
int i = 0;
for ( int k = 0; k < n; k++) {
if (k % 2 == 0) {
cout << arr[i] << " " ;
i++;
}
else {
cout << arr[j] << " " ;
j--;
}
}
}
int main()
{
int arr[] = { 123, 45, 67, 89, 60, 33 };
int N = sizeof (arr) / sizeof (arr[0]);
rearrangeArray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void rearrangeArray( int arr[], int n)
{
Arrays.sort(arr);
int j = n - 1 ;
int i = 0 ;
for ( int k = 0 ; k < n; k++)
{
if (k % 2 == 0 )
{
System.out.print(arr[i] + " " );
i++;
}
else
{
System.out.print(arr[j] + " " );
j--;
}
}
}
public static void main(String args[])
{
int arr[] = { 123 , 45 , 67 , 89 , 60 , 33 };
int N = arr.length;
rearrangeArray(arr, N);
}
}
|
Python3
def rearrangeArray(arr, n):
arr = sorted (arr)
j = n - 1
i = 0
for k in range (n):
if (k % 2 = = 0 ):
print (arr[i], end = " " )
i + = 1
else :
print (arr[j], end = " " )
j - = 1
if __name__ = = '__main__' :
arr = [ 123 , 45 , 67 , 89 , 60 , 33 ]
N = len (arr)
rearrangeArray(arr, N)
|
C#
using System;
class GFG{
static void rearrangeArray( int [] arr, int n)
{
Array.Sort(arr);
int j = n - 1;
int i = 0;
for ( int k = 0; k < n; k++)
{
if (k % 2 == 0)
{
Console.Write(arr[i] + " " );
i++;
}
else
{
Console.Write(arr[j] + " " );
j--;
}
}
}
public static void Main()
{
int [] arr = { 123, 45, 67, 89, 60, 33 };
int N = arr.Length;
rearrangeArray(arr, N);
}
}
|
Javascript
<script>
function rearrangeArray(arr, n)
{
arr.sort((a, b) => a - b);
let j = n - 1;
let i = 0;
for (let k = 0; k < n; k++)
{
if (k % 2 == 0)
{
document.write(arr[i] + " " );
i++;
}
else
{
document.write(arr[j] + " " );
j--;
}
}
}
let arr = [ 123, 45, 67, 89, 60, 33 ];
let N = arr.length;
rearrangeArray(arr, N);
</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