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].
// 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))
}
// 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];
}
// 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];
}
// 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]);
}
// 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]);
}
// 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 <)
}
// 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
}
}
// 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)
}
// 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);
}
// 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)
}
// 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++) {
}
}
// 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[]
}
}
// 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]
}
}
// 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
}
}
// 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];
}
}
// 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];
}
}
// 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];
}
}
// 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);
}
// 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;
}
// 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;
}
// 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
}
// 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;
}
// 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;
}
10
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
-> total, target, count: 10 5 [ 0, 1, 2, 0, 0, 1 ] i, j, dpj: 1 0 0 i, j, dpj: 1 1 -1 i, j, dpj: 1 2 -1 i, j, dpj: 1 3 -1 i, j, dpj: 1 4 -1 i, j, dpj: 1 5 -1 i, j, dpj: 1 6 -1 i, j, dpj: 1 7 -1 i, j, dpj: 1 8 -1 i, j, dpj: 1 9 -1 i, j, dpj: 1 10 -1 i, j, dpj: 2 0 1 i, j, dpj: 2 1 0 i, j, dpj: 2 2 -1 i, j, dpj: 2 3 -1 i, j, dpj: 2 4 -1 i, j, dpj: 2 5 -1
// 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;
}
// 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;
}
-> total, target, count: 10 5 [ 0, 1, 2, 0, 0, 1 ] i, j, dpj: 1 0 0 new dpj: 1 i, j, dpj: 1 1 -1 new dpj: 0 i, j, dpj: 1 2 -1 new dpj: -1 i, j, dpj: 1 3 -1 new dpj: -1 i, j, dpj: 1 4 -1 new dpj: -1 i, j, dpj: 1 5 -1 new dpj: -1 i, j, dpj: 1 6 -1 new dpj: -1 i, j, dpj: 1 7 -1 new dpj: -1 i, j, dpj: 1 8 -1 new dpj: -1 i, j, dpj: 1 9 -1 new dpj: -1 i, j, dpj: 1 10 -1 new dpj: -1 i, j, dpj: 2 0 1 new dpj: 2 i, j, dpj: 2 1 0 new dpj: 2 i, j, dpj: 2 2 -1 new dpj: 1 i, j, dpj: 2 3 -1 new dpj: 1 i, j, dpj: 2 4 -1 new dpj: 0 i, j, dpj: 2 5 -1 new dpj: 0
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
[1, 1, 1, 1, 1]
[]
[-10]
[-100, 1, 100]
-> total, target, count: 10 5 [ 0, 1, 2, 0, 0, 1 ] i, j, dpj: 1 0 0 new dpj: 1 i, j, dpj: 1 1 -1 new dpj: 0 i, j, dpj: 1 2 -1 new dpj: -1 i, j, dpj: 1 3 -1 new dpj: -1 i, j, dpj: 1 4 -1 new dpj: -1 i, j, dpj: 1 5 -1 new dpj: -1 i, j, dpj: 1 6 -1 new dpj: -1 i, j, dpj: 1 7 -1 new dpj: -1 i, j, dpj: 1 8 -1 new dpj: -1 i, j, dpj: 1 9 -1 new dpj: -1 i, j, dpj: 1 10 -1 new dpj: -1 i, j, dpj: 2 0 1 new dpj: 2 i, j, dpj: 2 1 0 new dpj: 2 i, j, dpj: 2 2 -1 new dpj: 1 i, j, dpj: 2 3 -1 new dpj: 1 i, j, dpj: 2 4 -1 new dpj: 0 i, j, dpj: 2 5 -1 new dpj: 0
function result: 1
-> total, target, count: 5 2.5 [ 0, 5 ] i, j, dpj: 1 0 0 new dpj: 5 i, j, dpj: 1 1 -1 new dpj: 4 i, j, dpj: 1 2 -1 new dpj: 3 i, j, dpj: 1 3 -1 new dpj: 2 i, j, dpj: 1 4 -1 new dpj: 1 i, j, dpj: 1 5 -1 new dpj: 0
function result: 0
function result: 10
function result: 1
-> total, target, count: 201 100.5 [ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... 1 more item ] i, j, dpj: 1 0 0 new dpj: 1 i, j, dpj: 1 1 -1 new dpj: 0 i, j, dpj: 1 2 -1 new dpj: -1 i, j, dpj: 1 3 -1 new dpj: -1 i, j, dpj: 1 4 -1 new dpj: -1 i, j, dpj: 1 5 -1 new dpj: -1 i, j, dpj: 1 6 -1 new dpj: -1 i, j, dpj: 1 7 -1 new dpj: -1 i, j, dpj: 1 8 -1 new dpj: -1 i, j, dpj: 1 9 -1 new dpj: -1 i, j, dpj: 1 10 -1 new dpj: -1 i, j, dpj: 1 11 -1 new dpj: -1 i, j, dpj: 1 12 -1 new dpj: -1 i, j, dpj: 1 13 -1 new dpj: -1 i, j, dpj: 1 14 -1 new dpj: -1 i, j, dpj: 1 15 -1 new dpj: -1 i, j, dpj: 1 16 -1 new dpj: -1 i, j, dpj: 1 17 -1 new dpj: -1 i, j, dpj: 1 18 -1 new dpj: -1 i, j, dpj: 1 19 -1 new dpj: -1 i, j, dpj: 1 20 -1 new dpj: -1 i, j, dpj: 1 21 -1 new dpj: -1 i, j, dpj: 1 22 -1 new dpj: -1 i, j, dpj: 1 23 -1 new dpj: -1 i, j, dpj: 1 24 -1 new dpj: -1 i, j, dpj: 1 25 -1 new dpj: -1 i, j, dpj: 1 26 -1 new dpj: -1 i, j, dpj: 1 27 -1 new dpj: -1 i, j, dpj: 1 28 -1 new dpj: -1 i, j, dpj: 1 29 -1 new dpj: -1 i, j, dpj: 1 30 -1 new dpj: -1 i, j, dpj: 1 31 -1 new dpj: -1 i, j, dpj: 1 32 -1 new dpj: -1 i, j, dpj: 1 33 -1 new dpj: -1 i, j, dpj: 1 34 -1 new dpj: -1 i, j, dpj: 1 35 -1 new dpj: -1 i, j, dpj: 1 36 -1 new dpj: -1 i, j, dpj: 1 37 -1 new dpj: -1 i, j, dpj: 1 38 -1 new dpj: -1 i, j, dpj: 1 39 -1 new dpj: -1 i, j, dpj: 1 40 -1 new dpj: -1 i, j, dpj: 1 41 -1 new dpj: -1 i, j, dpj: 1 42 -1 new dpj: -1 i, j, dpj: 1 43 -1 new dpj: -1 i, j, dpj: 1 44 -1 new dpj: -1 i, j, dpj: 1 45 -1 new dpj: -1 i, j, dpj: 1 46 -1 new dpj: -1 i, j, dpj: 1 47 -1 new dpj: -1 i, j, dpj: 1 48 -1
// 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;
}
// 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;
}
[1, 1, 1, 1, 1]
[]
[-10]
[-100, 1, 100]
function result: 1
function result: 0
function result: 10
function result: 1
// 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;
}
// 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;
}
[1, 1, 1, 1, 1]
[]
[-10]
[-100, 1, 100]
function result: 1
function result: 0
function result: 10
function result: 1
// 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;
}
// 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;
}
// 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;
}
[1, 1, 1, 1, 1]
[]
[-10]
[-100, 1, 100]
[1, 1, 1, 1, 1, 1]
[-1, -1, -1, -1, -1, -1]
[-1, 1, -1, 1, -1, 1]
function result: 1
function result: 0
function result: 10
function result: 1
function result: 0
function result: 0
function result: 0
// 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;
}
[1, 1, 1, 1, 1]
[]
[-10]
[-100, 1, 100]
[1, 1, 1, 1, 1, 1]
[-1, -1, -1, -1, -1, -1]
[-1, 1, -1, 1, -1, 1]
function result: 1
function result: 0
function result: 10
function result: 1
function result: 0
function result: 0
function result: 0
// 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 solution obtained perfect score.