Minimum number of Apples to be collected from trees to guarantee M red apples
Last Updated :
10 Aug, 2022
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 tree
Input: 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:
- If M is less than equal to S*K then print M.
- Else if M is less than equal to S*K+E+W then print S*K + (M-S*K) * K
- Else print -1.
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>
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.
Similar Reads
Minimum number of leaves required to be removed from a Tree to satisfy the given condition
Given a Tree consisting of N vertices, rooted at vertex 1 and an array val[] representing the values assigned to each vertex, and an array cost[] representing the cost of each edge in the Tree, the task is to find the minimum number of leaves to be removed from the given tree such that: For any vert
8 min read
Maximum number of apples that can be eaten by a person
Given two arrays apples[] and days[] representing the count of apples an apple tree produces and the number of days these apples are edible from the ith day respectively, the task is to find the maximum number of apples a person can eat if the person can eat at most one apple in a day. Examples Inpu
9 min read
Collect all coins in minimum number of steps
Given many stacks of coins which are arranged adjacently. We need to collect all these coins in the minimum number of steps where in one step we can collect one horizontal line of coins or vertical line of coins and collected coins should be continuous.Examples : Input : height[] = [2 1 2 5 1] Each
8 min read
Program to find minimum number of lectures to attend to maintain 75%
Consider the subject Data Structures for which the total number of classes held till present date is M , and some students attend only N out of these classes. Find the minimum number of lectures they have to attend so that their 75\% attendance is maintained. Examples: Input : M = 7 and N = 6 Output
4 min read
Count minimum number of fountains to be activated to cover the entire garden
There is a one-dimensional garden of length N. In each position of the N length garden, a fountain has been installed. Given an array a[]such that a[i] describes the coverage limit of ith fountain. A fountain can cover the range from the position max(i - a[i], 1) to min(i + a[i], N). In beginning, a
13 min read
Minimum number of equal amount bags to collect at least M money
Given unlimited number of coins of two denomination X and Y. Also given bags with capacity of N rupees, independent of number of coins. The task is to find the minimum number of bags such that each bag contains the same amount of rupees and sum of all the bags amount is at least M. Examples : Input
6 min read
Minimum number of coins to be collected per hour to empty N piles in at most H hours
Given an array arr[] consisting of N integers representing the number of coins in each pile, and an integer H, the task is to find the minimum number of coins that must be collected from a single pile per hour such that all the piles are emptied in less than H hours. Note: Coins can be collected onl
8 min read
Minimum number of items to be delivered
Given N buckets, each containing A[i] items. Given K tours within which all of the items are needed to be delivered. It is allowed to take items from only one bucket in 1 tour. The task is to tell the minimum number of items needed to be delivered per tour so that all of the items can be delivered w
14 min read
Minimum number of operations required to make all elements equal
Given an array arr[] of length N along with an integer M. All the elements of arr[] are in the range [1, N]. Then your task is to output the minimum number of operations required to make all elements equal given that in one operation you can select at most M elements and increment all of them by 1.
5 min read
Minimum number of cameras required to monitor all nodes of a Binary Tree
Given a Binary Tree consisting of N nodes, the task is to find the minimum number of cameras required to monitor the entire tree such that every camera placed at any node can monitor the node itself, its parent, and its immediate children. Examples: Input: 0 / 0 /\ 0 0Output: 1Explanation: 0 / 0
8 min read