Tasks Details
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
The submission is being evaluated.
hard
1.
MinAbsSum
Given array of integers, find the lowest absolute sum of elements.
Task Score
100%
Correctness
100%
Performance
100%
For a given array A of N integers and a sequence S of N integers from the set {−1, 1}, we define val(A, S) as follows:
val(A, S) = |sum{ A[i]*S[i] for i = 0..N−1 }|
(Assume that the sum of zero elements equals zero.)
For a given array A, we are looking for such a sequence S that minimizes val(A,S).
Write a function:
function solution(A);
that, given an array A of N integers, computes the minimum value of val(A,S) from all possible values of val(A,S) for all possible sequences S of N integers from the set {−1, 1}.
For example, given array:
A[0] = 1 A[1] = 5 A[2] = 2 A[3] = -2your function should return 0, since for S = [−1, 1, −1, 1], val(A, S) = 0, which is the minimum possible value.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..20,000];
- each element of array A is an integer within the range [−100..100].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 69 minutes
Notes
not defined yet
Code: 08:01:28 UTC,
java,
autosave
Code: 08:03:47 UTC,
js,
autosave
Code: 08:04:00 UTC,
js,
autosave
Code: 08:05:30 UTC,
js,
autosave
Code: 08:06:00 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b))
}
Code: 08:06:25 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = A[A.length - 1];
}
Code: 08:07:43 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = MaA[A.length - 1];
}
Code: 08:07:54 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
}
Code: 08:08:48 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
}
Code: 08:09:04 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
for (let i = 0; i <)
}
Code: 08:09:34 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = [];
for (let i = 0; i <= max; i++) {
count
}
}
Code: 08:10:04 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0)
}
Code: 08:10:17 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
}
Code: 08:10:29 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
for (let i = 0)
}
Code: 08:10:48 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
for (let i = 0; i < A.length; i++) {
}
}
Code: 08:10:58 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
for (let i = 0; i < A.length; i++) {
A[]
}
}
Code: 08:12:04 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
for (let i = 0; i < A.length; i++) {
A[i]
}
}
Code: 08:12:34 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total
}
}
Code: 08:12:45 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
}
Code: 08:13:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
}
Code: 08:16:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
}
Code: 08:16:44 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: max + 1 }, () => 0);
}
Code: 08:17:04 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
}
Code: 08:17:46 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
}
Code: 08:22:40 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
retu
}
Code: 08:22:45 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
return 1;
}
The submission is being evaluated.
Code: 08:23:00 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total)
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
return 1;
}
The submission is being evaluated.
Code: 08:34:20 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
return 1;
}
Code: 08:40:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
return 1;
}
Code: 08:40:24 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let)
return 1;
}
Code: 08:41:38 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i )
return 1;
}
Code: 08:42:08 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
}
}
return 1;
}
Code: 08:43:03 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
fo
}
}
return 1;
}
Code: 08:43:13 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j )
}
}
return 1;
}
Code: 08:43:27 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j )
}
}
return 1;
}
Code: 08:43:41 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < total)
}
}
return 1;
}
Code: 08:43:52 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < total;)
}
}
return 1;
}
Code: 08:44:13 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++)
}
}
return 1;
}
Code: 08:44:43 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i]
}
}
}
}
return 1;
}
Code: 08:45:08 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (dp[j])
}
}
}
return 1;
}
Code: 08:45:38 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j]
}
}
}
}
return 1;
}
Code: 08:45:54 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
}
}
}
return 1;
}
Code: 08:52:18 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
}
}
}
return 1;
}
Code: 08:52:29 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
}
}
}
return 1;
}
Code: 08:52:59 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
}
}
}
}
return 1;
}
Code: 08:54:10 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if
}
}
}
}
return 1;
}
Code: 08:54:30 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
}
}
}
}
}
return 1;
}
Code: 08:55:01 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs)
}
}
}
}
}
return 1;
}
Code: 08:55:21 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return 1;
}
Code: 08:56:06 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
ret
return 1;
}
Code: 08:56:16 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:56:39 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:56:42 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
console.log(total);
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
The submission is being evaluated.
Code: 08:57:09 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, ')
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:57:21 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, dp:')
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:57:31 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, dp:', total, target, dp)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:57:42 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, dp:', total, target, dp)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
co
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:57:53 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, dp:', total, target, dp)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('')
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:58:05 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('')
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:58:15 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dp:')
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:58:25 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dp:', i, j, dp)
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:58:36 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 08:59:22 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
The submission is being evaluated.
Code: 08:59:49 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
co
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 09:00:09 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
The submission is being evaluated.
Code: 09:01:34 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j))
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 09:01:44 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
return 1;
}
Code: 09:02:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
Code: 09:07:02 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
Code: 09:07:19 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
Code: 09:07:28 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
The submission is being evaluated.
Code: 09:07:42 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
Code: 09:07:48 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
The submission is being evaluated.
Code: 09:08:27 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
// } if (A.length === 1) {
// return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
Code: 09:08:31 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
// } if (A.length === 1) {
// return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
The submission is being evaluated.
Code: 09:08:47 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
Code: 09:09:07 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
Code: 09:09:30 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
The submission is being evaluated.
Code: 09:09:43 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
The submission is being evaluated.
Code: 09:09:46 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 0) {
return 0;
} if (A.length === 1) {
return Math.abs(A[0]);
}
A.sort((a, b) => Math.abs(a) - Math.abs(b));
let max = Math.abs(A[A.length - 1]);
let count = Array.from({ length: max + 1 }, () => 0);
let total = 0;
for (let i = 0; i < A.length; i++) {
A[i] = Math.abs(A[i]);
count[A[i]]++;
total += A[i];
}
let dp = Array.from({ length: total + 1 }, () => -1);
dp[0] = 0;
const target = total / 2;
let minAbsSum = total;
// console.log('-> total, target, count:', total, target, count)
for (let i = 0; i < count.length; i++) {
if (count[i] > 0) {
for (let j = 0; j < dp.length; j++) {
// console.log('i, j, dpj:', i, j, dp[j])
if (dp[j] >= 0) {
dp[j] = count[i];
} else if (j >= i && dp[j - i] > 0) {
dp[j] = dp[j - i] - 1;
}
// console.log('new dpj:', dp[j])
if (dp[j] >= 0) {
if (j === target) {
return 0;
} else {
minAbsSum = Math.min(minAbsSum, Math.abs(total - 2 * j));
}
}
}
}
}
return minAbsSum;
}
The submission is being evaluated.