Created
May 30, 2019 02:57
-
-
Save cdmunoz/ee09b3c6bec55a0b25f0c4fb7186a8f2 to your computer and use it in GitHub Desktop.
Hackerrank's Sherlock and Array
https://www.hackerrank.com/challenges/sherlock-and-array
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
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