Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Puzzles
  • Funny Riddles
  • Interesting Riddles
  • Mathematical Riddles
  • Animal Riddles
  • Mathematical Puzzles
  • Rubik's Cube
  • Aptitude-Puzzles
  • Top 100 Puzzles
  • Puzzles Quiz
Open In App
Next Article:
Minimum number of Apples to be collected from trees to guarantee M red apples
Next article icon

Minimum number of Apples to be collected from trees to guarantee M red apples

Last Updated : 10 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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> 

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


Next Article
Minimum number of Apples to be collected from trees to guarantee M red apples

H

harshitkap00r
Improve
Article Tags :
  • Greedy
  • Mathematical
  • Puzzles
  • DSA
Practice Tags :
  • Greedy
  • Mathematical
  • Puzzles

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
    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 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 mails required to distribute all the questions
    Given N questions in a test and K students in the class. Out of the batch of K students, N students memorised exactly one question each. A mail can contain about a maximum of X questions. Find the minimum number of mails required so that the entire class gets to know about all the questions. NOTE: A
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences