Given an array of n-positive elements. In each operation, you can select some elements and decrease them by 1 and increase remaining elements by m. The task is to determine that whether after some iterations is it possible to have at least half of elements of given array equal to zero or not.
Examples:
Input : arr[] = {3, 5, 6, 8}, m = 2
Output : Yes
Input : arr[] = {4, 7, 12, 13, 34}, m = 7
Output : No
If we try to analyze the problem statement we will find that at any step either you are decreasing an element by 1 or increasing it by m. That means on every single step performed, there are total three possibilities if we compare two elements.
Let a1 and a2 are two elements then:
- Either both element got decreased by 1 and hence resulting no change in their actual difference.
- Both element got increased by m and hence resulting no change in their actual difference again.
- One element got decreased by 1 and other one got increased by m and hence resulting a change of (m+1) in their actual difference.
It means on every step you are going to keep the difference between any two element either same or increase it by (m+1).
So if you take the modulo of all element by (m+1) and keep its frequency we can check how many elements can be made equal to zero at any point of time.
Algorithm :
- Create a hash table of size m+1
- Capture the frequency of elements as (arr[i] % (m+1)) and store in hash table.
- Find out the maximum frequency and if it is greater than or equals to n/2, then the answer is YES otherwise NO.
Below is the implementation of the above approach:
C++
// C++ program to find whether half-array // reducible to 0 #include <bits/stdc++.h> using namespace std; // Function to print the desired // result after computation void isHalfReducible(int arr[], int n, int m) { int frequencyHash[m + 1]; int i; memset(frequencyHash, 0, sizeof(frequencyHash)); for (i = 0; i < n; i++) { frequencyHash[arr[i] % (m + 1)]++; } for (i = 0; i <= m; i++) { if (frequencyHash[i] >= n / 2) break; } if (i <= m) cout << "Yes" << endl; else cout << "No" << endl; } // Driver Code int main() { int arr[] = { 8, 16, 32, 3, 12 }; int n = sizeof(arr) / sizeof(arr[0]); int m = 7; isHalfReducible(arr, n, m); return 0; } |
Java
// Java Program to find whether half-array // reducible to 0 public class GFG { // Function to print the desired // result after computation static void isHalfReducible(int arr[], int n, int m) { int frequencyHash[] = new int[m + 1]; int i; for(i = 0 ; i < frequencyHash.length ; i++) frequencyHash[i] = 0 ; for (i = 0; i < n; i++) { frequencyHash[arr[i] % (m + 1)]++; } for (i = 0; i <= m; i++) { if (frequencyHash[i] >= n / 2) break; } if (i <= m) System.out.println("Yes") ; else System.out.println("No") ; } // Driver code public static void main(String args[]) { int arr[] = { 8, 16, 32, 3, 12 }; int n = arr.length ; int m = 7; isHalfReducible(arr, n, m); } // This code is contributed by ANKITRAI1 } |
Python3
# Python3 program to find whether # half-array reducible to 0 # Function to print the desired # result after computation def isHalfReducible(arr, n, m): frequencyHash =[0]*(m + 1); i = 0; while(i < n): frequencyHash[(arr[i] % (m + 1))] += 1; i += 1; i = 0; while(i <= m): if(frequencyHash[i] >= (n / 2)): break; i += 1; if (i <= m): print("Yes"); else: print("No"); # Driver Code arr = [ 8, 16, 32, 3, 12 ]; n = len(arr); m = 7; isHalfReducible(arr, n, m); # This code is contributed by mits |
C#
// C# Program to find whether half-array // reducible to 0 using System; public class GFG { // Function to print the desired // result after computation static void isHalfReducible(int[] arr, int n, int m) { int[] frequencyHash = new int[m + 1]; int i; for(i = 0 ; i < frequencyHash.Length ; i++) frequencyHash[i] = 0 ; for (i = 0; i < n; i++) { frequencyHash[arr[i] % (m + 1)]++; } for (i = 0; i <= m; i++) { if (frequencyHash[i] >= n / 2) break; } if (i <= m) Console.WriteLine("Yes") ; else Console.WriteLine("No") ; } // Driver code public static void Main() { int[] arr = { 8, 16, 32, 3, 12 }; int n = arr.Length ; int m = 7; isHalfReducible(arr, n, m); } // This code is contributed by Subhadeep } |
PHP
<?php // PHP program to find whether // half-array reducible to 0 // Function to print the desired // result after computation function isHalfReducible($arr, $n, $m) { $frequencyHash = array_fill(0, $m + 1, 0); $i = 0; for (;$i < $n; $i++) { $frequencyHash[($arr[$i] % ($m + 1))]++; } for ($i = 0; $i <= $m; $i++) { if ($frequencyHash[$i] >= ($n / 2)) break; } if ($i <= $m) echo "Yes\n"; else echo "No\n"; } // Driver Code $arr = array( 8, 16, 32, 3, 12 ); $n = sizeof($arr); $m = 7; isHalfReducible($arr, $n, $m); // This code is contributed by mits ?> |
Yes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the sum of the first half and second half elements of an array
- 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
- Reorder an array such that sum of left half is not equal to sum of right half
- Check if an array has some palindromic subsequence of length at least 3
- Sort first half in ascending and second half in descending order | 1
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Sort first half in ascending and second half in descending order | Set 2
- Count number of rotated strings which have more number of vowels in the first half than second half
- Reduce every element of the array to it's half retaining the sum zero
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Find the index which is the last to be reduced to zero after performing a given operation
- First subarray having sum at least half the maximum sum of any subarray of size K
- Maximum Possible Product in Array after performing given Operations
- Minimum element left from the array after performing given operations
- Maximum count of equal numbers in an array after performing given operations
- Maximum sum of all elements of array after performing given operations
- Number of array elements derivable from D after performing certain operations
- Find the modified array after performing k operations of given type
- Number of elements from the array which are reachable after performing given operations on D
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.

