We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Apple and Orange
  5. Discussions

Apple and Orange

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2201 Discussions, By:

votes

Please Login in order to post a comment

  • navjotahuja92
    6 years ago+ 84 comments

    python way ;)

    print(sum([1 for x in apple if (x + a) >= s and (x + a) <= t]))
    print(sum([1 for x in orange if (x + b) >= s and (x + b) <= t]))
    
    433|
    Permalink
    View more Comments..
  • RodneyShag
    5 years ago+ 23 comments

    Java solution - passes 100% of test cases

    From my HackerRank solutions.

    Runtime: O(m + n)
    Space Complexity: O(1)

    Avoid using arrays to store values since that will take O(m + n) space.

    import java.util.Scanner;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Read and save input */
            Scanner scan = new Scanner(System.in);
            int s = scan.nextInt();
            int t = scan.nextInt();
            int a = scan.nextInt();
            int b = scan.nextInt();
            int m = scan.nextInt();
            int n = scan.nextInt();
            
            /* Calculate # of apples that fall on house */
            int applesOnHouse = 0;
            for (int i = 0; i < m; i++) {
                int applePosition = a + scan.nextInt();
                if (applePosition >= s && applePosition <= t) {
                    applesOnHouse++;
                }
            }
            System.out.println(applesOnHouse);
            
            /* Calculate # of oranges that fall on house */
            int orangesOnHouse = 0;
            for (int i = 0; i < n; i++) {
                int orangePosition = b + scan.nextInt();
                if (orangePosition >= s && orangePosition <= t) {
                    orangesOnHouse++;
                }
            }
            System.out.println(orangesOnHouse);
            
            scan.close();
        }
    }
    

    Let me know if you have any questions.

    50|
    Permalink
    View more Comments..
  • DavidODW
    6 years ago+ 10 comments

    javascript way :)

    var apple_count = apple.filter(value => value + a >= s && value + a <= t).length;
    var orange_count = orange.filter(value => value + b >= s && value + b <= t).length;
    
    41|
    Permalink
    View more Comments..
  • sayyid_ali777
    2 years ago+ 1 comment

    For the one who creates this challenge !!!!!

    Why can't you explain clearly your challenge . when I see the answer it is a just simple code But understanding the question is the basis to answer correctly

    Kindly rewrite your question

    Thanks

    14|
    Permalink
  • sharma1992nee
    3 years ago+ 6 comments

    C++ Solution , any optimizations please suggest :

    void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges)
    {
        int m = apples.size();
        int n = oranges.size();
        int appCount=0;
        int orgCount=0;
    
        for(int i =0; i< m ; i++)
        {
            int appSum = a + apples[i];
            if(appSum >= s && appSum <= t)
                appCount++;
        }
    
        for(int i =0; i< n ; i++)
        {
            int orgSum = b + oranges[i];
            if(orgSum >= s && orgSum <= t)
                orgCount++;
        }
        cout<<appCount<<endl;
        cout<<orgCount<<endl;
    }
    

    `

    10|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature