# Apple and Orange | HackerRank Solution

Hello coders, today we are going to solve Apple and Orange HackerRank Solution which is a Part of HackerRank Algorithm Series.

Contents

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

1
2

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

## Constraints

• 1 <= s, t, a, b, m, n <= 105
• – 10 <= 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

7 11
5 15
3 2
-2 2 1
5 -6

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 positionÂ 15 + 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Â 1Â as our second line of output.

## Solution – Apple and Orange

### C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int s, t, a, b, n, m, d, ans1=0, ans2=0;
cin >> s >> t >> a >> b >> m >> n;

for(int i=0;i<m; i++) {
cin>>d;
d = a+d;
if(d>=s && d<=t)
ans1++;
}
for(int i=0;i<n; i++) {
cin>>d;
d = b+d;
if(d>=s && d<=t)
ans2++;
}
cout << ans1 << endl;
cout << ans2 << endl;
return 0;
}

### Python

import math
import os
import random
import re
import sys

# Complete the countApplesAndOranges function below.
def countApplesAndOranges(s, t, a, b, apples, oranges):
count_Apples = 0
count_Oranges= 0

for i in range(len(apples)):
if a+apples[i] >= s and a+apples[i] <= t:
count_Apples +=1
for i in range(len(oranges)):
if b+oranges[i] >= s and b+oranges[i] <=t:
count_Oranges +=1
print(count_Apples)
print(count_Oranges)

if __name__ == '__main__':
st = input().split()
s = int(st[0])
t = int(st[1])
ab = input().split()
a = int(ab[0])
b = int(ab[1])
mn = input().split()
m = int(mn[0])
n = int(mn[1])
apples = list(map(int, input().rstrip().split()))
oranges = list(map(int, input().rstrip().split()))
countApplesAndOranges(s, t, a, b, apples, oranges)

Disclaimer: The above Problem (Apple and Orange) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.