Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIMaximum money that can be withdrawn in two steps

Maximum money that can be withdrawn in two steps

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 coin 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:
 

 

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;
}


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


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


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 */


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.
 
?>


Javascript




<script>
 
// Javascript 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) {
        let temp = X;
        X = Y;
        Y = temp;
    }
 
    // Take from the maximum
    let coins = X;
 
    // Refill
    X--;
 
    // Again, take the maximum
    coins += Math.max(X, Y);
 
    return coins;
}
 
// Driver code
 
    let X = 7, Y = 5;
 
    document.write(maxCoins(X, Y));
 
</script>


Output: 

13

 

Time Complexity: O(1), as there is no loop.

Auxiliary Space: O(1), as no extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments