Sort by

recency

|

2994 Discussions

|

  • + 0 comments

    php solution:

    function countApplesAndOranges(t, b, oranges) { orange_count = 0;

    foreach (`$apples as $`key => $value) {
        `$sum = $`a + $value;
        if( `$sum >= $`s && `$sum <= $`t ){
            $apple_count++;
        }
    }
    
    foreach (`$oranges as $`key => $value) {
        `$sum = $`b + $value;
        if( `$sum >= $`s && `$sum <= $`t ){
            $orange_count++;
        }
    }
    
    echo `$apple_count."\n".$`orange_count;
    

    }

  • + 0 comments

    Clean Python Code:

    def countApplesAndOranges(s, t, a, b, apples, oranges):
        # Write your code here
        apple_count = 0
        orange_count = 0
        
        
        for i in range(len(apples)):
            apples[i] += a
            if s <= apples[i] <= t:
                apple_count += 1
                
        for i in range(len(oranges)):
            oranges[i] += b
            if s <= oranges[i] <= t:
                orange_count += 1
        
        print(apple_count)
        print(orange_count)
    
  • + 0 comments

    Optimization code in TypeScript

    function countApplesAndOranges(s: number, t: number, a: number, b: number, apples: number[], oranges: number[]): void {
        // Write your code here
        
        let appleCount=0
        let orangeCount=0
        
       for (let i of apples) {
        if (a+i>=s && a+i<=t)
            appleCount++;
        }
        for(let i of oranges) {
            if (b+i>=s && b+i<=t)
                orangeCount++;
        }
        
        console.log(appleCount)
        console.log(orangeCount)
    }
    
  • + 0 comments

    That is Brute force silution but not time env to examine all tests

    function countApplesAndOranges(s: number, t: number, a: number, b: number, apples: number[], oranges: number[]): void {
        // Write your code here
        
        let appleCount=0
        let orangeCount=0
        
        for(let i=0; i<apples.length; i++) {
            apples[i]+=a
        }
        for(let i=0; i<oranges.length; i++) {
            oranges[i]+=b
        }
        
        for(let i=s; i<=t; i++) {
            for(let j=0; j<apples.length; j++) {
                if(apples[j]==i) {
                    appleCount++
                } else if(oranges[j]==i) {
                    orangeCount++
                }
            }
        }
        
        console.log(appleCount)
        console.log(orangeCount)
    }
    
  • + 0 comments

    public static void countApplesAndOranges(int s, int t, int a, int b, List apples, List oranges) { // Write your code here int nbrapples , nbroranges ; nbrapples = apples.stream().map( d -> a + d ) .filter(d ->( d >= s && d <= t)).collect(toList()).size(); nbroranges = oranges.stream().map( d -> b + d ) .filter(d ->( d >= s && d <= t)).collect(toList()).size();

        System.out.println(nbrapples);
        System.out.println(nbroranges);
    }