Thursday, July 21, 2022
HomeWordPress DevelopmentHackerrank-Apple and Orange Resolution in Kotlin

Hackerrank-Apple and Orange Resolution in Kotlin


Sam’s home has an apple tree and an orange tree that yield an abundance of fruit. Utilizing the knowledge given under, decide the variety of apples and oranges that land on Sam’s home.

Within the diagram under:

  • The purple area denotes the home, the place s is the beginning level, and t is the endpoint. The apple tree is to the left of the home, and the orange tree is to its proper.
  • Assume the bushes are situated on a single level, the place the apple tree is at level a, and the orange tree is at level b.
  • When a fruit falls from its tree, it lands d items of distance from its tree of origin alongside the x -axis. *A unfavorable worth of d means the fruit fell d items to the tree’s left, and a optimistic worth of d means it falls d items to the tree’s proper. *

Given the worth of d for m apples and n oranges, decide what number of apples and oranges will fall on Sam’s home (i.e., within the inclusive vary [s,t])?
For instance, Sam’s home is between s=7 and t=10. The apple tree is situated 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] items distance from a, and oranges=[3,-2,-4] items distance. Including every apple distance to the place 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 within the inclusive vary 7-10 so we print

1
2
Enter fullscreen mode

Exit fullscreen mode

Perform Description

Full the countApplesAndOranges perform within the editor under. It ought to print the variety of apples and oranges that land on Sam’s home, every on a separate line.

countApplesAndOranges has the next parameter(s):

  • s: integer, place to begin of Sam’s home location.
  • t: integer, ending location of Sam’s home location.
  • a: integer, location of the Apple tree.
  • b: integer, location of the Orange tree.
  • apples: integer array, distances at which every apple falls from the tree.
  • oranges: integer array, distances at which every orange falls from the tree.

Enter Format

The primary line incorporates two space-separated integers denoting the respective values of s and t.
The second line incorporates two space-separated integers denoting the respective values of a and b.
The third line incorporates two space-separated integers denoting the respective values of m and n.
The fourth line incorporates space-separated integers denoting the respective distances that every apple falls from level a.
The fifth line incorporates n space-separated integers denoting the respective distances that every orange falls from level b.

Constraints

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

Output Format

Print two integers on two totally different traces:

  1. The primary integer: the variety of apples that fall on Sam’s home.
  2. The second integer: the variety of oranges that fall on Sam’s home.

Pattern Enter 0

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

Enter fullscreen mode

Exit fullscreen mode

Pattern Output 0

1
1

Enter fullscreen mode

Exit fullscreen mode

ANSWER:

 enjoyable 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)
 }
Enter fullscreen mode

Exit fullscreen mode

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments