Check if at least half array is reducible to zero by performing some operations
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
Recommended Posts:
- Maximum sum of all elements of array after performing given operations
- Maximum Possible Product in Array after performing given Operations
- Minimum element left from the array after performing given operations
- Number of elements from the array which are reachable after performing given operations on D
- Number of array elements derivable from D after performing certain operations
- Find the final sequence of the array after performing given operations
- Find the modified array after performing k operations of given type
- Maximum count of equal numbers in an array after performing given operations
- Check if the last element of array is even or odd after performing a operation p times
- Find prime numbers in the first half and second half of an array
- Find the sum of the first half and second half elements of an array
- Check if array sum can be made K by three operations on it
- Check if the frequency of any character is more than half the length of the string
- Sort first half in ascending and second half in descending order | Set 2
- Sort first half in ascending and second half in descending order | 1
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.



