by Travis Scott Lee
Sam’s house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam’s house.
In the diagram below:
- The red region denotes the house, where is s the start point, and t is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.
- 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.
- When a fruit falls from its tree, it lands d units of distance from its tree of origin along the x-axis. *A negative value of d means the fruit fell d units to the tree’s left, and a positive value of d means it falls d units to the tree’s right. *
Given the value of d for m apples and n oranges, determine 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 at a = 4 and the orange at b = 12. There are m = 3 apples and n = 3 oranges. Apples are thrown apples = [ 2,3, -4] units distance from a, and oranges = [ 3,-2,-4] units distance. Adding each apple distance to the position of the tree, they land at [ 4 +2, 4 +3, 4 + -4] = [6, 7, 0]. Oranges land at [ 12 + 3, 12 + -2, 12 + -4] = [15,10,8]. One apple and two oranges land in the inclusive range 7– 10 so we print
Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam’s house, each on a separate line.
countApplesAndOranges has the following parameter(s):
- s: integer, starting point of Sam’s house location.
- t: integer, ending location of Sam’s house location.
- a: integer, location of the Apple tree.
- b: integer, location of the Orange tree.
- apples: integer array, distances at which each apple falls from the tree.
- oranges: integer array, distances at which each orange falls from the tree.
The first line contains two space-separated integers denoting the respective values of s and t.
The second line contains two space-separated integers denoting the respective values of a and b.
The third line contains two space-separated integers denoting the respective values of m and n.
The fourth line contains m space-separated integers denoting the respective distances that each apple falls from point a.
The fifth line contains n space-separated integers denoting the respective distances that each orange falls from point b.
- 1 ≤ s,t,a,b,m,n ≤ 10⁵
- -10⁵ ≤ d ≤ 10⁵
- a < s < t <b
Print two integers on two different lines:
- The first integer: the number of apples that fall on Sam’s house.
- The second integer: the number of oranges that fall on Sam’s house.
In this algothrim, we set up two
let variables called appleCount and orangeCount. They will represent the apples and orange the fall within the area of the house. We use two
for loops to iterate throughout the two arrays of apples and oranges. Within this loops, we will check if they fall within the boundary of the house, and if it does, we add to the count. Finally, we will console.log both results.