• + 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);
    }