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