Minimum number of Apples to be collected from trees to guarantee M red apples
There are different kinds of apple trees in the four directions (East, West, North, South), which may grow both red and green apples such that each tree grows exactly K apples, in the following manner:
- N – number of trees to the north does not have red apples.
- S – number of trees to the south does not have green apples.
- W – number of trees in the west has some red apples.
- E – number of trees in the east have some green apples.
However, the colors of apples cannot be distinguished outside the house. So, the task is to find the minimum number of apples to be collected from the trees to guarantee M red apples. If it is not possible, print -1.
Examples:
Input: M = 10, K = 15, N = 0, S = 1, W = 0, E = 0
Output: 10
Explanation: It simply gets 10 apples from the 1st south treeInput: M = 10, K = 15, N = 3, S = 0, W = 1, E = 0
Output: -1
Explanation: There are no red apples in the South, North and East. But in the West there are atleast 1 red apple and total tree is 1, So, total no. of guaranteed red apple is 1 * 1 = 1 which is less than M.
Approach: Every apple in the south ensures that it is red. So first, take an apple from the south. In the East and West, there is at least 1 red apple in each tree. That’s why for guaranteed it is considered that there is only 1 red apple on each tree in the east and west. For the north there is no red apple, so, neglect that. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; // Function to minimum no. of apples int minApples( int M, int K, int N, int S, int W, int E){ // If we get all required apple // from South if (M <= S * K) return M; // If we required trees at // East and West else if (M <= S * K + E + W) return S * K + (M-S * K) * K; // If we doesn't have enough // red apples else return -1; } // Driver Code int main(){ // No. of red apple for gift int M = 10; // No. of red apple in each tree int K = 15; // No. of tree in North int N = 0; // No. of tree in South int S = 1; // No. of tree in West int W = 0; // No. of tree in East int E = 0; // Function Call int ans = minApples(M,K,N,S,W,E); cout<<ans<<endl; } // This code is contributed by ipg2016107. |
Java
// Java program for the above approach import java.io.*; class GFG { // Function to minimum no. of apples static int minApples( int M, int K, int N, int S, int W, int E) { // If we get all required apple // from South if (M <= S * K) return M; // If we required trees at // East and West else if (M <= S * K + E + W) return S * K + (M-S * K) * K; // If we doesn't have enough // red apples else return - 1 ; } // Driver code public static void main(String[] args) { // No. of red apple for gift int M = 10 ; // No. of red apple in each tree int K = 15 ; // No. of tree in North int N = 0 ; // No. of tree in South int S = 1 ; // No. of tree in West int W = 0 ; // No. of tree in East int E = 0 ; // Function Call int ans = minApples(M,K,N,S,W,E); System.out.println(ans); } } // This code is contributed by code_hunt. |
Python3
# Python program for the above approach # Function to minimum no. of apples def minApples(): # If we get all required apple # from South if M < = S * K: return M # If we required trees at # East and West elif M < = S * K + E + W: return S * K + (M - S * K) * K # If we doesn't have enough # red apples else : return - 1 # Driver Code if __name__ = = "__main__" : # No. of red apple for gift M = 10 # No. of red apple in each tree K = 15 # No. of tree in North N = 0 # No. of tree in South S = 1 # No. of tree in West W = 0 # No. of tree in East E = 0 # Function Call ans = minApples() print (ans) |
C#
// C# program for the above approach using System; class GFG{ // Function to minimum no. of apples static int minApples( int M, int K, int N, int S, int W, int E) { // If we get all required apple // from South if (M <= S * K) return M; // If we required trees at // East and West else if (M <= S * K + E + W) return S * K + (M - S * K) * K; // If we doesn't have enough // red apples else return -1; } // Driver code public static void Main(String[] args) { // No. of red apple for gift int M = 10; // No. of red apple in each tree int K = 15; // No. of tree in North int N = 0; // No. of tree in South int S = 1; // No. of tree in West int W = 0; // No. of tree in East int E = 0; // Function Call int ans = minApples(M, K, N, S, W, E); Console.Write(ans); } } // This code is contributed by shivanisinghss2110 |
Javascript
<script> // JavaScript program for the above approach; // Function to minimum no. of apples function minApples() { // If we get all required apple // from South if (M <= S * K) return M; // If we required trees at // East and West else if (M <= S * K + E + W) return S * K + (M - S * K) * K; // If we doesn't have enough // red apples else return -1; } // Driver Code // No. of red apple for gift M = 10 // No. of red apple in each tree K = 15 // No. of tree in North N = 0 // No. of tree in South S = 1 // No. of tree in West W = 0 // No. of tree in East E = 0 // Function Call ans = minApples() document.write(ans); // This code is contributed by Potta Lokesh </script> |
10
Time Complexity: O(1) // since no loop is used the algorithm takes constant space to execute
Auxiliary Space: O(1) // since no extra array is used the solution takes up constant space.
Contact Us