DEV Community

Tshihab07
Tshihab07

Posted on

3 1

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay