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` `?>` |

## Javascript

`<script>` `// javascript program to find whether half-array` `// reducible to 0` `// Function to print the desired` `// result after computation` `function` `isHalfReducible(arr, n, m)` `{` ` ` `var` `frequencyHash = Array(m+1).fill(0);` ` ` `var` `i;` ` ` `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)` ` ` `document.write( ` `"Yes"` `);` ` ` `else` ` ` `document.write( ` `"No"` `);` `}` `// Driver Code` `var` `arr = [ 8, 16, 32, 3, 12 ];` `var` `n = arr.length;` `var` `m = 7;` `isHalfReducible(arr, n, m);` `</script>` |

**Output:**

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **DSA Live Classes**