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

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

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

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Combinatorial Game Theory | Set 3 (Grundy Numbers/Nimbers and Mex)
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Find the index which is the last to be reduced to zero after performing a given operation
- Convert an array to reduced form | Set 2 (Using vector of pairs)
- Count numbers from 1 to n that have 4 as a digit
- Count Triplets such that one of the numbers can be written as sum of the other two
- Count subsets having distinct even numbers
- Count of numbers which can be made power of 2 by given operation
- Count the numbers < N which have equal number of divisors as K
- Array Index with same count of even or odd numbers on both sides
- Count elements which divide all numbers in range L-R
- Count numbers formed by given two digit with sum having given digits
- Count different numbers possible using all the digits their frequency times
- Find the count of maximum contiguous Even numbers

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.