Sort by

recency

|

3020 Discussions

|

  • + 0 comments

    Java Beginner Approach:

    public static void countApplesAndOranges(int s, int t, int a, int b, List<Integer> apples, List<Integer> oranges) {
            int appleCount=0, orangeCount=0, pos; 
            
            for(int i=0; i<apples.size(); i++){
                pos = a+apples.get(i); 
                if(pos >= s && pos <= t)
                    appleCount++;
                    
                pos = 0;
            }
            
            for(int i=0; i<oranges.size(); i++){
                pos = b+oranges.get(i); 
                if(pos >= s && pos <= t)
                    orangeCount++;
                    
                pos = 0;
            }
            
            System.out.println(appleCount);
            System.out.println(orangeCount);
        }
    
  • + 0 comments

    Here is muy C# solution

     public static void countApplesAndOranges(int s, int t, int a, int b, List<int> apples, List<int> oranges)
        {
          Console.WriteLine(apples.Select(x => calculateRange(a, s, t, x)).Sum());
          Console.WriteLine(oranges.Select(x => calculateRange(b, s, t, x)).Sum());
        }
        
        public static int calculateRange(int localtedTree, int localtedHomeS, int localtedHomeT, int fruit)
        =>((localtedTree + fruit) >= localtedHomeS && (localtedTree + fruit) <= localtedHomeT) ? 1 : 0;       
        
    
  • + 0 comments

    Here is my java solution, o(n)

    public static void countApplesAndOranges(int s, int t, int a, int b, List<Integer> apples, List<Integer> oranges) {
        // Write your code here
            int applesInHome = 0;
            int orangesInHome = 0;
            boolean flagApple = true;
            boolean flagOrange = true;
            int i = 0;
            while (flagApple || flagOrange) {
                if (i >= apples.size()) {
                    flagApple = false;
                }
                if (i >= oranges.size()) {
                    flagOrange = false;
                }
                if (flagApple && (apples.get(i) + a >= s && apples.get(i) + a <= t)) {
                    applesInHome++;
                }
                if (flagOrange && (oranges.get(i) + b >= s && oranges.get(i) + b <= t)) {
                    orangesInHome++;
                }
                i++; 
    
        }
        System.out.println(applesInHome);
        System.out.println(orangesInHome);
    }
    
  • + 0 comments

    Here is my c++ solution, you can see the explanation here : https://youtu.be/YJVWinC21Fg

    void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
        int orange = 0, apple = 0;
        for(int app: apples) if(app + a >= s && app + a <=t) apple++;
        for(int orr: oranges) if(orr + b >= s && orr + b <=t) orange++;
        cout << apple << endl << orange;
    }
    
  • + 0 comments

    Python

    app_loc = [i + a for i in apples] org_loc = [i + b for i in oranges]

    app_count = sum(1 for i in app_loc if s <= i <= t)
    org_count = sum(1 for i in org_loc if s <= i <= t)
    
    print(app_count)
    print(org_count)