Sort by

recency

|

3103 Discussions

|

  • + 0 comments
    function countApplesAndOranges(start: number, end: number, appleTree: number, orangeTree: number, apples: number[], oranges: number[]): void {
        let applesCount = 0, orangesCount = 0;
        
        for (let index = 0; index < Math.max(apples.length, oranges.length); index ++) {
            const applePosition = appleTree + apples?.[index];
            const orangePosition = orangeTree + oranges?.[index];
            
            if (!Number.isNaN(applePosition) && applePosition >= start && applePosition <= end) {
                applesCount++;
            }
            
            if (!Number.isNaN(orangePosition) && orangePosition >= start && orangePosition <= end) {
                orangesCount++;
            }
        }
        
        console.log(applesCount.toString());
        console.log(orangesCount.toString());
    }
    
  • + 0 comments

    python 3

    def countApplesAndOranges(s, t, a, b, apples, oranges):

    apples = sum(1 for d in apples if s <= a + d <= t)
    
    oranges = sum(1 for d in oranges if s <= b + d <= t)
    
    print(apples)
    print(oranges)
    
  • + 0 comments

    Java

    public static void countApplesAndOranges(int s, int t, int a, int b, List<Integer> apples, List<Integer> oranges) {
        // Write your code here
        long countApples =apples.stream().map(apple->apple+a).filter(pos->pos>=s && pos<=t).count();
        long countOranges =oranges.stream().map(orange->orange+b).filter(pos->pos>=s && pos<=t).count();
        
        System.out.println(countApples+"\n"+countOranges);
    
        }
    
  • + 0 comments

    python a_count = 0 b_count = 0 for apple in apples: apple += a if apple <= t and apple >= s: a_count += 1 for orange in oranges: orange += b if orange <= t and orange >= s: b_count += 1 print(f"{a_count} \n{b_count}")

  • + 0 comments

    python a_count = 0 b_count = 0 for apple in apples: apple += a if apple <= t and apple >= s: a_count += 1 for orange in oranges: orange += b if orange <= t and orange >= s: b_count += 1 print(f"{a_count} \n{b_count}")