# Apple and Orange Solution in Kotlin -HackerRank

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

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

*t***, and the orange tree is at point**

*a***.**

*b*When a fruit falls from its tree, it lands ** d** units of distance from its tree of origin along the

**-axis. A negative value of**

*x***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**

*d***Complete the function countApplesAndOranges**

where :

** start **Starting point of Sam’s house location.

** end** Ending location of Sam’s house location.

** Loc a** Location of the Apple tree.

** Loc o** Location of the Orange tree.

** size a** Number of apples that fell from the tree.

** apples **Distance at which each apple falls from the tree.

** size o** Number of oranges that fell from the tree.

**orange **Distance at which each orange falls from the tree.

Given the value of ** d** for

**apples and oranges, can you determine how many apples and oranges will fall on Sam’s house (i.e., in the inclusive range**

*m***[s,t]**)? Print the number of apples that fall on Sam’s house as your first line of output, then print the number of oranges that fall on Sam’s house as your second line of output.

**Input Format**

The first line contains two space-separated integers denoting the respective values of and . The second line contains two space-separated integers denoting the respective values of and . The third line contains two space-separated integers denoting the respective values of and . The fourth line contains space-separated integers denoting the respective distances that each apple falls from point . The fifth line contains space-separated integers denoting the respective distances that each orange falls from point .

**Constraints**

*1 < s, t, a, b, m, n < 10**-10 < d < 10**a < s < t < b*

**Output Format**

Print two lines of output:

- On the first line, print the number of apples that fall on Sam’s house.
- On the second line, print the number of oranges that fall on Sam’s house.

**Sample Input**

`7 11 `

5 15

3 2

-2 2 1

5 -6

**Sample Output**

`1 `

1

**ANSWER**

// Complete the countApplesAndOranges function below.

fun countApplesAndOranges(s: Int, t: Int, a: Int, b: Int, apples: Array<Int>, oranges: Array<Int>): Unit {

val startPointHome = s

val endPointHome = t

val appleLoc = a

val orangeLoc = b

var orangeOnHouse = 0

var applesOnHouse = 0// Calculate of apples that fall on house

for (x in 0 until apples.size) {

val item = apples[x]

var applePosition = appleLoc + item

if (applePosition >= startPointHome &&

applePosition <= endPointHome) {

applesOnHouse++

}

}// Calculate of orange that fall on house

for (x in 0 until oranges.size) {

val item = oranges[x]

var orangePosition = orangeLoc + item

if (orangePosition >= startPointHome &&

orangePosition <= endPointHome) {

orangeOnHouse++

}

}println(applesOnHouse)

println(orangeOnHouse)

}

**Explanation**

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