Broad Network


MinAbsSum at app.codility.com/programmers in JavaScript Explained: Given array of integers, find the lowest absolute sum of elements.

Task score : 100% - Correctness : 100% ; Performance : 100%
Detected time complexity :
Lesson 17 at app.codility.com/programmers
The solution and its explanation are given below.

Category of Article : Technology | Computers and Software | Algorithm

By: Chrysanthus Date Published: 4 Oct 2025

Problem

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] = -2 

your 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]. 

Note

| . . . | means take the positive value of what is inside.

In order to better understand the problem, three different sets of S are used for illustration, as follows: 

A = [1, 5, 2, -2] and S = [-1, +1, -1, +1]

val(A, S) = |sum{ A[i]*S[i] for i = 0..N−1 }| = |1x(-1) + 5x(+1) + 2x(-1) + (-2)x(+1)|

= |(-1) + (+5) + (-2) + (-2)| = |-1 + 5 – 2 – 2| = |0| = 0 .

Note that the given set for S = [-1, +1, -1, +1] is just one of 2N sets; other two sets of which are as follows: 

A = [1, 5, 2, -2] and S = [1, 1, 1, 1]

val(A, S) = |sum{ A[i]*S[i] for i = 0..N−1 }| = |1x1 + 5x1 + 2x1 + -2x1| = |1 + 5 + 2 – 2| = |+6| = 6

A = [1, 5, 2, -2] and S = [-1, -1, +1, -1]

val(A, S) = |sum{ A[i]*S[i] for i = 0..N−1 }| = |1x-1 + 5x-1 + 2x+1 + -2x-1| = |-1 + -5 + +2 ++ 2| = |-1 - 5 + 2 + 2| = |-2| =  2

The three answers here are 0, 6 and 2 and the minimum is 0. Zero does not necessarily have to be the minimum for other similar problems (tasks). The minimum can still be any number higher than 0 (for other problems).

In the problem description, it is said, “(Assume that the sum of zero elements equals zero.)”. So when N is 0 for an empty set, val(A, S) = 0. 

Problem Interpretation

Using the given example, the question is, what is the minimum absolute sum of a given set of numbers, with all the different possible combinations of negative and positive signs, for the numbers. From above, the given set is the A = [1, 5, 2, -2]. Now {−1, 1, −1, 1} is one set of positive and negative signs. {1, 1, 1, 1} is another set of positive (and negative signs/zero-negative). {−1, -1, −1, -1} is yet another set of (positive and) negative signs.

If the set of signs is {−1, 1, −1, 1}, then the absolute minimum sum is |-1 + 5 – 2 – 2| = |0| = 0. If the set of signs is {1, 1, 1, 1}, then the absolute minimum sum is |1 + 5 + 2 – 2| = |+6| = 6. If the set of signs is {-1, -1, 1, -1}, then the absolute minimum sum is |-1 - 5 + 2 + 2| = |-2| = 2. 

Illustration

The given example is used for illustration. Let the + sign of +1 be represented by the bit (Binary Digit) of 1. Let the negative sign of -1 be represented by the bit (Binary Digit) of 0. So the set {−1, 1, −1, 1} becomes the binary number 0101 which is the decimal number 510. The set {1, 1, 1, 1}} is the binary number 1111 which is the decimal number 1510. The set {-1, -1, 1, -1} is the binary number 0010 which is the decimal number 210.    

So the different decimal values for k are 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15. In binary, these numbers are: 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, with each considered from the right end. If there are N elements in the given set, then there are N (or N+1 which includes the empty set) sets, consisting of 1’s and/or 0’s – see table below. Remember that each value for k represents a sub-task. The different possible sub-task results, are actually from the different combinations of k and the indexes from 0 to N. Each combination has a result. dp stands for dynamic programming. 

So, 010 means - , considered as - - - -. 110 means + , considered as - - - +. 210 means +,- , considered as - - + -. 310 means +,+ , considered as - - + +. 410 means +,-,- considered as - + - -. 810 means +,-,-,- , considered as + - - -; and so on.

Different Examples

Well, the following tables show the results for different sets: 

The given set is {1, 5, 2, -2}.

Index

signs

set

summing

sum

abs(sum)

0

-1

-1

-1

-1

1, 5, 2, -2

-1-5-2+2

-6

6

1

-1

-1

-1

+1

1, 5, 2, -2

-1-5-2-2

-10

10

2

-1

-1

+1

-1

1, 5, 2, -2

-1-5+2+2

-2

2

3

-1

-1

+1

+1

1, 5, 2, -2

-1-5+2-2

-6

6

4

-1

+1

-1

-1

1, 5, 2, -2

-1+5-2+2

+4

4

5

-1

+1

-1

+1

1, 5, 2, -2

-1+5-2-2

+0

0

6

-1

+1

+1

-1

1, 5, 2, -2

-1+5+2+2

+8

8

7

-1

+1

+1

+1

1, 5, 2, -2

-1+5+2-2

+4

4

8

+1

-1

-1

-1

1, 5, 2, -2

+1-5-2+2

-4

4

9

+1

-1

-1

+1

1, 5, 2, -2

+1-5-2-2

-8

8

10

+1

-1

+1

-1

1, 5, 2, -2

+1-5+2+2

-0

0

11

+1

-1

+1

+1

1, 5, 2, -2

+1-5+2-2

-4

4

12

+1

+1

-1

-1

1, 5, 2, -2

+1+5-2+2

+6

6

13

+1

+1

-1

+1

1, 5, 2, -2

+1+5-2-2

+2

2

14

+1

+1

+1

-1

1, 5, 2, -2

+1+5+2+2

+10

10

15

+1

+1

+1

+1

1, 5, 2, -2

+1+5+2-2

+6

6

For the set {1, 5, 2, -2}, the minimum abs(sum) of 0 is obtained at index 5 with -1,+1,-1,+1 (0101) or at index 10 with +1,-1,+1,-1 (1010).

The given set is {1, -4, 2}.

Index

Signs

set

summing

sum

abs(sum)

0

-1

-1

-1

1, -4, 2

-1+4-2

+1

1

1

-1

-1

+1

1, -4, 2

-1+4+2

+5

5

2

-1

+1

-1

1, -4, 2

-1-4-2

-7

7

3

-1

+1

+1

1, -4, 2

-1-4+2

-3

3

4

+1

-1

-1

1, -4, 2

+1+4-2

+3

3

5

+1

-1

+1

1, -4, 2

+1+4+2

+7

7

6

+1

+1

-1

1, -4, 2

+1-4-2

-5

5

7

+1

+1

+1

1, -4, 2

+1-4+2

-1

1

For the set {1, -4, 2}, the minimum abs(sum) of 1 is obtained at index 0 with -1,-1,-1 (000) or at index 7 with +1,+1,+1 (111). 

The given set is {2, -5}.

Index

signs

set

summing

sum

abs(sum)

0

-1

-1

2, -5

-2+5

+3

3

1

-1

+1

2, -5

-2-5

-7

7

2

+1

-1

2, -5

+2+5

+7

7

3

+1

+1

2, -5

+2-5

-3

3

For the set {2, -5}, the minimum abs(sum) of 3 is obtained at index 0 with -1,-1, (00) or at index 3 with +1,+1 (11).

The given set is {6}.

Index

signs

Set

summing

sum

abs(sum)

0

-1

6

-6

-6

6

1

+1

6

+6

+6

6

For the set {6}, the minimum abs(sum) of 6 is obtained at index 0 with -1 (0) or at index 1 with +1 (1).

The Program

In the code, conversion of the index k from base 10 to base 2 is used. 

The pow() function for power is in the math library. So the math.h header has to be included. To evaluate 2N, do

    ret = pow(2, N) 

The abs() function for absolute, is in the cstdlib library. So the cstdlib header has to be included.

The assumptions in the program description are:

 

·         N is an integer within the range [0..20,000];

·         each element of array A is an integer within the range [−100..100]. 

