Broad Network


MaxSliceSum at app.codility.com/programmers in JavaScript Explained: Find a maximum sum of a compact subsequence of array elements.

Task score : 100% - Correctness : 100% ; Performance : 100%
Detected time complexity : O(N)
Lesson 9 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: 30 Jul 2025

Problem

A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].

Write a function:
    function solution(A);
that, given an array A consisting of N integers, returns the maximum sum of any slice of A.

For example, given array A such that:

A[0] = 3  A[1] = 2  A[2] = -6
A[3] = 4  A[4] = 0

the function should return 5 because:
<script type='text/javascript'>
    "use strict";

    function solution(A)  {
        let N = A.length;

        let prevMaxSum = A[0];
        let accumulatingMaxSum = A[0];

        for (let i=1; i < N; i++) {
            let tempRunTotal = accumulatingMaxSum + A[i];  //could be smaller than A[i]
            if (A[i] > tempRunTotal)
                accumulatingMaxSum = A[i];  //elements start rising
            else
                accumulatingMaxSum = tempRunTotal;  //continue to accumulate

            if (accumulatingMaxSum > prevMaxSum)  //comparing all the maximum sums
                prevMaxSum = accumulatingMaxSum;
        }

        return prevMaxSum;
    }

    const A = [5, -7, 3, 5, -2, 4, -1];  //  {3, 5, -2, 4} -> 10
    let ret1 = solution(A);
    document.write(ret1 + '<br>');

    const B = [-2, 1, -3, 4, -1, 2, 1, -5, 4];  //  {4, −1, 2, 1} -> 6
    let ret2 = solution(B);
    document.write(ret2 + '<br>');

    const C = [3, 2, -6, 4, 0];  //{3, 2} -> 5
    let ret3 = solution(C);
    document.write(ret3 + '<br>');

    const D = [3, 2, 6, -1, 4, 5, -1, 2];  //{3, 2, 6, -1, 4, 5, -1, 2} -> 20
    let ret4 = solution(D);
    document.write(ret4 + '<br>');

    const E = [5, 7, -4, -10, -6, 6, 5, 10, -5, 15, 4, -8, -15, -22];  // {6, 5, 10, -5, 15, 4}->35
    let ret5 = solution(E);
    document.write(ret5 + '<br>');

    const F = [-4, 10, 15, 9, -5, -20, -3, -12, -3, 4, 6, 3, 2, 8, 3, -5, -2];  // {10, 15, 9}->34
    let ret6 = solution(F);
    document.write(ret6 + '<br>');
</script>
The output is:

    10
    6
    5
    20
    35
    34

At app.codility.com/programmers the scoring and detected time complexity for this solution() is:

    Task score : 100% -  Correctness : 100% ; Performance : 100%
    Detected time complexity : O(N)

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 NEXT

Comments