DEV Community

Tshihab07
Tshihab07

Posted on

Hackerrank - Apple and Orange Solution in Python

step - 1: Get maximum between apple and orange array

step - 2: iterate over the maximum

step - 3: check the array indexes and if True add the fruit's position with the tree location (a or b) so that we get the actual position of the fruit

step - 4: nested if condition for the actual position of the fruit is in between the house range (s and t) and if true the count will be increased

Step - 5: print the output

def countApplesAndOranges(s, t, a, b, apples, oranges):
    # Write your code here
    apple_count = 0
    orange_count = 0

    max_length = max(len(apples), len(oranges))

    for idx in range(max_length):
        if idx < len(apples):
            count = apples[idx] + a
            if s <= count <= t:
                apple_count += 1

        if idx < len(oranges):
            count = oranges[idx] + b
            if s <= count <= t:
                orange_count += 1

    print(apple_count)
    print(orange_count)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)