A
A
Anton2019-05-04 06:05:29
IT education
Anton, 2019-05-04 06:05:29

How to correctly understand the problem "Apple and Orange" on hackerrank?

Good afternoon!
I got to the problem "Apple and Orange" on hackerrank and got confused in its intricacies.
Sam's house has an apple tree and an orange tree, which provide an abundance of fruit. In the diagram below, the red area represents its home, where s is the start point and t is the end point.
The apple tree is to the left of his house, and the orange tree is to the right.
We can assume that the trees are located at one point, where the apple tree is at point a, and the orange tree is at point b.
When a fruit falls from its tree, it lands at a distance of d units from its tree along the x-axis.
A negative value of d means that the fruit fell to the left of the tree, and a positive value of d means that it fell to the right of the tree.
Given the value of d for m apples and n oranges, how many apples and oranges will fall on Sam's house (i.e. in the inclusive range [s,t)?
For example, Sam's house is between s=7 and t=10. The apple tree is located on a=4 and the orange on b=12.
There are m=3 apples and n=3 oranges.
Apple trees throw apples at [2,3,-4] distance units from a, and orange trees throw oranges at [3,-2,-4] distance units.
For each apple distance added to the tree position, they land at [4+2,4+3,4-4]=[6,7,0]
Oranges fall to the ground [12+3,12-2,12-4 ]=[15,10,8]
One apple and two oranges land in the range 7-10, so we print


1
2

Where did [2,3,-4] and [3,-2,-4] come from?
Looks like the issue is missing

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2019-05-07
Patsev @chemtech

[2,3,-4]:
First apple fell to position 2
Second apple fell to position 3
Third apple fell to position -4
[3,-2,-4]
First orange fell to position 3
First orange fell to position -2
The first orange fell to position -4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question