Sort by

recency

|

3042 Discussions

|

  • + 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
    def countApplesAndOranges(s, t, a, b, apples, oranges):
        # Write your code here
        
        apple_count = len([ i for i in apples if a+i in range(s, t+1)])
        orange_count = len([ j for j in oranges if b +j in range(s, t+1)])
        
                
        print(apple_count)
        print(orange_count)
    
  • + 0 comments
    def countApplesAndOranges(s, t, a, b, apples, oranges):
    
        result_a = []
        result_o = []
        for i in apples:
             i = i + a
             if i in range(s,t+1):
                 result_a.append(i)
        print(len(result_a))
        
        for o in oranges:
            o = o + b
            if o in range(s,t+1):
                result_o.append(o)
        print(len(result_o))
    
  • + 0 comments

    TypeScript Solution 1. Declare orange and apple amount variables 2. Declare pointer variable 3. Loops until pointer is length of apple array - 1 while checking if apples and oranges aat that index are within the range of s to t 4. Checks if there are more oranges in the orange array 5. Begins to loop through the oranges array from where it left off in the last loop 6. Console log apple and orange amounts

    function countApplesAndOranges(s: number, t: number, a: number, b: number, apples: number[], oranges: number[]): void {
      let orangeAmount = 0;
      let appleAmount = 0;
      let pointer: number;
      
      for (pointer = 0; pointer < apples.length; pointer++) {
        // Check if apples[pointer] && (apples[pointer] >= s && apples[pointer] <= t)
          // Increment appleAmount
          
        // Check if oranges[pointer] && (oranges[pointer] >= s && apples[pointer] <= t)
          // Increment orangeAmount
        if (apples[pointer] && (apples[pointer] + a >= s && apples[pointer] + a <= t)) {
          appleAmount += 1;
        }
        
        if (oranges[pointer] && (oranges[pointer] + b >= s && oranges[pointer] + b <= t)) {
          orangeAmount += 1;
        }
      }
      
      // Check if pointer < oranges.length - 1 
        // Loop though and apply the same if statemtns as the previous loop
        
      // console log appleAmount
      // console log orangeAmount
      
      if (pointer < oranges.length - 1) {
        for (; pointer < oranges.length; pointer++) {
          if (oranges[pointer] && (oranges[pointer] + b >= s && oranges[pointer] + b <= t)) {
          orangeAmount += 1;
          }
        } 
      }
      
      console.log(appleAmount);
      console.log(orangeAmount);
    }
    
  • + 0 comments

    PHP solution

    function countApplesAndOranges(
        int $houseStart,
        int $houseEnd,
        int $applePoint,
        int $orrangePoint,
        array $apples,
        array $oranges
    ) {
        $getPoints=static function(int $treePoint, array $points) use ($houseStart,$houseEnd) {
            $res=0;
            foreach($points as $point) {
                $fallPoint = $treePoint+$point;
                if ($fallPoint>=$houseStart && $fallPoint<=$houseEnd) $res++;
            }
            return $res;
        };
        
        foreach([$applePoint=>$apples, $orrangePoint=>$oranges] as $tree=>$fruits) {
            print($getPoints($tree,$fruits) . "\n");
        }
    }