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

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 m apples and oranges, can you determine how many apples and oranges will fall on Sam’s house (i.e., in the inclusive range [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:

  1. On the first line, print the number of apples that fall on Sam’s house.
  2. 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.

--

--

--

Hey curious person, I am Software Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to install Android Studio on your Mac and launch Emulator.

Android 13: Notification Runtime Permission

Time Conversion Solution in Kotlin — HackerRank

Implementation of Adjust SDK- Android App

Data Binding in Android — Divami

Data binding

Most Common Android Problems  — Android Pitfalls 🐭 🧀

Searchbox for Flutter: Build mobile search experiences with appbase.io

String, StringBuffer, StringBuilder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Umar Syaid Himawan

Umar Syaid Himawan

Hey curious person, I am Software Developer

More from Medium

The Philosophy of ViewModel

Coroutine Adventure Part.4

Using Secrets in Circle Ci and Local

Explore on Guard Clause