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:

  1. Either both element got decreased by 1 and hence resulting no change in their actual difference.
  2. Both element got increased by m and hence resulting no change in their actual difference again.
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.