So the minimum absolute sum cannot exceed 20000 * 100 + 1 = 2000001 (could still be 2000000)

The program is (read the comments as well): 

<script type='text/javascript'>
    "use strict";

    function solution(A) {
        let N = A.length;
        let pw = Math.pow(2,N);
        let minAbsSum = 20000 * 100 + 1;

        if (N==0) return 0;
    
        //code for binary content
        for (let k=0; k < pw; k++) {    //converting each number in the range K, to base 2 number
            let no = k;                 //as well as looking for minimum |sum{ A[i]*S[i] for i = 0..N−1 }|
            let j = N-1;
            let sum = 0;
            while (j >= 0) {    //the number bits
                let remainder = no % 2;
                if (remainder == 0)
                    sum = sum + (-1 * A[j]);
                if (remainder == 1)
                    sum = sum + (+1 * A[j]);   

                if (j == 0) 
                    break;
            
                no = parseInt(no / 2);    //integer division
                j--;
            }

            sum = Math.abs(sum);

            if (sum < minAbsSum)
                minAbsSum = sum;
        }

        return minAbsSum;
    }

    const A = [1, 5, 2, -2];
    let ret1 = solution(A);
    document.write(ret1 + '<br>');

    const B = [1, -4, 2];
    let ret2 = solution(B);
    document.write(ret2 + '<br>');

    const C = [2, -5];
    let ret3 = solution(C);
    document.write(ret3 + '<br>');
    
    const D = [6];
    let ret4 = solution(D);
    document.write(ret4 + '<br>');

    const E = [10, 20, 30, 5];
    let ret5 = solution(E);
    document.write(ret5 + '<br>');

    const F = [];
    let ret6 = solution(F);
    document.write(ret6 + '<br>');
 
</script>

Output is: 


0
1
3
6
5
0

At app.codility.com/programmers the scoring for this solution(), using the particular test cases at app.codility.com/programmers, is:

    Task score : 54% -  Correctness : 100% ; Performance : 0%

Analysis summary 

Example tests

example1
example test

OK

Correctness tests

simple1
simple 1

OK

simple2
simple 2

OK

simple3
simple 3

OK

range
range 2..20

OK

extreme
empty and single element

OK

functional
small functional test

OK

 Performance tests

medium1
medium random

1. 0.004 s WRONG ANSWER, got 2000001 expected 0

2. 0.003 s OK

 

WRONG ANSWER
got 2000001 expected 0

 

medium2
multiples of 10 + 5

1. 0.004 s WRONG ANSWER, got 2000001 expected 5

2. 0.003 s OK

 

WRONG ANSWER
got 2000001 expected 5

 

big1
multiples of 5 + 42

1. 0.004 s WRONG ANSWER, got 2000001 expected 2

2. 0.003 s OK

 

WRONG ANSWER
got 2000001 expected 2

 

big3
all 4s and one 3

1. 0.004 s WRONG ANSWER, got 2000001 expected 3

 

WRONG ANSWER
got 2000001 expected 3

 

big4
multiples of 10

1. 0.004 s WRONG ANSWER, got 2000001 expected10

2. 0.004 s OK

 

WRONG ANSWER
got 2000001 expected 10

 

Conclusion

The author believes that all the answers on performance by the examiner are wrong assessments, for the following reasons: 

-          No timing complaint has been made. In fact, the timings for some of them are OK.

-          Take for example, the case of medium2. The examiner's expected answer is 5 (got 2000001) and the author's equivalent answer is also 5, for his program above.

-          All the examiner's answers have "got 2000001". Does that make sense?

So the scoring for the author should have been: 

    Task score : 100% -  Correctness : 100% ; Performance : 100%

Thanks for reading

The rest of the 17 lessons, with explanation of lessons, explanation of tasks, and explanation of solutions, can be found at (click): Explanations in JavaScript for the 17 Algorithm Lessons at https://app.codility.com/programmers with Problems and Solutions





Related Links

More Related Links

Cousins

BACK

Comments