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 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
/* | |
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); | |
} |
sivsivsree
commented
Nov 29, 2018
function solution(A) {
let prevSum = A[0];
let nextSum = A.slice(1, A.length).reduce((a, b) => {
return a + b;
});
let min = Math.abs(nextSum - prevSum);
for (let i = 1; i < A.length - 1; i++) {
prevSum += A[i];
nextSum -= A[i];
if (Math.abs(prevSum - nextSum) < min) {
min = Math.abs(prevSum - nextSum);
}
}
return min;
}
function solution(A) {
let leftNum = 0;
let rightNum = A.reduce((a,b) => a+b);
let answer = null;
for (let i=0; i<A.length-1; i++) {
leftNum += A[i];
rightNum -= A[i];
const diff = Math.abs(leftNum - rightNum)
if (answer === null || answer > diff) {
answer = diff
}
}
return answer
}
function solution(A) {
const arr = [...A];
let solution = Infinity
let contR = 0;
let contL = 0
function sumTotal(arr){
arr.map(item => {contL += item})
return contL
}
sumTotal(arr)
for (var i = 0; i < arr.length - 1; i++){
contR += arr[i]
contL -= arr[i]
if(solution > Math.abs(contL - contR)){
solution = Math.abs(contL - contR)
}
}
return solution
}
function solution(A) {
let sumA = A.reduce((a, b) => a + b, 0)
let sumB = 0
let diff = []
for(let i=0; i<A.length-1; i++){
sumB += A[i]
sumA -= A[i]
diff[i] = Math.abs(sumA-sumB)
}
D = diff.sort((a,b)=>a-b,0)
return D[0];
}
function solution(A) {
const sum = A.reduce((s, i) => s + i, 0)
let min = Number.MAX_VALUE
let half_sum = 0
for(let i = 0; i < A.length - 1; i++) {
half_sum += A[i]
min = Math.min(min, Math.abs(sum - (2 * half_sum)))
}
return min
}
function solution(A) {
const total = A.reduce((acc, number) => acc + number, 0)
let sum1 = 0;
let sum2 = total;
let number;
for (i= 0; i < A.length - 1; i++) {
sum1 += A[i]
sum2 -= A[i]
const value = Math.abs(sum1 - sum2)
if (number === undefined || value < number) {
number = value
}
}
return number
}
`
function solution(A) {
if (A.length === 2) {
return Math.abs(A[0] - A[1])
}
let lowest = Number.MAX_VALUE
let totalSumRight = A.reduce((s, c, idx)=>s+c, 0)
let totalSumLeft = 0
for (let i = 0; i < A.length-1; i++) {
totalSumLeft = totalSumLeft + A[i]
const ltotalSumRight = totalSumRight - totalSumLeft
const abDiff = Math.abs(totalSumLeft - ltotalSumRight)
if (abDiff < lowest) {
lowest = abDiff
}
}
return lowest
}
`
function solution(A) {
let sumLeft = A[0];
let sumRight = A.reduceRight(
(sum, current, i) => (i === 0 ? sum : sum + current),
0
);
let result = getDiff(sumLeft, sumRight);
for (let i = 1; i < A.length - 1; i++) {
const current = A[i];
sumRight -= current;
sumLeft += current;
const diff = getDiff(sumLeft, sumRight);
result = result > diff ? diff : result;
}
return result;
}
function getDiff(num1, num2) {
return Math.abs(num1 - num2);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment