Home > Backend Development > C++ > In C, the maximum amount that can be withdrawn in two steps

In C, the maximum amount that can be withdrawn in two steps

WBOY
Release: 2023-09-22 08:05:17
forward
1415 people have browsed it

In C, the maximum amount that can be withdrawn in two steps

We have two lockers, called L1 and L2, which contain some coins. L1 has A coins and L2 has B coins. We must remove coins from the locker to maximize the amount withdrawn. Each time a coin is removed from any locker, it is replaced with the previous amount minus 1 coin. If we take A coins from L1, then it will be replaced by A-1 coins, and if we take B coins from L2, then it will be replaced by B-1 coins. The task is to maximize the amount withdrawn in two steps. This means that the coin can only be withdrawn twice.

Input - L1 - 10, L2 - 11

Output - Maximum amount that can be withdrawn in two steps - 21

Explanation - In the first step, we take 11 coins from L2, L2 will be replaced by 11-1=10 coins.

In the second step, both L1 and L2 have 10 coins, so it can be taken from either one, and we have 11 10 = 21 coins, which is the maximum.

Input - L1-5, L2-5

Output - Maximum amount that can be withdrawn in two steps - 10

Explanation - In the first step, we take 5 coins from L1, L1 will be replaced by 5-1=4 coins.

In the second step, L1 has 4 coins and L2 has 5 coins, so we take 5 coins from L2 and we have 5 5 = 10 coins, which is the maximum.

The method used by the following program is as follows

  • We have two integer lockers L1 and L2, which contain some coins.

  • The function maxMoney(int A, int B) takes the number of coins in the locker as input.

  • In the maxMoney() function, we use the variable 'money' to store the maximum amount.

  • Initially, the value of money comes from the larger of A or B. (money=A>B?A:B)

  • Compare the value of money with A or B to determine which container's coin was removed.

  • Now replace the container with the previous amount minus 1 coin. (A-- or B--)

  • Add the value of money to the larger of A or B again. (money =A>B?A:B)

    If k is small, the sum of the smallest k elements is the smallest -
  • Storage abs((sum of the entire array) in D1 - (twice the sum of the smallest k elements)). Twice because the array sum also contains these elements.

    If k is larger, the sum of the largest k elements is the largest -

  • Store abs ((sum of the entire array) - (the largest k twice the sum of elements)). Twice because the array sum also contains these elements.

  • Compare D1 with D2 and store the maximum value in maxD.

  • Return maxD as the result.

Example

Live Demonstration

Code:
#include <stdio.h>
#include <math.h>
// Function to return the maximum coins we can get
int maxMoney(int A, int B){
   //take coins
   int money=A>B?A:B;
   //refill the lockers with 1 less no.of coins
   if(money==A)
      A--;
   else
      B--;
   //withdraw again
   money+=A>B?A:B;
   return money;
}
// Driver code
int main(){
   int L1 = 8, L2 = 9;
   printf("Maximum money that can be withdrawn in two steps: %d" , maxMoney(L1, L2));
   return 0;
}
Copy after login

Output

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

Maximum money that can be withdrawn in two steps: 17
Copy after login

The above is the detailed content of In C, the maximum amount that can be withdrawn in two steps. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template