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 <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; } |
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 |
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.
Recommended Posts:
- Find prime numbers in the first half and second half of an array
- Find the XOR of first half and second half elements of an array
- Find the sum of the first half and second half elements of an array
- Minimum insertions to make XOR of an Array equal to half of its sum
- Sort first half in ascending and second half in descending order | 1
- Sort first half in ascending and second half in descending order | Set 2
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Count number of rotated strings which have more number of vowels in the first half than second half
- Number of subarrays such that XOR of one half is equal to the other
- Reduce every element of the array to it's half retaining the sum zero
- Check if at least half array is reducible to zero by performing some operations
- Find if array has an element whose value is half of array sum
- Check if minimum element in array is less than or equals half of every other element
- Find elements larger than half of the elements in an array
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Minimum length of square to contain at least half of the given Coordinates
- Check if the frequency of any character is more than half the length of the string
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Find an element in array such that sum of left array is equal to sum of right array
- Reorder an array according to given indexes
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

