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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

Yes
1 1 1 2 2 3

Time Complexity: O(N*log(N))

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : SHIVAMSINGH67