There are two cash lockers, one has **X** number of coins and the other has **Y** number of coins, you can withdraw money at max two times, when you withdraw from a locker you will get the total money of the locker and the locker will be refilled with **Z – 1** coins if it had **Z** coins initially. The task is to find the maximum coins you can get.

**Examples:**

Input:X = 6, Y = 3

Output:11

Take from locker X i.e. 6

Now, X = 5 and Y = 3

Take again from locker X i.e. 5.

Input:X = 4, Y = 4

Output:8

**Approach:** In order to maximize the number of coins, take from the locker which has the maximum value then update the locker and again take from the locker with the maximum value.

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 maximum coins we can get ` `int` `maxCoins(` `int` `X, ` `int` `Y) ` `{ ` ` ` ` ` `// Update elements such that X > Y ` ` ` `if` `(X < Y) ` ` ` `swap(X, Y); ` ` ` ` ` `// Take from the maximum ` ` ` `int` `coins = X; ` ` ` ` ` `// Refill ` ` ` `X--; ` ` ` ` ` `// Again, take the maximum ` ` ` `coins += max(X, Y); ` ` ` ` ` `return` `coins; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `X = 7, Y = 5; ` ` ` ` ` `cout << maxCoins(X, Y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` `// Function to return the maximum coins we can get ` ` ` `static` `int` `maxCoins(` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` ` ` `// Update elements such that X > Y ` ` ` `if` `(X < Y) { ` ` ` `swap(X, Y); ` ` ` `} ` ` ` ` ` `// Take from the maximum ` ` ` `int` `coins = X; ` ` ` ` ` `// Refill ` ` ` `X--; ` ` ` ` ` `// Again, take the maximum ` ` ` `coins += Math.max(X, Y); ` ` ` ` ` `return` `coins; ` ` ` `} ` ` ` ` ` `static` `void` `swap(` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` `int` `temp = X; ` ` ` `X = Y; ` ` ` `Y = temp; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `X = ` `7` `, Y = ` `5` `; ` ` ` `System.out.println(maxCoins(X, Y)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum coins we can get ` `def` `maxCoins(X, Y) : ` ` ` ` ` `# Update elements such that X > Y ` ` ` `if` `(X < Y) : ` ` ` `X, Y ` `=` `Y, X; ` ` ` ` ` `# Take from the maximum ` ` ` `coins ` `=` `X; ` ` ` ` ` `# Refill ` ` ` `X ` `-` `=` `1` `; ` ` ` ` ` `# Again, take the maximum ` ` ` `coins ` `+` `=` `max` `(X, Y); ` ` ` ` ` `return` `coins; ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `X ` `=` `7` `; Y ` `=` `5` `; ` ` ` ` ` `print` `(maxCoins(X, Y)); ` ` ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function to return the maximum coins we can get ` ` ` `static` `int` `maxCoins(` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` ` ` `// Update elements such that X > Y ` ` ` `if` `(X < Y) { ` ` ` `swap(X, Y); ` ` ` `} ` ` ` ` ` `// Take from the maximum ` ` ` `int` `coins = X; ` ` ` ` ` `// Refill ` ` ` `X--; ` ` ` ` ` `// Again, take the maximum ` ` ` `coins += Math.Max(X, Y); ` ` ` ` ` `return` `coins; ` ` ` `} ` ` ` ` ` `static` `void` `swap(` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` `int` `temp = X; ` ` ` `X = Y; ` ` ` `Y = temp; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `X = 7, Y = 5; ` ` ` `Console.WriteLine(maxCoins(X, Y)); ` ` ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the maximum coins we can get ` `function` `maxCoins(` `$X` `, ` `$Y` `) ` `{ ` ` ` `// Update elements such that X > Y ` ` ` `if` `(` `$X` `< ` `$Y` `) ` ` ` `swap(` `$X` `, ` `$Y` `); ` ` ` ` ` `// Take from the maximum ` ` ` `$coins` `= ` `$X` `; ` ` ` ` ` `// Refill ` ` ` `$X` `--; ` ` ` ` ` `// Again, take the maximum ` ` ` `$coins` `+= max(` `$X` `, ` `$Y` `); ` ` ` ` ` `return` `$coins` `; ` `} ` ` ` `// Driver code ` ` ` `$X` `= 7; ` `$Y` `= 5; ` ` ` `echo` `maxCoins(` `$X` `, ` `$Y` `); ` ` ` `// This code is contributed by Naman_Garg. ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

13

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:

- Select numbers in such way to maximize the amount of money
- Total money to be paid after traveling the given number of hours
- Minimize Cash Flow among a given set of friends who have borrowed money from each other
- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Largest number N which can be reduced to 0 in K steps
- Find the number of stair steps
- Find the Alpha Score of the Given Steps (Using BST)
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Number of steps to convert to prime factors
- Probability of reaching a point with 2 or 3 steps at a time
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Minimum steps to color the tree with given colors
- Count minimum steps to get the given desired array
- Generate array with minimum sum which can be deleted in P steps
- Find the minimum number of steps to reach M from N
- Count the minimum steps to reach 0 from the given integer N
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way

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.