Open In App

Count the numbers that can be reduced to zero or less in a game

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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: 

  1. Those who do not exceed X on adding Y say count1 which can be reduced to ? 0 by A.
  2. 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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of numbers
int countNumbers(int a[], int n, int x, int y)
{
 
    // Base case
    if (y < x)
        return n;
 
    // Count the numbers
    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;
}
 
// Driver Code
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




// Java implementation of the approach
 
class GFG
{
    // Function to return the count of numbers
    static int countNumbers(int a[], int n, int x, int y)
    {
     
        // Base case
        if (y < x)
            return n;
     
        // Count the numbers
        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;
    }
     
    // Driver Code
    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));   
    }
}
 
// This code is contributed by ihritik


Python3




# Python3 implementation of the approach
 
# Function to return the count of numbers
def countNumbers( a,  n, x, y):
 
 
    # Base case
    if (y < x):
        return n
 
    # Count the numbers
    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
 
 
# Driver Code
a = [ 1, 2, 4, 2, 3 ]
n = len(a)
 
x = 3
y = 3
print(countNumbers(a, n, x, y))
 
# This code is contributed by ihritik


C#




// C# implementation of the approach
using System;
 
class GFG
{
    // Function to return the count of numbers
    static int countNumbers(int []a, int n, int x, int y)
    {
     
        // Base case
        if (y < x)
            return n;
     
        // Count the numbers
        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;
    }
     
    // Driver Code
    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));
    }
}
 
// This code is contributed by ihritik


PHP




<?php
// PHP implementation of the approach
 
// Function to return the count of numbers
function countNumbers($a, $n, $x, $y)
{
 
    // Base case
    if ($y < $x)
        return $n;
 
    // Count the numbers
    $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;
}
 
// Driver Code
$a = array( 1, 2, 4, 2, 3 );
$n = sizeof($a);
 
$x = 3;
$y = 3;
 
echo countNumbers($a, $n, $x, $y);
 
// This code is contributed by Ryuga
?>


Javascript




<script>
 
// javascript implementation of the approach
 
// Function to return the count of numbers
function countNumbers(a, n, x, y)
{
 
    // Base case
    if (y < x)
        return n;
     
    var i;
    // Count the numbers
    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;
}
 
// Driver Code
    var a = [1, 2, 4, 2, 3];
    var n = a.length;
 
    var x = 3, y = 3;
    document.write(parseInt(countNumbers(a, n, x, y)));
 
// This code is contributed by ipg2016107.
</script>


Output

2

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




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of numbers
int countNumbers(int a[], int n, int x, int y)
{
    // Simulate the game
    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;
}
 
// Driver Code
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)
    {
        // Simulate the game
        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) {
        // Simulate the game
        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):
    # Simulate the game
    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
 
# Driver Code
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) {
    // Simulate the game
    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.”



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads