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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.



Improved By : ihritik, AnkitRai01



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.