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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game of N stones where each player can remove 1, 3 or 4
- Two player game in which a player can remove all occurrences of a number
- 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
- Optimal Strategy for a Game | DP-31
- Combinatorial Game Theory | Set 1 (Introduction)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
- Minimax Algorithm in Game Theory | Set 1 (Introduction)
- Minimax Algorithm in Game Theory | Set 2 (Introduction to Evaluation Function)
- Implementation of Tic-Tac-Toe game
- Minimax Algorithm in Game Theory | Set 4 (Alpha-Beta Pruning)
- Minimax Algorithm in Game Theory | Set 5 (Zobrist Hashing)

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.