HackerRank ‘Apple and Orange’ Solution

Short Problem Definition:

Sam’s house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where s is the start point, and i is the endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point a, and the orange tree is at point b.

Link

Apple And Orange

Complexity:

time complexity is O(N)

space complexity is O(1)

Execution:

Follow the problem specification.

Solution:
def solve(house_left, house_right, tree, elements):
    cnt = 0
    for ele in elements:
        if tree+ele >= house_left and tree+ele <= house_right:
           cnt += 1
    return cnt

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

Facebooktwittergoogle_plusredditpinterestlinkedin