Last active
September 2, 2022 13:57
-
-
Save jeanlescure/797eef515cfa4a05830b to your computer and use it in GitHub Desktop.
Codility TapeEquilibrium Solution in Javascript - 100% score
This file contains 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
/* | |
A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. | |
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. | |
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| | |
In other words, it is the absolute difference between the sum of the first part and the sum of the second part. | |
*/ | |
function solution(A) { | |
var sumRight = A.reduce((pv, cv, idx) => (idx > 0) ? (pv + cv) : (0), 0); | |
var sumLeft = 0; | |
var substractions = []; | |
var maxI = A.length - 1; | |
for(var i = 0; i < maxI; i += 1){ | |
sumLeft += A[i]; | |
substractions.push(Math.abs(sumLeft - sumRight)); | |
if (i + 1 <= maxI) sumRight -= A[i + 1]; | |
} | |
return substractions.reduce((pv, cv, idx) => (idx > 0) ? ((pv < cv)? pv : cv) : (cv), 0); | |
} |
sarpisik
commented
Jul 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment