Javatpoint Logo
Javatpoint Logo

Apple and Orange

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 s is 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. *
Apple and Orange

Given the value of d for m apples and 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 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

Function Description

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.

Input Format

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 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.

Constraints

  • 1 ≤ s, t, a, b, m, n ≤ 105
  • -105 ≤ d ≤ 105
  • a < s < t < b

Output Format

Print two integers on two different lines:

  1. The first integer: the number of apples that fall on Sam's house.
  2. The second integer: the number of oranges that fall on Sam's house.

Sample Input 0

Sample Output 0

1
1

Explanation 0

The first apple falls at position 5 - 2 = 3.

The second apple falls at position 5 + 2 = 7.

The third apple falls at position 5 + 1 = 6.

The first orange falls at position15 + 5 = 20.

The second orange falls at position 15 - 6 = 9.

Only one fruit (the second apple) falls within the region between 7 and 11, so we print 1 as our first line of output.

Only the second orange falls within the region between 7 and 11, so we print as our second line of output.

The Code OF Apple and Orange Problem in C

Explanation of above code:

1. Declared variable s, t, b, a, m, n, d

2. Declared and initialized variable apple = 0; and orange = 0

3. Asked to insert value of start & end point of boundary of Sam's house

4. Asked to insert value of apple & orange tree location

5. Asked to insert value for no. of apples & no. of oranges

6. Loop for entering distance of m apples is given below:

Let starting point s = 7 and end point t = 11 of Sam's House, location of apple tree a = 5 and orange tree b = 15, and no. of apples m = 3 and no. of oranges n = 2.

Hence above for loop run for m = 3, Let value entered are [-2 2 1],

7. Loop for entering distance of n oranges is below

As we already look, starting point s = 7 and end point t = 11 of Sam's House, location of apple tree a = 5 and orange tree b = 15, and no. of apples m = 3 and no. of oranges n = 2.

Hence above for loop run for n = 2, Let value entered are [5, -6],

8. At last line the statement printf( " %d %d ", apple, orange);

It will print values of apple and oranges which actually fell on or inside the boundary of Sam's house, since value of apple =1 and orange = 1, therefore output will be: 1 1


Next TopicHigh Availability





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA