Reorder an array such that sum of left half is not equal to sum of right half
Last Updated :
07 Oct, 2022
Given an array arr[] of even length, the task is to check whether is it possible to reorder the array element such that the sum of the left half is not equal to the sum of the right half of the array. If it is possible then print “Yes” with the reordered sequence else print “No”.
Examples:
Input: arr[] = {1, 2, 2, 1, 3, 1}
Output:
Yes
1 1 1 2 2 3
Explanation:
sum of left half = 1 + 2 + 2 = 5
sum of right half = 2 + 2 + 3 = 7
both the sums are not equal.
Input: arr[] = {1, 1}
Output: No
Explanation:
There is no such arrangement possible.
Approach: If all the elements of the array are equal then we can’t reorder the array elements to fulfill the given conditions. Else in the sorted sequence of the given array, the sum of the left half and the right half of the array will always be unequal.
Below is the implementation of the above approach:
C
#include <stdio.h>
#include <stdlib.h>
int compare( const void * a, const void * b)
{
return (*( int *)a - *( int *)b);
}
void printArr( int arr[], int n)
{
qsort (arr, n, sizeof ( int ), compare);
if (arr[0] == arr[n - 1])
{
printf ( "No\n" );
}
else
{
printf ( "Yes\n" );
for ( int i = 0; i < n; i++)
{
printf ( "%d " , arr[i]);
}
}
}
int main()
{
int arr[] = { 1, 2, 2, 1, 3, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
printArr(arr, N);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void printArr( int arr[], int n)
{
sort(arr, arr + n);
if (arr[0] == arr[n - 1]) {
cout << "No" << endl;
}
else {
cout << "Yes" << endl;
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
}
}
int main()
{
int arr[] = { 1, 2, 2, 1, 3, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
printArr(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void printArr( int [] arr, int n)
{
Arrays.sort(arr);
if (arr[ 0 ] == arr[n - 1 ])
{
System.out.println( "No" );
}
else
{
System.out.println( "Yes" );
for ( int i = 0 ; i < n; i++)
{
System.out.print(arr[i] + " " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 1 , 3 , 1 };
int N = arr.length;
printArr(arr, N);
}
}
|
Python3
def printArr(arr, n):
arr.sort()
if (arr[ 0 ] = = arr[n - 1 ]):
print ( "No" )
else :
print ( "Yes" )
for i in range (n):
print (arr[i], end = " " )
print ()
if __name__ = = '__main__' :
arr = [ 1 , 2 , 2 , 1 , 3 , 1 ]
N = len (arr)
printArr(arr, N)
|
C#
using System;
class GFG{
public static void printArr( int [] arr, int n)
{
Array.Sort(arr);
if (arr[0] == arr[n - 1])
{
Console.Write( "No\n" );
}
else
{
Console.Write( "Yes\n" );
for ( int i = 0; i < n; i++)
{
Console.Write(arr[i] + " " );
}
}
}
public static void Main()
{
int [] arr = new int [6]{ 1, 2, 2, 1, 3, 1 };
int N = arr.Length;
printArr(arr, N);
}
}
|
Javascript
<script>
function printArr(arr , n)
{
arr.sort();
if (arr[0] == arr[n - 1]) {
document.write( "No<br/>" );
}
else {
document.write( "Yes<br/>" );
for (i = 0; i < n; i++) {
document.write(arr[i] + " " );
}
}
}
var arr = [ 1, 2, 2, 1, 3, 1 ];
var N = arr.length;
printArr(arr, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1) because constant space for variables is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...