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

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

**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.