Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created May 30, 2019 02:57
Show Gist options
  • Save cdmunoz/ee09b3c6bec55a0b25f0c4fb7186a8f2 to your computer and use it in GitHub Desktop.
Save cdmunoz/ee09b3c6bec55a0b25f0c4fb7186a8f2 to your computer and use it in GitHub Desktop.
package hakerrank
import kotlin.collections.*
import kotlin.io.*
import kotlin.text.*
/**
* Hackerrank's Sherlock and Array
* https://www.hackerrank.com/challenges/sherlock-and-array
*
*/
// Complete the balancedSums function below.
fun balancedSums(arr: Array<Int>): String {
var positionLeft = 0
var positionRight = arr.lastIndex
var sum = 0
while (positionLeft != positionRight) {
if (sum >= 0) {
sum -= arr[positionRight]
positionRight--
} else {
sum += arr[positionLeft]
positionLeft++
}
}
return if (sum == 0) "YES" else "NO"
}
fun main(args: Array<String>) {
val T = readLine()!!.trim().toInt()
for (TItr in 1..T) {
val n = readLine()!!.trim().toInt()
val arr = readLine()!!.trimEnd().split(" ").map { it.toInt() }.toTypedArray()
val result = balancedSums(arr)
println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment