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++
#include <bits/stdc++.h>
using namespace std;
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;
}
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
public class GFG {
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" ) ;
}
public static void main(String args[])
{
int arr[] = { 8 , 16 , 32 , 3 , 12 };
int n = arr.length ;
int m = 7 ;
isHalfReducible(arr, n, m);
}
}
|
Python3
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" );
arr = [ 8 , 16 , 32 , 3 , 12 ];
n = len (arr);
m = 7 ;
isHalfReducible(arr, n, m);
|
C#
using System;
public class GFG {
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" ) ;
}
public static void Main()
{
int [] arr = { 8, 16, 32, 3, 12 };
int n = arr.Length ;
int m = 7;
isHalfReducible(arr, n, m);
}
}
|
PHP
<?php
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" ;
}
$arr = array ( 8, 16, 32, 3, 12 );
$n = sizeof( $arr );
$m = 7;
isHalfReducible( $arr , $n , $m );
?>
|
Javascript
<script>
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" );
}
var arr = [ 8, 16, 32, 3, 12 ];
var n = arr.length;
var m = 7;
isHalfReducible(arr, n, m);
</script>
|
Complexity Analysis:
Time Complexity : O(n+m), since there runs two loops, one for n times and the other for m times.
Auxiliary Complexity: O(m), since m extra space has been taken.
Last Updated :
06 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...