Created
September 23, 2018 19:43
-
-
Save ylegall/f15aef26e8d5afcbbe07e50989761b62 to your computer and use it in GitHub Desktop.
hackerrank easy puzzle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream | |
import java.util.* | |
// https://www.hackerrank.com/challenges/picking-numbers/problem | |
// Complete the pickingNumbers function below. | |
fun pickingNumbers(a: Array<Int>): Int { | |
val counts = a.groupingBy{ it }.eachCount() | |
val entries = counts.entries.asSequence().sortedBy { e -> e.key }.toList() | |
var maxLen = 0 | |
for (i in 1 until entries.size) { | |
maxLen = Math.max(maxLen, entries[i].value) | |
if (entries[i].key - entries[i-1].key <= 1) { | |
val len = entries[i].value + entries[i-1].value | |
maxLen = Math.max(maxLen, len) | |
} | |
} | |
return maxLen | |
} | |
fun main(args: Array<String>) { | |
FileInputStream(args[0]).use { stream -> | |
val scan = Scanner(stream) | |
val n = scan.nextLine() | |
val a = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray() | |
val result = pickingNumbers(a) | |
println(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment