Sort by

recency

|

3050 Discussions

|

  • + 0 comments

    function countApplesAndOranges(s, t, a, b, apples, oranges) { // Write your code here

    let appleCount = apples.filter(apple => {
        return (apple + a >= s) && (apple + a <= t)
    })
    
    let orangeCount = oranges.filter(orange => {
        return (orange + b >= s) && (orange + b <= t)
    })    
    console.log(appleCount.length)
    console.log( orangeCount.length)
    

    }

  • + 0 comments

    My solution, guys.

    void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
      int napple = 0; 
      int norange = 0; 
      
      for(unsigned i = 0; i<apples.size(); i++){
        if((apples[i]+a) >= s && (apples[i]+a) <= t)
          napple += 1; 
      }
      for(unsigned i = 0; i<oranges.size(); i++){
        if((oranges[i]+b) >= s && (oranges[i]+b) <= t)
          norange += 1; 
      }
      
      cout<<napple<<endl;
      cout<<norange;
    }
    
  • + 0 comments

    Look at my creative C++ solution :-)

    void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
        std::pair<int, int> fruits_landed = {0,0};
        
        std::transform(apples.begin(), apples.end(), apples.begin(), [a](int pos)->int{
            return (pos + a);
        });
        std::transform(oranges.begin(), oranges.end(), oranges.begin(), [b](int pos)->int{
            return (pos + b);
        });
        
        fruits_landed.first = std::count_if(apples.begin(), apples.end(), [s,t](int pos)->bool{
            return (pos >= s && pos <= t);
        });
        fruits_landed.second = std::count_if(oranges.begin(), oranges.end(), [s,t](int pos)->bool{
            return (pos <= t && pos >= s);
        });
        
        std::cout << fruits_landed.first << endl << fruits_landed.second << endl;
    }
    

    Here is an even better version -

    void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
        std::pair<int, int> fruits_landed = {0,0};
        std::function<bool(int)> condition = [s,t](int pos)->bool{ return (pos >= s && pos <= t);};
        
        std::transform(apples.begin(), apples.end(), apples.begin(), [a](int pos)->int{
            return (pos + a);
        });
        std::transform(oranges.begin(), oranges.end(), oranges.begin(), [b](int pos)->int{
            return (pos + b);
        });
        
        fruits_landed.first = std::count_if(apples.begin(), apples.end(), [condition](int pos)->bool{
            return condition(pos);
        });
        fruits_landed.second = std::count_if(oranges.begin(), oranges.end(), [condition](int pos)->bool{
            return condition(pos);
        });
        
        std::cout << fruits_landed.first << endl << fruits_landed.second << endl;
    }
    
  • + 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

    simple with es6 functions

        console.log(apples.map(n=>n+a).filter(n=>n>=s&&n<=t).length)
        console.log(oranges.map(n=>n+b).filter(n=>n>=s&&n<=t).length)