Reorder an array such that sum of left half is not equal to sum of right half
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
// C program for the above approach #include <stdio.h> #include <stdlib.h> // A comparator function used by qsort int compare( const void * a, const void * b) { return (*( int *)a - *( int *)b); } // Function to print the required // reordering of array if possible void printArr( int arr[], int n) { // Sort the array in increasing order qsort (arr, n, sizeof ( int ), compare); // If all elements are equal, then // it is not possible if (arr[0] == arr[n - 1]) { printf ( "No\n" ); } // Else print the sorted array arr[] else { printf ( "Yes\n" ); for ( int i = 0; i < n; i++) { printf ( "%d " , arr[i]); } } } // Driver Code int main() { // Given array int arr[] = { 1, 2, 2, 1, 3, 1 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call printArr(arr, N); return 0; } // This code is contributed by equbalzeeshan |
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the required // reordering of array if possible void printArr( int arr[], int n) { // Sort the array in increasing order sort(arr, arr + n); // If all elements are equal, then // it is not possible if (arr[0] == arr[n - 1]) { cout << "No" << endl; } // Else print the sorted array arr[] else { cout << "Yes" << endl; for ( int i = 0; i < n; i++) { cout << arr[i] << " " ; } } } // Driver Code int main() { // Given array int arr[] = { 1, 2, 2, 1, 3, 1 }; int N = sizeof (arr) / sizeof (arr[0]); // Function Call printArr(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to print the required // reordering of array if possible public static void printArr( int [] arr, int n) { // Sort the array in increasing order Arrays.sort(arr); // If all elements are equal, then // it is not possible if (arr[ 0 ] == arr[n - 1 ]) { System.out.println( "No" ); } // Else print the sorted array arr[] else { System.out.println( "Yes" ); for ( int i = 0 ; i < n; i++) { System.out.print(arr[i] + " " ); } } } // Driver code public static void main(String[] args) { // Given array int arr[] = { 1 , 2 , 2 , 1 , 3 , 1 }; int N = arr.length; // Function call printArr(arr, N); } } // This code is contributed by equbalzeeshan |
Python3
# Python3 program for the above approach # Function to print the required # reordering of the array if possible def printArr(arr, n): # Sort the array in increasing # order arr.sort() # If all elements are equal, # then it is not possible if (arr[ 0 ] = = arr[n - 1 ]): print ( "No" ) # Else print the sorted # array arr[] else : print ( "Yes" ) for i in range (n): print (arr[i], end = " " ) print () # Driver Code if __name__ = = '__main__' : # Given array arr = [ 1 , 2 , 2 , 1 , 3 , 1 ] N = len (arr) # Function Call printArr(arr, N) # This code is contributed by Shivam Singh |
C#
// C# code for the above approach using System; class GFG{ // Function to print the required // reordering of array if possible public static void printArr( int [] arr, int n) { // Sort the array in increasing order Array.Sort(arr); // If all elements are equal, then // it is not possible if (arr[0] == arr[n - 1]) { Console.Write( "No\n" ); } // Else print the sorted array arr[] else { Console.Write( "Yes\n" ); for ( int i = 0; i < n; i++) { Console.Write(arr[i] + " " ); } } } // Driver code public static void Main() { // Given array int [] arr = new int [6]{ 1, 2, 2, 1, 3, 1 }; int N = arr.Length; // Function call printArr(arr, N); } } // This code is contributed by equbalzeeshan |
Javascript
<script> // Javascript program for the above approach // Function to print the required // reordering of array if possible function printArr(arr , n) { // Sort the array in increasing order arr.sort(); // If all elements are equal, then // it is not possible if (arr[0] == arr[n - 1]) { document.write( "No<br/>" ); } // Else print the sorted array arr else { document.write( "Yes<br/>" ); for (i = 0; i < n; i++) { document.write(arr[i] + " " ); } } } // Driver code // Given array var arr = [ 1, 2, 2, 1, 3, 1 ]; var N = arr.length; // Function call printArr(arr, N); // This code contributed by umadevi9616 </script> |
Yes 1 1 1 2 2 3
Time Complexity: O(N*log(N))
Auxiliary Space: O(1) because constant space for variables is used
Please Login to comment...