There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.

Example 1:

Input: numBottles = 9, numExchange = 3 Output: 13 Explanation: You can exchange 3 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 9 + 3 + 1 = 13.

Example 2:

Input: numBottles = 15, numExchange = 4 Output: 19 Explanation: You can exchange 4 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 15 + 3 + 1 = 19.

Constraints:

  • 1 <= numBottles <= 100
  • 2 <= numExchange <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: numBottles, numExchange, maxWaterBottles

    numBottles = 9
    numExchange = 3
    write (*, '(A, I0)') 'Example 1: ', maxWaterBottles(numBottles, numExchange)

    numBottles = 15
    numExchange = 4
    write (*, '(A, I0)') 'Example 2: ', maxWaterBottles(numBottles, numExchange)

contains

    function maxWaterBottles(n, m) result(res)
        implicit none
        integer, value :: n, m
        integer :: res

        res = n + m - 1
    end function maxWaterBottles
end program main
Compiled
Executed
Correct
module WaterBottles
    implicit none

contains

    function maxWaterBottles(numBottles, numExchange) result(maxBottles)
        integer, intent(in) :: numBottles
        integer, intent(in) :: numExchange
        integer :: maxBottles
        integer :: fullBottles
        integer :: emptyBottles

        fullBottles = numBottles / numExchange
        emptyBottles = numBottles - (fullBottles * numExchange)

        maxBottles = fullBottles + emptyBottles
    end function maxWaterBottles
end module WaterBottles

program main
    use WaterBottles
    implicit none

    integer :: numBottles
    integer :: numExchange
    integer :: maxBottles

    ! Example 1
    numBottles = 9
    numExchange = 3
    maxBottles = maxWaterBottles(numBottles, numExchange)
    write (*,*) "Example 1:", maxBottles

    ! Example 2
    numBottles = 15
    numExchange = 4
    maxBottles = maxWaterBottles(numBottles, numExchange)
    write (*,*) "Example 2:", maxBottles
end program main
🌐 Data from online sources
def numWaterBottles(numBottles: int, numExchange: int) -> int:
    totalBottles = numBottles
    while numBottles >= numExchange:
        newBottles = numBottles // numExchange
        totalBottles += newBottles
        numBottles = newBottles + numBottles % numExchange
    return totalBottles

The algorithm initializes the totalBottles variable with the value of numBottles. It then uses a while loop to repeatedly calculate the number of new full bottles the person can get by exchanging the current number of empty bottles.

In each iteration, the loop calculates the new number of full bottles by dividing numBottles by numExchange and flooring the result. It then adds these new bottles to the totalBottles variable. The loop also updates the number of empty bottles by adding the new full bottles and the remainder of numBottles divided by numExchange.

Once the number of empty bottles is less than the required numExchange, the loop terminates, and the function returns the value of totalBottles as the maximum number of water bottles the person can drink.

🌐 Data from online sources
int numWaterBottles(int numBottles, int numExchange) {
    int totalBottles = numBottles;
    while (numBottles >= numExchange) {
        int newBottles = numBottles / numExchange;
        totalBottles += newBottles;
        numBottles = newBottles + numBottles % numExchange;
    }
    return totalBottles;
}

The algorithm initializes the totalBottles variable with the value of numBottles. It then uses a while loop to repeatedly calculate the number of new full bottles the person can get by exchanging the current number of empty bottles.

In each iteration, the loop calculates the new number of full bottles by dividing numBottles by numExchange and flooring the result. It then adds these new bottles to the totalBottles variable. The loop also updates the number of empty bottles by adding the new full bottles and the remainder of numBottles divided by numExchange.

Once the number of empty bottles is less than the required numExchange, the loop terminates, and the function returns the value of totalBottles as the maximum number of water bottles the person can drink.