Breaking the Records solution in Kotlin -HackerRank

Umar Syaid Himawan
2 min readNov 12, 2020

Print two space-seperated integers describing the respective numbers of times her best (highest) score increased and her worst (lowest) score decreased.

Problem Story

Maria plays games of college basketball in a season. Because she wants to go pro, she tracks her points scored per game sequentially in an array defined as score = [ 80, 81, ………, 8n-1]. After each game , she checks to see if score Si breaks her record for most or least points scored so far during that season.

Given Maria’s array of scores for a season of n games, find and print the number of times she breaks her record for most and least points scored during the season.

Note

Assume her records for most and least points at the start of the season are the number of points scored during the first game of the season.

Input Format

The first line contains an integer denoting n (the number of games). The second line contains n space-separated integers describing the respective values of 80, 81, ………, 8n-1,

Output Format

Print two space-seperated integers describing the respective numbers of times her best (highest) score increased and her worst (lowest) score decreased.

Sample Input

9 
10 5 20 20 4 5 2 25 1

Sample Output

24

ANSWER


fun breakingRecords(scores: Array<Int>): Array<Int> {
var maxTotal = 0
var minTotal = 0
var max = 0
var min = 0

for (x in 0 until scores.size) {
val it = scores[x]
if (x != 0) {
if (it > max) {
max = it
maxTotal++
}
else if (it < min) {
min = it
minTotal++
}

} else {
max = it
min = it
}
}
return listOf(maxTotal, minTotal).toTypedArray()}

Explanation

The diagram below depicts the number of times Maria broke her best and worst records throughout the season:

She broke her best record twice (after games 2 and 7) and her worst record four times (after games 1, 4, 6, and 8), so we print 2 4 as our answer. Note that she did not break her record for best score during game , as her score during that game was not strictly greater than her best record at the time

--

--