- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count the numbers that can be reduced to zero or less in a gamein C++

Given an array of positive numbers and two integers A and B. Two players are playing a game in which they will reduce numbers in the array. Player 1 can decrease any element of the array by A and player 2 can increase any element of the array by B. The goal is to find the count of numbers that can be reduced to 0 or less by player 1. The first player makes the first move. The number once reduced to 0 or less can’t be taken into consideration by player 2.

**For Example**

## Input

arr[] = { 1,4,5,2 } A=2, B=3

## Output

Count of numbers that can be reduced to zero or less in a game are: 1

## Explanation

The only number that can be reduced by player 1 is 1 as on first move it will be reduced to −1. Rest all will become greater than A after player 2 increases their value.

## Input

arr[] = { 1,4,5,2 } A=4, B=4

## Output

Count of numbers that can be reduced to zero or less in a game are: 2

## Explanation

On first move player 1 reduces 4 to 0. arr[]= [ 1, 0, 5, 2 ] Player 2 will increase 1 to 4. arr[]= [ 5, 0, 5, 2 ] Player 1 will decrease 2 to −2. Arr[] = [ 5, 0, 5, −2 ]. From now onwards all numbers are greater than A so cannot be reduced by player 1 to 0 or less as player 2 is also increasing them simultaneously.

**Approach used in the below program is as follows** −

In this approach first check if A>B. If yes then in N moves A will reduce all of N elements of arr[] to 0 or less.If A<=B then we will check

all numbers which do not become greater than A even after player 2 adds B to them. Let's say this count as C1.

All numbers that are less than A and become greater than A after player 2 adds B to them. Let's say this count as C2.

Total count will be C= C1+ (C2+1)/2. As in case 2, only half of those will reduce to 0 or less as both players simultaneously increase/decrease them. And player 2 can only increase half of them to more than A. In the meantime, player 1 will reduce the other half to <=0.

Take an integer array containing positive numbers.

Take two variables A and B.

Function reduced_zero(int arr[], int size, int A, int B) will return the count of count the numbers that can be reduced to zero or less in a game

Take the initial count as 0.

Take two variables temp_1 and temp_2 as temporary counts.

If A > B then return the length of the array which is size.

Now traverse array using for loop, for each arr[i] if sum of element and B < A then increment temp_1.

For each element arr[i] <=A, increment temp_2.

Now after the end of a for loop, take count=temp_1+ (temp_2+1)/2. As given in formula.

Return count as result.

## Example

#include <bits/stdc++.h> using namespace std; int reduced_zero(int arr[], int size, int A, int B){ int count = 0; int temp_1 = 0, temp_2 = 0; if (A > B){ return size; } for(int i = 0; i < size; i++){ if (A >= arr[i] + B){ temp_1++; } else if(A >= arr[i]){ temp_2++; } } int temp = (temp_2 + 1) / 2; count = temp + temp_1; return count; } int main(){ int arr[] = { 3, 3, 1, 2, 4, 7, 1}; int A = 4, B = 1; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of numbers that can be reduced to zero or less in a game are: "<<reduced_zero(arr, size, A, B); return 0; }

## Output

If we run the above code it will generate the following output −

Count of numbers that can be reduced to zero or less in a game are: 7

- Related Articles
- How can reverberations in a big hall or auditorium be reduced?
- If the product of two whole numbers is zero, can we say that one or both of them will be zero? Justify through examples.
- C++ Program to check string can be reduced to 2022 or not
- Is it true that displacement of a body can be positive or zero, but never negative?
- Find the index which is the last to be reduced to zero after performing a given operation in Python
- Count numbers which can be constructed using two numbers in C++
- Check if a number from every row can be selected such that xor of the numbers is greater than zero in C++
- Count Triplets such that one of the numbers can be written as sum of the other two in C++
- State true or false: Displacement can be zero but distance never.""
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- How to get the largest of zero or more numbers in JavaScript?
- How to get the smallest of zero or more numbers in JavaScript?
- What is reverberation? How can it be reduced?
- How to find numbers in an array that are greater than, less than, or equal to a value in java?