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

C++Server Side ProgrammingProgramming

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

 Live Demo

#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
raja
Updated on 05-Jan-2021 06:23:32

Advertisements