Hackerrank-Apple and Orange Solution in Kotlin
Published: 2022-07-20 21:11:01

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. *
 
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 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 < 10
-     -10 < d < 10
-     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

```
**ANSWER:**

```
 fun countApplesAndOranges(
        s: Int,
        t: Int,
        a: Int,
        b: Int,
        apples: Array<Int>,
        oranges: Array<Int>
    ): Unit {
         var countApple = 0
         var countOrang = 0

         for (i in apples) {
             var appleDistance = a + i
             if (s <= appleDistance && appleDistance <= t)
                 countApple = countApple + 1
         }
         for (i in oranges) {
             var orangeDistance = b + i
             if (s <= orangeDistance && orangeDistance <= t)
                 countOrang = countOrang + 1
         }
         println(countApple)
         println(countOrang)
 }
```