Count the numbers that can be reduced to zero or less in a game
Last Updated :
10 Apr, 2023
Given two integers X and Y and an array of N integers. Player A can decrease any element of the array by X and Player B can increase any element of the array by Y. The task is to count the number of elements that A can reduce to 0 or less. They both play optimally for an infinite time with A making the first move.
Note: A number once reduced to zero or less cannot be increased.
Examples:
Input: a[] = {1, 2, 4, 2, 3}, X = 3, Y = 3
Output: 2
A reduces 2 to -1
B increases 1 to 4
A reduces 2 to -1
B increases 4 to 7 and the game goes on.
Input: a[] = {1, 2, 4, 2, 3}, X = 3, Y = 2
Output: 5
Approach: Since the game goes on for an infinite time, we print N if X > Y. Now we need to solve for X ? Y. The numbers can be of two types:
- Those who do not exceed X on adding Y say count1 which can be reduced to ? 0 by A.
- Those who are < X and exceed X on adding Y say count2 only half of which can be reduced to ? 0 by A as they are playing optimally and B will try to increase any one of those numbers so that it becomes > X in each one of his turns.
So, the answer will be count1 + ((count2 + 1) / 2).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int a[], int n, int x, int y)
{
if (y < x)
return n;
int count1 = 0, count2 = 0;
for ( int i = 0; i < n; i++) {
if (a[i] + y <= x)
count1++;
else if (a[i] <= x)
count2++;
}
int number = (count2 + 1) / 2 + count1;
return number;
}
int main()
{
int a[] = { 1, 2, 4, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
int x = 3, y = 3;
cout << countNumbers(a, n, x, y);
return 0;
}
|
Java
class GFG
{
static int countNumbers( int a[], int n, int x, int y)
{
if (y < x)
return n;
int count1 = 0 , count2 = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] + y <= x)
count1++;
else if (a[i] <= x)
count2++;
}
int number = (count2 + 1 ) / 2 + count1;
return number;
}
public static void main(String []args)
{
int a[] = { 1 , 2 , 4 , 2 , 3 };
int n = a.length;
int x = 3 , y = 3 ;
System.out.println(countNumbers(a, n, x, y));
}
}
|
Python3
def countNumbers( a, n, x, y):
if (y < x):
return n
count1 = 0
count2 = 0
for i in range ( 0 , n):
if (a[i] + y < = x):
count1 = count1 + 1
elif (a[i] < = x):
count2 = count2 + 1
number = (count2 + 1 ) / / 2 + count1
return number
a = [ 1 , 2 , 4 , 2 , 3 ]
n = len (a)
x = 3
y = 3
print (countNumbers(a, n, x, y))
|
C#
using System;
class GFG
{
static int countNumbers( int []a, int n, int x, int y)
{
if (y < x)
return n;
int count1 = 0, count2 = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] + y <= x)
count1++;
else if (a[i] <= x)
count2++;
}
int number = (count2 + 1) / 2 + count1;
return number;
}
public static void Main()
{
int [] a = { 1, 2, 4, 2, 3 };
int n = a.Length;
int x = 3, y = 3;
Console.WriteLine(countNumbers(a, n, x, y));
}
}
|
PHP
<?php
function countNumbers( $a , $n , $x , $y )
{
if ( $y < $x )
return $n ;
$count1 = 0 ;
$count2 = 0 ;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] + $y <= $x )
$count1 ++;
else if ( $a [ $i ] <= $x )
$count2 ++;
}
$number = floor (( $count2 + 1) / 2) + $count1 ;
return $number ;
}
$a = array ( 1, 2, 4, 2, 3 );
$n = sizeof( $a );
$x = 3;
$y = 3;
echo countNumbers( $a , $n , $x , $y );
?>
|
Javascript
<script>
function countNumbers(a, n, x, y)
{
if (y < x)
return n;
var i;
var count1 = 0, count2 = 0;
for (i = 0; i < n; i++) {
if (a[i] + y <= x)
count1++;
else if (a[i] <= x)
count2++;
}
var number = (count2 + 1) / 2 + count1;
return number;
}
var a = [1, 2, 4, 2, 3];
var n = a.length;
var x = 3, y = 3;
document.write(parseInt(countNumbers(a, n, x, y)));
</script>
|
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of elements in the array.
- Auxiliary Space: O(1), as we are not using any extra space.
New Approach:- We can simulate the game by repeatedly adding Y to the numbers in the array and checking which numbers become negative or zero. If a number becomes negative or zero, we remove it from the array. We repeat this process until we cannot remove any more numbers from the array.
The count of the numbers that can be reduced to zero or less is the size of the array at the end of the process.
Steps:-
- Define a function countNumbers that takes an array a of integers, its size n, and two integers x and y as input parameters. The function returns an integer that represents the count of numbers that can be reduced to zero or less in a game.
- Simulate the game by using a while loop that repeats until there are no numbers that can be removed from the array.
- Inside the loop, use a boolean variable removed to keep track of whether any numbers have been removed from the array in the current iteration. Initialize removed to false.
- Iterate over all the numbers in the array a using a for loop. For each number, add y to it.
- If the resulting value of the number is less than or equal to zero, swap it with the last element of the array and decrement the size of the array n by 1. This removes the number from the array.
- If any numbers have been removed from the array in the current iteration, set removed to true.
- Check if no numbers have been removed from the array in the current iteration. If so, break out of the while loop.
- Return the final value of n as the count of numbers that can be reduced to zero or less in the game.
- In the main function, define an array a of integers and initialize it with some values. Also, define integers x and y that represent the rules of the game.
- Call the countNumbers function with the input parameters and print its output to the console.
Here is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int a[], int n, int x, int y)
{
while ( true ) {
bool removed = false ;
for ( int i = 0; i < n; i++) {
a[i] += y;
if (a[i] <= 0) {
swap(a[i], a[n - 1]);
n--;
removed = true ;
}
}
if (!removed)
break ;
}
return n;
}
int main()
{
int a[] = { 1, 2, 4, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
int x = 3, y = 3;
cout << countNumbers(a, n, x, y);
return 0;
}
|
C#
using System;
class Program
{
static int countNumbers( int [] a, int n, int x, int y)
{
while ( true )
{
bool removed = false ;
for ( int i = 0; i < n; i++)
{
a[i] += y;
if (a[i] <= 0)
{
int temp = a[i];
a[i] = a[n - 1];
a[n - 1] = temp;
n--;
removed = true ;
}
}
if (!removed)
break ;
}
return n;
}
static void Main( string [] args)
{
int [] a = { 1, 2, 4, 2, 3 };
int n = a.Length;
int x = 3, y = 3;
Console.WriteLine(countNumbers(a, n, x, y));
}
}
|
Java
import java.util.Arrays;
public class GameNumbers {
public static int countNumbers( int [] a, int n, int x, int y) {
while ( true ) {
boolean removed = false ;
for ( int i = 0 ; i < n; i++) {
a[i] += y;
if (a[i] <= 0 ) {
int temp = a[i];
a[i] = a[n - 1 ];
a[n - 1 ] = temp;
n--;
removed = true ;
}
}
if (!removed)
break ;
}
return n;
}
public static void main(String[] args) {
int [] a = { 1 , 2 , 4 , 2 , 3 };
int n = a.length;
int x = 3 , y = 3 ;
System.out.println(countNumbers(a, n, x, y));
}
}
|
Python3
def countNumbers(a, n, x, y):
while True :
removed = False
for i in range (n):
a[i] + = y
if a[i] < = 0 :
a[i], a[n - 1 ] = a[n - 1 ], a[i]
n - = 1
removed = True
if not removed:
break
return n
a = [ 1 , 2 , 4 , 2 , 3 ]
n = len (a)
x, y = 3 , 3
print (countNumbers(a, n, x, y))
|
Javascript
function countNumbers(a, n, x, y) {
while ( true ) {
let removed = false ;
for (let i = 0; i < n; i++) {
a[i] += y;
if (a[i] <= 0) {
[a[i], a[n - 1]] = [a[n - 1], a[i]];
n--;
removed = true ;
}
}
if (!removed) {
break ;
}
}
return n;
}
const a = [1, 2, 4, 2, 3];
const n = a.length;
const x = 3, y = 3;
console.log(countNumbers(a, n, x, y));
|
Output
5
Time complexity:
The while loop will run at most n times, where n is the size of the input array. In each iteration, we iterate over the entire array to add y to each element and remove the ones that become negative or zero. Therefore, the time complexity of the function is O(n^2).
Auxiliary space:
The function uses a constant amount of extra space, regardless of the size of the input. Therefore, the auxiliary space complexity is O(1).
“Note that this approach has a worst-case time complexity of O(n^2), where n is the size of the array. This is because we may need to remove all the numbers in the array, and each removal operation takes O(n) time. However, in practice, the time complexity is likely to be much lower, since we are removing numbers from the array in each iteration.”
Share your thoughts in the comments
Please Login to comment...