Broad Network


MaxSliceSum at app.codility.com/programmers in PHP 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:
<?php
    function solution($A)  {
        $N = count($A);

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

        for ($i=1; $i < $N; $i++) {
            $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;
    }

    $A = [5, -7, 3, 5, -2, 4, -1];  //  {3, 5, -2, 4} -> 10
    $ret1 = solution($A);
    echo $ret1 . "\n";

    $B = [-2, 1, -3, 4, -1, 2, 1, -5, 4];  //  {4, −1, 2, 1} -> 6
    $ret2 = solution($B);
    echo $ret2 . "\n";

    $C = [3, 2, -6, 4, 0];  //{3, 2} -> 5
    $ret3 = solution($C);
    echo $ret3 . "\n";

    $D = [3, 2, 6, -1, 4, 5, -1, 2];  //{3, 2, 6, -1, 4, 5, -1, 2} -> 20
    $ret4 = solution($D);
    echo $ret4 . "\n";

    $E = [5, 7, -4, -10, -6, 6, 5, 10, -5, 15, 4, -8, -15, -22];  // {6, 5, 10, -5, 15, 4}->35
    $ret5 = solution($E);
    echo $ret5 . "\n";

    $F = [-4, 10, 15, 9, -5, -20, -3, -12, -3, 4, 6, 3, 2, 8, 3, -5, -2];  // {10, 15, 9}->34
    $ret6 = solution($F);
    echo $ret6 . "\n";
?>
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 PHP for the 17 Algorithm Lessons at https://app.codility.com/programmers with Problems and Solutions





Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments