A man starts from his house with a few cakes. let them be N. Now he visits K places before reaching home. At each place, he can buy a cake, sell a cake or do nothing. But he must sell L cakes before reaching home. Find the maximum number of cakes he can have at any point in his journey. N, K, and L are given as input which is the cakes he has before starting, the number of places visited, and the number of cakes he has to sell respectively.
Examples:
Input: N = 5, K = 3, L = 1
Output: 7
Explanation: First we will buy 2 cakes to make a total of 7 then we will sell 1 cake.Input: N = 1, K = 4, L = 3
Output: -1
Explanation: There is no way we will have a cake to sell at last place.
Approach: To solve the problem follow the below observations:
Observations:
- We have K places to visit and at L places we have to sell, for getting the maximum number of cakes at a time we will first buy cakes to reach the maximum, and then at last L places, we will sell the cakes.
- In order to do this we have to make sure that at the particular time, we will have a cake when we have to sell. For this, we will find the number of places we can buy cakes which is BuyCakes = K-L (places to visit – places to sell).
- Then we will check if the sum of N and BuyCakes is less than L, then return -1.
Below is the implementation of the above idea.
C++
// C++ code for the above approach: #include <iostream>; using namespace std; // Function to calculate the maximum // number of cakes int maxCakes( int N, int K, int L) { // Calculate the number of cakes we can buy int BuyCakes = K - L; // Check the availability of cakes when // we have to sell if (N + BuyCakes < L) { // Return -1 if we have a shortage of // cakes when we have to sell return -1; } else { // Return the maximum number of cakes // we will have at a place return BuyCakes + N; } } // Drivers code int main() { int N = 5; int K = 3; int L = 1; // Call the maxCakes function and // print the result cout << maxCakes(N, K, L) << endl; return 0; } |
Java
// Java code for the above approach class GFG { // function to calculate maximum number of cakes static int maxCakes( int N, int K, int L) { // calculate the number of cakes we can buy int BuyCakes = K - L; // check the availability of cakes // when we have to sell if (N + BuyCakes < L) { // return -1 if we have shortage of cakes // when we have to sell return - 1 ; } else { // return the maximum number of cakes // we will have at a place return BuyCakes + N; } } // Driver code public static void main(String[] args) { int N = 5 ; int K = 3 ; int L = 1 ; // call the maxCakes function and print the result System.out.println(maxCakes(N, K, L)); } } // This code is contributed by ragul21 |
Python3
# Python3 implementation of above approach def maxCakes(N, K, L): # number of cakes we can buy BuyCakes = K - L # checking the availability of cake when we have to sell if N + BuyCakes & lt L: # return -1 if we have sortage of cake when we have to sell return ( - 1 ) else : # returning max number of cakes we will have at a place return (BuyCakes + N) # Driver Code N = 5 K = 3 L = 1 # Call the maxCakes function and print the result print (maxCakes(N, K, L)) |
C#
// C# code for the above approach using System; class GFG { // function to calculate maximum number of cakes static int maxCakes( int N, int K, int L) { // calculate the number of cakes we can buy int BuyCakes = K - L; // check the availability of cakes // when we have to sell if (N + BuyCakes < L) { // return -1 if we have shortage of cakes // when we have to sell return -1; } else { // return the maximum number of cakes // we will have at a place return BuyCakes + N; } } // Driver code public static void Main(String[] args) { int N = 5; int K = 3; int L = 1; /* call the maxCakes function and print the result */ Console.WriteLine(maxCakes(N, K, L)); } } // This code is contributed by ragul21 |
Javascript
// Javascript code for the above approach // function to calculate maximum number of cakes function maxCakes(N, K, L) { // calculate the number of cakes we can buy let BuyCakes = K - L; /* calculate the availability of cakes when we have to sell */ if (N + BuyCakes < L) { /* return -1 if we have shortage of cakes when we have to sell*/ return -1; } else { /* return the maximum number of cakes we will have at a place */ return BuyCakes + N; } } // Driver code let N = 5; let K = 3; let L = 1; /* call the maxCakes function and print the result */ console.log(maxCakes(N, K, L)); // This code is contributed by ragul21 |
7
Time Complexity: O(1),
Auxiliary Space: O(1).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!