Your browser is not supported. Please, update your browser or switch to a different one. Learn more about which browsers are supported.
Task description
You are given an array A consisting of the integers −1, 0 and 1. A slice of that array is any pair of integers (P, Q) such that 0 ≤ P ≤ Q < N. Your task is to find the longest slice of A whose elements yield a non-negative sum.
Write a function:
function solution($A);
that, given an array A of length N, consisting only of the values −1, 0, 1, returns the length of the longest slice of A that yields a non-negative sum. If there's no such slice, your function should return 0.
For example, given A = [−1, −1, 1, −1, 1, 0, 1, −1, −1], your function should return 7, as the slice starting at the second position and ending at the eighth is the longest slice with a non-negative sum.
For another example, given A = [1, 1, −1, −1, −1, −1, −1, 1, 1] your function should return 4: both the first four elements and the last four elements of array A are longest valid slices.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of array A is an integer within the range [−1..1].
Task timeline
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
for($i = 1; $i < count($A); $i++) {
// Loop through lengths;
$pos = 0;
while(($pos + 1) < (count($A) - $i)) {
// Calculate the slice
$new_sum = array_sum(array_slice($A,$pos,$i));
if($new_sum > $sum) {
$sum = $new_sum;
$len = $i;
}
}
}
return $len;
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
for($i = 1; $i < count($A); $i++) {
// Loop through lengths;
$pos = 0;
while(($pos + 1) < (count($A) - $i)) {
// Calculate the slice
$new_sum = array_sum(array_slice($A,$pos,$i));
if($new_sum > $sum) {
$sum = $new_sum;
$len = $i;
}
}
break;
}
return $len;
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
for($i = 1; $i < count($A); $i++) {
// Loop through lengths;
$pos = 0;
while(($pos + 1) < (count($A) - $i)) {
// Calculate the slice
$new_sum = array_sum(array_slice($A,$pos,$i));
if($new_sum > $sum) {
$sum = $new_sum;
$len = $i;
}
break;
}
break;
}
return $len;
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
$tally = 0;
$start = 0;
$end = 0;
foreach($A as $k => $v) {
$tally += $v;
if($tally < 0) {
$start = $k;
$tally = 0;
$end = $k;
} else {
$end = $k;
}
}
return $end - $start;
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
$tally = 0;
$start = 0;
$end = 0;
foreach($A as $k => $v) {
$tally += $v;
if($tally < 0) {
$start = $k;
$tally = 0;
$end = $k;
} else {
$end = $k;
}
}
print "$start : $end";
return $end - $start;
}
1 : 8
6 : 8
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
$tally = 0;
$start = 0;
$end = 0;
foreach($A as $k => $v) {
$tally += $v;
if($tally < 0) {
$start = $k;
$tally = 0;
$end = $k;
} else {
$end = $k;
}
}
print "$start : $end";
return $end - $start;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
1 : 8
6 : 8
function result: 9
0 : 9
function result: 3
0 : 3
function result: 0
3 : 3
function result: 4
0 : 4
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
$tally = 0;
$start = 0;
$end = 0;
foreach($A as $k => $v) {
$tally += $v;
if($tally < 0) {
$start = $k;
$tally = 0;
$end = $k;
} else {
$end = $k;
}
}
print "$start : $end";
return $end - $start;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
1 : 8
6 : 8
function result: 9
0 : 9
function result: 3
0 : 3
function result: 0
3 : 3
function result: 4
0 : 4
function result: 5
3 : 8
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$end = 0;
$max = 0;
foreach($A as $k => $v) {
// Start a search from each position in the array
$end = max($v, $end);
$max = max($max, $end);
}
print "$end : $max";
return $max;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
1 : 1
1 : 1
function result: 1
1 : 1
function result: 0
0 : 0
function result: 0
0 : 0
function result: 0
0 : 0
function result: 1
1 : 1
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$end = 0;
$max = 0;
$max_values = [];
foreach($A as $k => $v) {
// Start a search from each position in the array
$max = $max + $v;
if($max < 0) $max = 0;
if($max > $end) $end = $max
}
return $end;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
PHP Parse error: syntax error, unexpected '}' in func.php on line 16 Parse error: syntax error, unexpected '}' in func.php on line 16
PHP Parse error: syntax error, unexpected '}' in func.php on line 16 Parse error: syntax error, unexpected '}' in func.php on line 16
PHP Parse error: syntax error, unexpected '}' in func.php on line 16 Parse error: syntax error, unexpected '}' in func.php on line 16
PHP Parse error: syntax error, unexpected '}' in func.php on line 16 Parse error: syntax error, unexpected '}' in func.php on line 16
PHP Parse error: syntax error, unexpected '}' in func.php on line 16 Parse error: syntax error, unexpected '}' in func.php on line 16
PHP Parse error: syntax error, unexpected '}' in func.php on line 16 Parse error: syntax error, unexpected '}' in func.php on line 16
PHP Parse error: syntax error, unexpected '}' in func.php on line 16 Parse error: syntax error, unexpected '}' in func.php on line 16
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$end = 0;
$max = 0;
$max_values = [];
foreach($A as $k => $v) {
// Start a search from each position in the array
$max = $max + $v;
if($max < 0) $max = 0;
if($max > $end) $end = $max;
}
return $end;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sum = array_sum($A);
$len = count($A);
$tally = 0;
$start = 0;
$end = 0;
foreach($A as $k => $v) {
// Start a search from each position in the array
$key = $k;
$sum = $v;
}
print "$start : $end";
return $end - $start;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
0 : 0
0 : 0
function result: 0
0 : 0
function result: 0
0 : 0
function result: 0
0 : 0
function result: 0
0 : 0
function result: 0
0 : 0
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while $sub_sum > 0 {
$sub_sum += $A[$sub_start];
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
PHP Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16 Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16
PHP Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16 Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16
PHP Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16 Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16
PHP Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16 Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16
PHP Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16 Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16
PHP Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16 Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16
PHP Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16 Parse error: syntax error, unexpected '$sub_sum' (T_VARIABLE), expecting '(' in func.php on line 16
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum > 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
PHP Notice: Undefined offset: -1 in func.php on line 17 Notice: Undefined offset: -1 in func.php on line 17 PHP Notice: Undefined offset: -2 in func.php on line 17 Notice: Undefined offset: -2 in func.php on line 17 PHP Notice: Undefined offset: -3 in func.php on line 17 Notice: Undefined offset: -3 in func.php on line 17 PHP Notice: Undefined offset: -4 in func.php on line 17 Notice: Undefined offset: -4 in func.php on line 17 PHP Notice: Undefined offset: -5 in func.php on line 17 Notice: Undefined offset: -5 in func.php on line 17 PHP Notice: Undefined offset: -6 in func.php on line 17 Notice: Undefined offset: -6 in func.php on line 17 PHP Notice: Undefined offset: -7 in func.php on line 17 Notice: Undefined offset: -7 in func.php on line 17 PHP Notice: Undefined offset: -8 in func.php on line 17 Notice: Undefined offset: -8 in func.php on line 17 PHP Notice: Undefined offset: -9 in func.php on line 17 Notice: Undefined offset: -9 in func.php on line 17 PHP Notice: Undefined offset: -10 in func.php on line 17 Notice: Undefined offset: -10 in func.php on line 17 PHP Notice: Undefined offset: -11 in func.php on line 17 Notice: Undefined offset: -11 in func.php on line 17 PHP Notice: Undefined offset: -12 in func.php on line 17 Notice: Undefined offset: -12 in func.php on line 17 PHP Notice: Undefined offset: -13 in func.php on line 17 Notice: Undefined offset: -13 in func.php on line 17 PHP Notice: Undefined offset: -14 in func.php on line 17 Notice: Undefined offset: -14 in func.php on line 17 PHP Notice: Undefined offset: -15 in func.php on line 17 Notice: Undefined offset: -15 in func.php on line 17 PHP Notice: Undefined offset: -16 in func.php on line 17 Notice: Undefined offset: -16 in func.php on line 17 PHP Notice: Undefined offset: -17 in func.php on line 17 Notice: Undefined offset: -17 in func.php on line 17 PHP Notic
PHP Notice: Undefined offset: -1 in func.php on line 17 Notice: Undefined offset: -1 in func.php on line 17 PHP Notice: Undefined offset: -2 in func.php on line 17 Notice: Undefined offset: -2 in func.php on line 17 PHP Notice: Undefined offset: -3 in func.php on line 17 Notice: Undefined offset: -3 in func.php on line 17 PHP Notice: Undefined offset: -4 in func.php on line 17 Notice: Undefined offset: -4 in func.php on line 17 PHP Notice: Undefined offset: -5 in func.php on line 17 Notice: Undefined offset: -5 in func.php on line 17 PHP Notice: Undefined offset: -6 in func.php on line 17 Notice: Undefined offset: -6 in func.php on line 17 PHP Notice: Undefined offset: -7 in func.php on line 17 Notice: Undefined offset: -7 in func.php on line 17 PHP Notice: Undefined offset: -8 in func.php on line 17 Notice: Undefined offset: -8 in func.php on line 17 PHP Notice: Undefined offset: -9 in func.php on line 17 Notice: Undefined offset: -9 in func.php on line 17 PHP Notice: Undefined offset: -10 in func.php on line 17 Notice: Undefined offset: -10 in func.php on line 17 PHP Notice: Undefined offset: -11 in func.php on line 17 Notice: Undefined offset: -11 in func.php on line 17 PHP Notice: Undefined offset: -12 in func.php on line 17 Notice: Undefined offset: -12 in func.php on line 17 PHP Notice: Undefined offset: -13 in func.php on line 17 Notice: Undefined offset: -13 in func.php on line 17 PHP Notice: Undefined offset: -14 in func.php on line 17 Notice: Undefined offset: -14 in func.php on line 17 PHP Notice: Undefined offset: -15 in func.php on line 17 Notice: Undefined offset: -15 in func.php on line 17 PHP Notice: Undefined offset: -16 in func.php on line 17 Notice: Undefined offset: -16 in func.php on line 17 PHP Notice: Undefined offset: -17 in func.php on line 17 Notice: Undefined offset: -17 in func.php on line 17 PHP Notic
function result: 1
function result: 1
function result: 1
PHP Notice: Undefined offset: -1 in func.php on line 17 Notice: Undefined offset: -1 in func.php on line 17 PHP Notice: Undefined offset: -2 in func.php on line 17 Notice: Undefined offset: -2 in func.php on line 17 PHP Notice: Undefined offset: -3 in func.php on line 17 Notice: Undefined offset: -3 in func.php on line 17 PHP Notice: Undefined offset: -4 in func.php on line 17 Notice: Undefined offset: -4 in func.php on line 17 PHP Notice: Undefined offset: -5 in func.php on line 17 Notice: Undefined offset: -5 in func.php on line 17 PHP Notice: Undefined offset: -6 in func.php on line 17 Notice: Undefined offset: -6 in func.php on line 17 PHP Notice: Undefined offset: -7 in func.php on line 17 Notice: Undefined offset: -7 in func.php on line 17 PHP Notice: Undefined offset: -8 in func.php on line 17 Notice: Undefined offset: -8 in func.php on line 17 PHP Notice: Undefined offset: -9 in func.php on line 17 Notice: Undefined offset: -9 in func.php on line 17 PHP Notice: Undefined offset: -10 in func.php on line 17 Notice: Undefined offset: -10 in func.php on line 17 PHP Notice: Undefined offset: -11 in func.php on line 17 Notice: Undefined offset: -11 in func.php on line 17 PHP Notice: Undefined offset: -12 in func.php on line 17 Notice: Undefined offset: -12 in func.php on line 17 PHP Notice: Undefined offset: -13 in func.php on line 17 Notice: Undefined offset: -13 in func.php on line 17 PHP Notice: Undefined offset: -14 in func.php on line 17 Notice: Undefined offset: -14 in func.php on line 17 PHP Notice: Undefined offset: -15 in func.php on line 17 Notice: Undefined offset: -15 in func.php on line 17 PHP Notice: Undefined offset: -16 in func.php on line 17 Notice: Undefined offset: -16 in func.php on line 17 PHP Notice: Undefined offset: -17 in func.php on line 17 Notice: Undefined offset: -17 in func.php on line 17 PHP Notic
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum > 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum >= 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum >= 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
function result: 10
function result: 4
function result: 1
function result: 5
function result: 9
function result: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum >= 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 10
function result: 4
function result: 1
function result: 5
function result: 9
function result: 14
function result: 12
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum >= 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
if($sub_sum < 0)
break;
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 10
function result: 4
function result: 1
function result: 5
function result: 9
function result: 14
function result: 12
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum >= 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 10
function result: 4
function result: 1
function result: 5
function result: 9
function result: 14
function result: 12
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum >= 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
print " -$sub_start";
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
-4 -3 -2 -1 -0 --1
--1 -0 --1 -1 -0 --1 -2 -1 -0 --1 -3 -2 -1 -6 -5 -4
function result: 10
--1 -0 --1 -1 -0 --1 -2 -1 -0 --1 -3 -2 -1 -0 --1 -4 -3 -2 -1 -0 --1 -5 -4 -3 -2 -1 -0 --1 -6 -5 -4 -3 -2 -1 -0 --1 -7 -6 -5 -4 -3 -2 -1 -0 --1
function result: 4
--1 -0 --1 -1 -0 --1
function result: 1
function result: 5
--1 -0 --1 -1 -0 --1 -2 -1 -0 --1
function result: 9
-5 -4 -3 -2 -1 -0 --1 -6 -5 -4 -3 -2 -1 -0 --1
function result: 14
--1 -0 --1 -1 -0 --1 -2 -1 -0 --1 -3 -2 -1 -0 --1 -4 -3 -2 -1 -0 --1 -5 -4 -3 -2 -1 -0 --1 -6 -5 -4 -3 -2 -1 -0 --1 -7 -6 -5 -4 -3 -2 -1 -0 --1 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1
function result: 12
--1 -0 --1 -1 -0 --1 -2 -1 -0 --1 -3 -2 -1 -0 --1 -4 -3 -2 -1 -0 --1 -5 -4 -3 -2 -1 -0 --1 -6 -5 -4 -3 -2 -1 -0 --1 -7 -6 -5 -4 -3 -2 -1 -0 --1 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1 -10 -9 -8 -7 -6 -5 -4 -3 -11 -10 -9 -12 -11 -10 -9 -8
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$sub_start = $k - 1;
while ($sub_sum >= 0 && $sub_start >= 0) {
$sub_sum += $A[$sub_start];
$sub_start--;
print " -$sub_start";
}
print "Start: $sub_start, End: $sub_end";
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
Start: -1, End: 0Start: 0, End: 1Start: 1, End: 2Start: 2, End: 3Start: 3, End: 4Start: 4, End: 5 -4 -3 -2 -1 -0 --1Start: -1, End: 6Start: 6, End: 7Start: 7, End: 8
Start: -1, End: 0 --1Start: -1, End: 1 -0 --1Start: -1, End: 2 -1 -0 --1Start: -1, End: 3 -2 -1 -0 --1Start: -1, End: 4 -3 -2 -1Start: 1, End: 5Start: 5, End: 6Start: 6, End: 7 -6 -5 -4Start: 4, End: 8
function result: 10
Start: -1, End: 0 --1Start: -1, End: 1 -0 --1Start: -1, End: 2 -1 -0 --1Start: -1, End: 3 -2 -1 -0 --1Start: -1, End: 4 -3 -2 -1 -0 --1Start: -1, End: 5 -4 -3 -2 -1 -0 --1Start: -1, End: 6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 9
function result: 4
Start: -1, End: 0 --1Start: -1, End: 1 -0 --1Start: -1, End: 2 -1 -0 --1Start: -1, End: 3
function result: 1
Start: -1, End: 0Start: 0, End: 1Start: 1, End: 2Start: 2, End: 3
function result: 5
Start: -1, End: 0 --1Start: -1, End: 1 -0 --1Start: -1, End: 2 -1 -0 --1Start: -1, End: 3 -2 -1 -0 --1Start: -1, End: 4
function result: 9
Start: -1, End: 0Start: 0, End: 1Start: 1, End: 2Start: 2, End: 3Start: 3, End: 4Start: 4, End: 5Start: 5, End: 6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 8
function result: 14
Start: -1, End: 0 --1Start: -1, End: 1 -0 --1Start: -1, End: 2 -1 -0 --1Start: -1, End: 3 -2 -1 -0 --1Start: -1, End: 4 -3 -2 -1 -0 --1Start: -1, End: 5 -4 -3 -2 -1 -0 --1Start: -1, End: 6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 13
function result: 12
Start: -1, End: 0 --1Start: -1, End: 1 -0 --1Start: -1, End: 2 -1 -0 --1Start: -1, End: 3 -2 -1 -0 --1Start: -1, End: 4 -3 -2 -1 -0 --1Start: -1, End: 5 -4 -3 -2 -1 -0 --1Start: -1, End: 6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -0 --1Start: -1, End: 11 -10 -9 -8 -7 -6 -5 -4 -3Start: 3, End: 12 -11 -10 -9Start: 9, End: 13 -12 -11 -10 -9 -8Start: 8, End: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 9
function result: 3
function result: 1
function result: 4
function result: 1
function result: 8
function result: 8
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
print " $sub_start:$sub_end($sub_sum)";
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
1:2(-2) 2:3(-2) 2:3(-3) 3:4(-3) 3:4(-2) 3:4(-3) 4:5(-2) 4:5(-3) 4:5(-2) 4:5(-3) 5:6(-2) 5:6(-1) 5:6(-2) 5:6(-1) 5:6(-2) 6:7(-2) 6:7(-2) 6:7(-1) 6:7(-2) 6:7(-1) 6:7(-2) 7:8(-4) 7:8(-3) 7:8(-3) 7:8(-2) 7:8(-3) 7:8(-2) 7:8(-3)
0:2(2) 1:3(0) 0:3(1) 3:4(-1) 3:4(-2) 3:4(-1) 4:5(-3) 4:5(-4) 4:5(-5) 4:5(-4) 5:6(-6) 5:6(-7) 5:6(-8) 5:6(-9) 5:6(-8) 6:7(-8) 6:7(-9) 6:7(-10) 6:7(-11) 6:7(-12) 6:7(-11) 7:8(-9) 7:8(-10) 7:8(-11) 7:8(-12) 7:8(-13) 7:8(-14) 7:8(-13)
function result: 9
0:2(4) 1:3(6) 0:3(7) 2:4(9) 1:4(10) 0:4(11) 3:5(13) 2:5(14) 1:5(15) 0:5(16) 4:6(16) 3:6(17) 2:6(18) 1:6(19) 0:6(20) 5:7(18) 4:7(19) 3:7(20) 2:7(21) 1:7(22) 0:7(23) 6:8(21) 5:8(20) 4:8(21) 3:8(22) 2:8(23) 1:8(24) 0:8(25) 7:9(23) 6:9(22) 5:9(21) 4:9(22) 3:9(23) 2:9(24) 1:9(25) 0:9(26)
function result: 3
0:2(0) 1:3(0) 0:3(0)
function result: 1
1:2(-4) 2:3(-6) 2:3(-7)
function result: 4
0:2(0) 1:3(0) 0:3(0) 2:4(0) 1:4(0) 0:4(0)
function result: 1
1:2(-4) 2:3(-6) 2:3(-7) 3:4(-7) 3:4(-8) 3:4(-9) 4:5(-7) 4:5(-8) 4:5(-9) 4:5(-10) 5:6(-8) 5:6(-7) 5:6(-8) 5:6(-9) 5:6(-10) 6:7(-8) 6:7(-7) 6:7(-6) 6:7(-7) 6:7(-8) 6:7(-9) 7:8(-7) 7:8(-6) 7:8(-5) 7:8(-4) 7:8(-5) 7:8(-6) 7:8(-7)
function result: 8
0:2(4) 1:3(4) 0:3(5) 2:4(3) 1:4(4) 0:4(5) 3:5(3) 2:5(2) 1:5(3) 0:5(4) 4:6(3) 3:6(2) 2:6(1) 1:6(2) 0:6(3) 5:7(3) 4:7(2) 3:7(1) 2:7(0) 1:7(1) 0:7(2) 6:8(2) 5:8(2) 4:8(1) 3:8(0) 3:8(-1) 1:8(0) 0:8(1) 7:9(0) 6:9(0) 5:9(0) 5:9(-1) 5:9(-2) 5:9(-3) 5:9(-2) 5:9(-1) 9:10(-3) 9:10(-3) 9:10(-3) 9:10(-3) 9:10(-4) 9:10(-5) 9:10(-6) 9:10(-5) 9:10(-4) 10:11(-4) 10:11(-5) 10:11(-5) 10:11(-5) 10:11(-5) 10:11(-6) 10:11(-7) 10:11(-8) 10:11(-7) 10:11(-6) 11:12(-4) 11:12(-5) 11:12(-6) 11:12(-6) 11:12(-6) 11:12(-6) 11:12(-7) 11:12(-8) 11:12(-9) 11:12(-8) 11:12(-7) 12:13(-5) 12:13(-4) 12:13(-5) 12:13(-6) 12:13(-6) 12:13(-6) 12:13(-6) 12:13(-7) 12:13(-8) 12:13(-9) 12:13(-8) 12:13(-7)
function result: 8
0:2(4) 1:3(4) 0:3(5) 2:4(3) 1:4(4) 0:4(5) 3:5(3) 2:5(2) 1:5(3) 0:5(4) 4:6(3) 3:6(2) 2:6(1) 1:6(2) 0:6(3) 5:7(3) 4:7(2) 3:7(1) 2:7(0) 1:7(1) 0:7(2) 6:8(2) 5:8(2) 4:8(1) 3:8(0) 3:8(-1) 1:8(0) 0:8(1) 7:9(0) 6:9(0) 5:9(0) 5:9(-1) 5:9(-2) 5:9(-3) 5:9(-2) 5:9(-1) 9:10(-3) 9:10(-3) 9:10(-3) 9:10(-3) 9:10(-4) 9:10(-5) 9:10(-6) 9:10(-5) 9:10(-4) 10:11(-6) 10:11(-7) 10:11(-7) 10:11(-7) 10:11(-7) 10:11(-8) 10:11(-9) 10:11(-10) 10:11(-9) 10:11(-8) 11:12(-8) 11:12(-9) 11:12(-10) 11:12(-10) 11:12(-10) 11:12(-10) 11:12(-11) 11:12(-12) 11:12(-13) 11:12(-12) 11:12(-11) 12:13(-9) 12:13(-10) 12:13(-11) 12:13(-12) 12:13(-12) 12:13(-12) 12:13(-12) 12:13(-13) 12:13(-14) 12:13(-15) 12:13(-14) 12:13(-13) 13:14(-11) 13:14(-10) 13:14(-11) 13:14(-12) 13:14(-13) 13:14(-13) 13:14(-13) 13:14(-13) 13:14(-14) 13:14(-15) 13:14(-16) 13:14(-15) 13:14(-14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
print " A[$k]$sub_start:$sub_end($sub_sum)";
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
A[2]1:2(-2) A[3]2:3(-2) A[3]2:3(-3) A[4]3:4(-3) A[4]3:4(-2) A[4]3:4(-3) A[5]4:5(-2) A[5]4:5(-3) A[5]4:5(-2) A[5]4:5(-3) A[6]5:6(-2) A[6]5:6(-1) A[6]5:6(-2) A[6]5:6(-1) A[6]5:6(-2) A[7]6:7(-2) A[7]6:7(-2) A[7]6:7(-1) A[7]6:7(-2) A[7]6:7(-1) A[7]6:7(-2) A[8]7:8(-4) A[8]7:8(-3) A[8]7:8(-3) A[8]7:8(-2) A[8]7:8(-3) A[8]7:8(-2) A[8]7:8(-3)
A[2]0:2(2) A[3]1:3(0) A[3]0:3(1) A[4]3:4(-1) A[4]3:4(-2) A[4]3:4(-1) A[5]4:5(-3) A[5]4:5(-4) A[5]4:5(-5) A[5]4:5(-4) A[6]5:6(-6) A[6]5:6(-7) A[6]5:6(-8) A[6]5:6(-9) A[6]5:6(-8) A[7]6:7(-8) A[7]6:7(-9) A[7]6:7(-10) A[7]6:7(-11) A[7]6:7(-12) A[7]6:7(-11) A[8]7:8(-9) A[8]7:8(-10) A[8]7:8(-11) A[8]7:8(-12) A[8]7:8(-13) A[8]7:8(-14) A[8]7:8(-13)
function result: 9
A[2]0:2(4) A[3]1:3(6) A[3]0:3(7) A[4]2:4(9) A[4]1:4(10) A[4]0:4(11) A[5]3:5(13) A[5]2:5(14) A[5]1:5(15) A[5]0:5(16) A[6]4:6(16) A[6]3:6(17) A[6]2:6(18) A[6]1:6(19) A[6]0:6(20) A[7]5:7(18) A[7]4:7(19) A[7]3:7(20) A[7]2:7(21) A[7]1:7(22) A[7]0:7(23) A[8]6:8(21) A[8]5:8(20) A[8]4:8(21) A[8]3:8(22) A[8]2:8(23) A[8]1:8(24) A[8]0:8(25) A[9]7:9(23) A[9]6:9(22) A[9]5:9(21) A[9]4:9(22) A[9]3:9(23) A[9]2:9(24) A[9]1:9(25) A[9]0:9(26)
function result: 3
A[2]0:2(0) A[3]1:3(0) A[3]0:3(0)
function result: 1
A[2]1:2(-4) A[3]2:3(-6) A[3]2:3(-7)
function result: 4
A[2]0:2(0) A[3]1:3(0) A[3]0:3(0) A[4]2:4(0) A[4]1:4(0) A[4]0:4(0)
function result: 1
A[2]1:2(-4) A[3]2:3(-6) A[3]2:3(-7) A[4]3:4(-7) A[4]3:4(-8) A[4]3:4(-9) A[5]4:5(-7) A[5]4:5(-8) A[5]4:5(-9) A[5]4:5(-10) A[6]5:6(-8) A[6]5:6(-7) A[6]5:6(-8) A[6]5:6(-9) A[6]5:6(-10) A[7]6:7(-8) A[7]6:7(-7) A[7]6:7(-6) A[7]6:7(-7) A[7]6:7(-8) A[7]6:7(-9) A[8]7:8(-7) A[8]7:8(-6) A[8]7:8(-5) A[8]7:8(-4) A[8]7:8(-5) A[8]7:8(-6) A[8]7:8(-7)
function result: 8
A[2]0:2(4) A[3]1:3(4) A[3]0:3(5) A[4]2:4(3) A[4]1:4(4) A[4]0:4(5) A[5]3:5(3) A[5]2:5(2) A[5]1:5(3) A[5]0:5(4) A[6]4:6(3) A[6]3:6(2) A[6]2:6(1) A[6]1:6(2) A[6]0:6(3) A[7]5:7(3) A[7]4:7(2) A[7]3:7(1) A[7]2:7(0) A[7]1:7(1) A[7]0:7(2) A[8]6:8(2) A[8]5:8(2) A[8]4:8(1) A[8]3:8(0) A[8]3:8(-1) A[8]1:8(0) A[8]0:8(1) A[9]7:9(0) A[9]6:9(0) A[9]5:9(0) A[9]5:9(-1) A[9]5:9(-2) A[9]5:9(-3) A[9]5:9(-2) A[9]5:9(-1) A[10]9:10(-3) A[10]9:10(-3) A[10]9:10(-3) A[10]9:10(-3) A[10]9:10(-4) A[10]9:10(-5) A[10]9:10(-6) A[10]9:10(-5) A[10]9:10(-4) A[11]10:11(-4) A[11]10:11(-5) A[11]10:11(-5) A[11]10:11(-5) A[11]10:11(-5) A[11]10:11(-6) A[11]10:11(-7) A[11]10:11(-8) A[11]10:11(-7) A[11]10:11(-6) A[12]11:12(-4) A[12]11:12(-5) A[12]11:12(-6) A[12]11:12(-6) A[12]11:12(-6) A[12]11:12(-6) A[12]11:12(-7) A[12]11:12(-8) A[12]11:12(-9) A[12]11:12(-8) A[12]11:12(-7) A[13]12:13(-5) A[13]12:13(-4) A[13]12:13(-5) A[13]12:13(-6) A[13]12:13(-6) A[13]12:13(-6) A[13]12:13(-6) A[13]12:13(-7) A[13]12:13(-8) A[13]12:13(-9) A[13]12:13(-8) A[13]12:13(-7)
function result: 8
A[2]0:2(4) A[3]1:3(4) A[3]0:3(5) A[4]2:4(3) A[4]1:4(4) A[4]0:4(5) A[5]3:5(3) A[5]2:5(2) A[5]1:5(3) A[5]0:5(4) A[6]4:6(3) A[6]3:6(2) A[6]2:6(1) A[6]1:6(2) A[6]0:6(3) A[7]5:7(3) A[7]4:7(2) A[7]3:7(1) A[7]2:7(0) A[7]1:7(1) A[7]0:7(2) A[8]6:8(2) A[8]5:8(2) A[8]4:8(1) A[8]3:8(0) A[8]3:8(-1) A[8]1:8(0) A[8]0:8(1) A[9]7:9(0) A[9]6:9(0) A[9]5:9(0) A[9]5:9(-1) A[9]5:9(-2) A[9]5:9(-3) A[9]5:9(-2) A[9]5:9(-1) A[10]9:10(-3) A[10]9:10(-3) A[10]9:10(-3) A[10]9:10(-3) A[10]9:10(-4) A[10]9:10(-5) A[10]9:10(-6) A[10]9:10(-5) A[10]9:10(-4) A[11]10:11(-6) A[11]10:11(-7) A[11]10:11(-7) A[11]10:11(-7) A[11]10:11(-7) A[11]10:11(-8) A[11]10:11(-9) A[11]10:11(-10) A[11]10:11(-9) A[11]10:11(-8) A[12]11:12(-8) A[12]11:12(-9) A[12]11:12(-10) A[12]11:12(-10) A[12]11:12(-10) A[12]11:12(-10) A[12]11:12(-11) A[12]11:12(-12) A[12]11:12(-13) A[12]11:12(-12) A[12]11:12(-11) A[13]12:13(-9) A[13]12:13(-10) A[13]12:13(-11) A[13]12:13(-12) A[13]12:13(-12) A[13]12:13(-12) A[13]12:13(-12) A[13]12:13(-13) A[13]12:13(-14) A[13]12:13(-15) A[13]12:13(-14) A[13]12:13(-13) A[14]13:14(-11) A[14]13:14(-10) A[14]13:14(-11) A[14]13:14(-12) A[14]13:14(-13) A[14]13:14(-13) A[14]13:14(-13) A[14]13:14(-13) A[14]13:14(-14) A[14]13:14(-15) A[14]13:14(-16) A[14]13:14(-15) A[14]13:14(-14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
//print " A[$k]$sub_start:$sub_end($sub_sum)";
}
$sub_end--;
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 8
function result: 2
function result: 0
function result: 3
function result: 0
function result: 7
function result: 7
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
//print " A[$k]$sub_start:$sub_end($sub_sum)";
}
$sub_end++;
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 10
function result: 4
function result: 2
function result: 5
function result: 2
function result: 9
function result: 9
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
//print " A[$k]$sub_start:$sub_end($sub_sum)";
}
$sub_end++;
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 4
function result: 4
function result: 2
function result: 5
function result: 9
function result: 14
function result: 15
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
//print " A[$k]$sub_start:$sub_end($sub_sum)";
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 3
function result: 3
function result: 1
function result: 4
function result: 8
function result: 13
function result: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
print " A[$k]$sub_start:$sub_end($sub_sum)";
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
A[2]0:2(0) A[3]2:3(-2) A[3]2:3(-1) A[4]2:4(1) A[4]1:4(0) A[4]0:4(1) A[5]3:5(0) A[5]2:5(1) A[5]1:5(0) A[5]0:5(1) A[6]4:6(2) A[6]3:6(1) A[6]2:6(2) A[6]1:6(1) A[6]0:6(2) A[7]5:7(0) A[7]4:7(0) A[7]4:7(-1) A[7]2:7(0) A[7]2:7(-1) A[7]0:7(0) A[8]6:8(0) A[8]6:8(-1) A[8]6:8(-1) A[8]6:8(-2) A[8]6:8(-1) A[8]6:8(-2) A[8]6:8(-1)
A[2]0:2(0) A[3]1:3(0) A[3]1:3(-1) A[4]3:4(-1) A[4]1:4(0) A[4]1:4(-1) A[5]4:5(-1) A[5]2:5(0) A[5]1:5(1) A[5]0:5(0) A[6]4:6(0) A[6]3:6(1) A[6]2:6(2) A[6]1:6(3) A[6]0:6(2) A[7]5:7(4) A[7]4:7(5) A[7]3:7(6) A[7]2:7(7) A[7]1:7(8) A[7]0:7(7) A[8]6:8(7) A[8]5:8(8) A[8]4:8(9) A[8]3:8(10) A[8]2:8(11) A[8]1:8(12) A[8]0:8(11)
function result: 3
A[2]0:2(2) A[3]1:3(2) A[3]0:3(1) A[4]2:4(1) A[4]1:4(0) A[4]1:4(-1) A[5]4:5(-1) A[5]4:5(-2) A[5]4:5(-3) A[5]4:5(-4) A[6]5:6(-6) A[6]5:6(-7) A[6]5:6(-8) A[6]5:6(-9) A[6]5:6(-10) A[7]6:7(-10) A[7]6:7(-11) A[7]6:7(-12) A[7]6:7(-13) A[7]6:7(-14) A[7]6:7(-15) A[8]7:8(-15) A[8]7:8(-14) A[8]7:8(-15) A[8]7:8(-16) A[8]7:8(-17) A[8]7:8(-18) A[8]7:8(-19) A[9]8:9(-19) A[9]8:9(-18) A[9]8:9(-17) A[9]8:9(-18) A[9]8:9(-19) A[9]8:9(-20) A[9]8:9(-21) A[9]8:9(-22)
function result: 3
A[2]0:2(0) A[3]1:3(0) A[3]0:3(0)
function result: 1
A[2]1:2(-2) A[3]2:3(-2) A[3]2:3(-1)
function result: 4
A[2]0:2(0) A[3]1:3(0) A[3]0:3(0) A[4]2:4(0) A[4]1:4(0) A[4]0:4(0)
function result: 8
A[2]1:2(-2) A[3]2:3(-2) A[3]2:3(-1) A[4]2:4(1) A[4]1:4(2) A[4]0:4(3) A[5]3:5(3) A[5]2:5(4) A[5]1:5(5) A[5]0:5(6) A[6]4:6(6) A[6]3:6(5) A[6]2:6(6) A[6]1:6(7) A[6]0:6(8) A[7]5:7(8) A[7]4:7(7) A[7]3:7(6) A[7]2:7(7) A[7]1:7(8) A[7]0:7(9) A[8]6:8(9) A[8]5:8(8) A[8]4:8(7) A[8]3:8(6) A[8]2:8(7) A[8]1:8(8) A[8]0:8(9)
function result: 13
A[2]0:2(2) A[3]1:3(0) A[3]1:3(-1) A[4]3:4(-1) A[4]3:4(-2) A[4]3:4(-3) A[5]4:5(-3) A[5]4:5(-2) A[5]4:5(-3) A[5]4:5(-4) A[6]5:6(-3) A[6]5:6(-2) A[6]5:6(-1) A[6]5:6(-2) A[6]5:6(-3) A[7]6:7(-3) A[7]6:7(-2) A[7]6:7(-1) A[7]2:7(0) A[7]2:7(-1) A[7]2:7(-2) A[8]7:8(-2) A[8]7:8(-2) A[8]7:8(-1) A[8]3:8(0) A[8]2:8(1) A[8]1:8(0) A[8]1:8(-1) A[9]8:9(-2) A[9]8:9(-2) A[9]8:9(-2) A[9]8:9(-1) A[9]3:9(0) A[9]2:9(1) A[9]1:9(0) A[9]1:9(-1) A[10]9:10(-1) A[10]9:10(-1) A[10]9:10(-1) A[10]9:10(-1) A[10]4:10(0) A[10]3:10(1) A[10]2:10(2) A[10]1:10(1) A[10]0:10(0) A[11]9:11(2) A[11]8:11(3) A[11]7:11(3) A[11]6:11(3) A[11]5:11(3) A[11]4:11(4) A[11]3:11(5) A[11]2:11(6) A[11]1:11(5) A[11]0:11(4) A[12]10:12(4) A[12]9:12(5) A[12]8:12(6) A[12]7:12(6) A[12]6:12(6) A[12]5:12(6) A[12]4:12(7) A[12]3:12(8) A[12]2:12(9) A[12]1:12(8) A[12]0:12(7) A[13]11:13(7) A[13]10:13(6) A[13]9:13(7) A[13]8:13(8) A[13]7:13(8) A[13]6:13(8) A[13]5:13(8) A[13]4:13(9) A[13]3:13(10) A[13]2:13(11) A[13]1:13(10) A[13]0:13(9)
function result: 14
A[2]0:2(2) A[3]1:3(0) A[3]1:3(-1) A[4]3:4(-1) A[4]3:4(-2) A[4]3:4(-3) A[5]4:5(-3) A[5]4:5(-2) A[5]4:5(-3) A[5]4:5(-4) A[6]5:6(-3) A[6]5:6(-2) A[6]5:6(-1) A[6]5:6(-2) A[6]5:6(-3) A[7]6:7(-3) A[7]6:7(-2) A[7]6:7(-1) A[7]2:7(0) A[7]2:7(-1) A[7]2:7(-2) A[8]7:8(-2) A[8]7:8(-2) A[8]7:8(-1) A[8]3:8(0) A[8]2:8(1) A[8]1:8(0) A[8]1:8(-1) A[9]8:9(-2) A[9]8:9(-2) A[9]8:9(-2) A[9]8:9(-1) A[9]3:9(0) A[9]2:9(1) A[9]1:9(0) A[9]1:9(-1) A[10]9:10(-1) A[10]9:10(-1) A[10]9:10(-1) A[10]9:10(-1) A[10]4:10(0) A[10]3:10(1) A[10]2:10(2) A[10]1:10(1) A[10]0:10(0) A[11]9:11(0) A[11]8:11(1) A[11]7:11(1) A[11]6:11(1) A[11]5:11(1) A[11]4:11(2) A[11]3:11(3) A[11]2:11(4) A[11]1:11(3) A[11]0:11(2) A[12]10:12(4) A[12]9:12(5) A[12]8:12(6) A[12]7:12(6) A[12]6:12(6) A[12]5:12(6) A[12]4:12(7) A[12]3:12(8) A[12]2:12(9) A[12]1:12(8) A[12]0:12(7) A[13]11:13(7) A[13]10:13(8) A[13]9:13(9) A[13]8:13(10) A[13]7:13(10) A[13]6:13(10) A[13]5:13(10) A[13]4:13(11) A[13]3:13(12) A[13]2:13(13) A[13]1:13(12) A[13]0:13(11) A[14]12:14(11) A[14]11:14(10) A[14]10:14(11) A[14]9:14(12) A[14]8:14(13) A[14]7:14(13) A[14]6:14(13) A[14]5:14(13) A[14]4:14(14) A[14]3:14(15) A[14]2:14(16) A[14]1:14(15) A[14]0:14(14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
print " A[$k]$sub_start:$sub_end($sub_sum)";
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
A[0]-1:0(-1) A[1]0:1(-2) A[2]0:2(0) A[3]2:3(-1) A[4]0:4(1) A[5]0:5(1) A[6]0:6(2) A[7]0:7(0) A[8]6:8(-1)
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(0) A[3]1:3(-1) A[4]1:4(-1) A[5]0:5(0) A[6]0:6(2) A[7]0:7(7) A[8]0:8(11)
function result: 3
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(2) A[3]0:3(1) A[4]1:4(-1) A[5]4:5(-4) A[6]5:6(-10) A[7]6:7(-15) A[8]7:8(-19) A[9]8:9(-22)
function result: 3
A[0]-1:0(0) A[1]0:1(0) A[2]0:2(0) A[3]0:3(0)
function result: 1
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-2) A[3]2:3(-1)
function result: 4
A[0]-1:0(0) A[1]0:1(0) A[2]0:2(0) A[3]0:3(0) A[4]0:4(0)
function result: 8
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-2) A[3]2:3(-1) A[4]0:4(3) A[5]0:5(6) A[6]0:6(8) A[7]0:7(9) A[8]0:8(9)
function result: 13
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(2) A[3]1:3(-1) A[4]3:4(-3) A[5]4:5(-4) A[6]5:6(-3) A[7]2:7(-2) A[8]1:8(-1) A[9]1:9(-1) A[10]0:10(0) A[11]0:11(4) A[12]0:12(7) A[13]0:13(9)
function result: 14
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(2) A[3]1:3(-1) A[4]3:4(-3) A[5]4:5(-4) A[6]5:6(-3) A[7]2:7(-2) A[8]1:8(-1) A[9]1:9(-1) A[10]0:10(0) A[11]0:11(2) A[12]0:12(7) A[13]0:13(11) A[14]0:14(14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum -= $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
print " A[$k]$sub_start:$sub_end($sub_sum)";
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(2) A[3]0:3(3) A[4]0:4(3) A[5]0:5(3) A[6]0:6(2) A[7]0:7(2) A[8]0:8(3)
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-2) A[3]1:3(-1) A[4]0:4(1) A[5]0:5(4) A[6]0:6(8) A[7]0:7(11) A[8]0:8(13)
function result: 1
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-4) A[3]2:3(-7) A[4]3:4(-11) A[5]4:5(-16) A[6]5:6(-20) A[7]6:7(-23) A[8]7:8(-25) A[9]8:9(-26)
function result: 3
A[0]-1:0(0) A[1]0:1(0) A[2]0:2(0) A[3]0:3(0)
function result: 3
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(4) A[3]0:3(7)
function result: 4
A[0]-1:0(0) A[1]0:1(0) A[2]0:2(0) A[3]0:3(0) A[4]0:4(0)
function result: 8
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(4) A[3]0:3(7) A[4]0:4(9) A[5]0:5(10) A[6]0:6(10) A[7]0:7(9) A[8]0:8(7)
function result: 13
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-4) A[3]2:3(-5) A[4]3:4(-5) A[5]4:5(-4) A[6]5:6(-3) A[7]2:7(-2) A[8]1:8(-1) A[9]0:9(1) A[10]0:10(4) A[11]0:11(6) A[12]0:12(7) A[13]0:13(7)
function result: 14
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-4) A[3]2:3(-5) A[4]3:4(-5) A[5]4:5(-4) A[6]5:6(-3) A[7]2:7(-2) A[8]1:8(-1) A[9]0:9(1) A[10]0:10(4) A[11]0:11(8) A[12]0:12(11) A[13]0:13(13) A[14]0:14(14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum -= $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum > 0) {
$sub_start = $float_start;
}
}
print " A[$k]$sub_start:$sub_end($sub_sum)";
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(2) A[3]0:3(3) A[4]0:4(3) A[5]0:5(3) A[6]0:6(2) A[7]0:7(2) A[8]0:8(3)
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-2) A[3]2:3(-1) A[4]0:4(1) A[5]0:5(4) A[6]0:6(8) A[7]0:7(11) A[8]0:8(13)
function result: 1
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-4) A[3]2:3(-7) A[4]3:4(-11) A[5]4:5(-16) A[6]5:6(-20) A[7]6:7(-23) A[8]7:8(-25) A[9]8:9(-26)
function result: 1
A[0]-1:0(0) A[1]0:1(0) A[2]1:2(0) A[3]2:3(0)
function result: 3
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(4) A[3]0:3(7)
function result: 1
A[0]-1:0(0) A[1]0:1(0) A[2]1:2(0) A[3]2:3(0) A[4]3:4(0)
function result: 8
A[0]-1:0(1) A[1]0:1(2) A[2]0:2(4) A[3]0:3(7) A[4]0:4(9) A[5]0:5(10) A[6]0:6(10) A[7]0:7(9) A[8]0:8(7)
function result: 13
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-4) A[3]2:3(-5) A[4]3:4(-5) A[5]4:5(-4) A[6]5:6(-3) A[7]6:7(-2) A[8]2:8(-1) A[9]0:9(1) A[10]0:10(4) A[11]0:11(6) A[12]0:12(7) A[13]0:13(7)
function result: 14
A[0]-1:0(-1) A[1]0:1(-2) A[2]1:2(-4) A[3]2:3(-5) A[4]3:4(-5) A[5]4:5(-4) A[6]5:6(-3) A[7]6:7(-2) A[8]2:8(-1) A[9]0:9(1) A[10]0:10(4) A[11]0:11(8) A[12]0:12(11) A[13]0:13(13) A[14]0:14(14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 3
function result: 3
function result: 1
function result: 4
function result: 8
function result: 13
function result: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
print "Adjusting float: $sub_sum";
$sub_start = $float_start;
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 1Adjusting float: 2Adjusting float: 1Adjusting float: 2Adjusting float: 1Adjusting float: 2Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 0
Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 2Adjusting float: 3Adjusting float: 2Adjusting float: 4Adjusting float: 5Adjusting float: 6Adjusting float: 7Adjusting float: 8Adjusting float: 7Adjusting float: 7Adjusting float: 8Adjusting float: 9Adjusting float: 10Adjusting float: 11Adjusting float: 12Adjusting float: 11
function result: 3
Adjusting float: 2Adjusting float: 2Adjusting float: 1Adjusting float: 1Adjusting float: 0
function result: 3
Adjusting float: 0Adjusting float: 0Adjusting float: 0
function result: 1
function result: 4
Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 0
function result: 8
Adjusting float: 1Adjusting float: 2Adjusting float: 3Adjusting float: 3Adjusting float: 4Adjusting float: 5Adjusting float: 6Adjusting float: 6Adjusting float: 5Adjusting float: 6Adjusting float: 7Adjusting float: 8Adjusting float: 8Adjusting float: 7Adjusting float: 6Adjusting float: 7Adjusting float: 8Adjusting float: 9Adjusting float: 9Adjusting float: 8Adjusting float: 7Adjusting float: 6Adjusting float: 7Adjusting float: 8Adjusting float: 9
function result: 13
Adjusting float: 2Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 2Adjusting float: 1Adjusting float: 0Adjusting float: 2Adjusting float: 3Adjusting float: 3Adjusting float: 3Adjusting float: 3Adjusting float: 4Adjusting float: 5Adjusting float: 6Adjusting float: 5Adjusting float: 4Adjusting float: 4Adjusting float: 5Adjusting float: 6Adjusting float: 6Adjusting float: 6Adjusting float: 6Adjusting float: 7Adjusting float: 8Adjusting float: 9Adjusting float: 8Adjusting float: 7Adjusting float: 7Adjusting float: 6Adjusting float: 7Adjusting float: 8Adjusting float: 8Adjusting float: 8Adjusting float: 8Adjusting float: 9Adjusting float: 10Adjusting float: 11Adjusting float: 10Adjusting float: 9
function result: 14
Adjusting float: 2Adjusting float: 0Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 2Adjusting float: 1Adjusting float: 0Adjusting float: 0Adjusting float: 1Adjusting float: 1Adjusting float: 1Adjusting float: 1Adjusting float: 2Adjusting float: 3Adjusting float: 4Adjusting float: 3Adjusting float: 2Adjusting float: 4Adjusting float: 5Adjusting float: 6Adjusting float: 6Adjusting float: 6Adjusting float: 6Adjusting float: 7Adjusting float: 8Adjusting float: 9Adjusting float: 8Adjusting float: 7Adjusting float: 7Adjusting float: 8Adjusting float: 9Adjusting float: 10Adjusting float: 10Adjusting float: 10Adjusting float: 10Adjusting float: 11Adjusting float: 12Adjusting float: 13Adjusting float: 12Adjusting float: 11Adjusting float: 11Adjusting float: 10Adjusting float: 11Adjusting float: 12Adjusting float: 13Adjusting float: 13Adjusting float: 13Adjusting float: 13Adjusting float: 14Adjusting float: 15Adjusting float: 16Adjusting float: 15Adjusting float: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
print " AF($sub_sum) ";
$sub_start = $float_start;
} else {
print " CF($sub_sum) ";
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
AF(0) CF(-2) CF(-1) AF(1) AF(0) AF(1) AF(0) AF(1) AF(0) AF(1) AF(2) AF(1) AF(2) AF(1) AF(2) AF(0) AF(0) CF(-1) AF(0) CF(-1) AF(0) AF(0) CF(-1) CF(-1) CF(-2) CF(-1) CF(-2) CF(-1)
AF(0) AF(0) CF(-1) CF(-1) AF(0) CF(-1) CF(-1) AF(0) AF(1) AF(0) AF(0) AF(1) AF(2) AF(3) AF(2) AF(4) AF(5) AF(6) AF(7) AF(8) AF(7) AF(7) AF(8) AF(9) AF(10) AF(11) AF(12) AF(11)
function result: 3
AF(2) AF(2) AF(1) AF(1) AF(0) CF(-1) CF(-1) CF(-2) CF(-3) CF(-4) CF(-6) CF(-7) CF(-8) CF(-9) CF(-10) CF(-10) CF(-11) CF(-12) CF(-13) CF(-14) CF(-15) CF(-15) CF(-14) CF(-15) CF(-16) CF(-17) CF(-18) CF(-19) CF(-19) CF(-18) CF(-17) CF(-18) CF(-19) CF(-20) CF(-21) CF(-22)
function result: 3
AF(0) AF(0) AF(0)
function result: 1
CF(-2) CF(-2) CF(-1)
function result: 4
AF(0) AF(0) AF(0) AF(0) AF(0) AF(0)
function result: 8
CF(-2) CF(-2) CF(-1) AF(1) AF(2) AF(3) AF(3) AF(4) AF(5) AF(6) AF(6) AF(5) AF(6) AF(7) AF(8) AF(8) AF(7) AF(6) AF(7) AF(8) AF(9) AF(9) AF(8) AF(7) AF(6) AF(7) AF(8) AF(9)
function result: 13
AF(2) AF(0) CF(-1) CF(-1) CF(-2) CF(-3) CF(-3) CF(-2) CF(-3) CF(-4) CF(-3) CF(-2) CF(-1) CF(-2) CF(-3) CF(-3) CF(-2) CF(-1) AF(0) CF(-1) CF(-2) CF(-2) CF(-2) CF(-1) AF(0) AF(1) AF(0) CF(-1) CF(-2) CF(-2) CF(-2) CF(-1) AF(0) AF(1) AF(0) CF(-1) CF(-1) CF(-1) CF(-1) CF(-1) AF(0) AF(1) AF(2) AF(1) AF(0) AF(2) AF(3) AF(3) AF(3) AF(3) AF(4) AF(5) AF(6) AF(5) AF(4) AF(4) AF(5) AF(6) AF(6) AF(6) AF(6) AF(7) AF(8) AF(9) AF(8) AF(7) AF(7) AF(6) AF(7) AF(8) AF(8) AF(8) AF(8) AF(9) AF(10) AF(11) AF(10) AF(9)
function result: 14
AF(2) AF(0) CF(-1) CF(-1) CF(-2) CF(-3) CF(-3) CF(-2) CF(-3) CF(-4) CF(-3) CF(-2) CF(-1) CF(-2) CF(-3) CF(-3) CF(-2) CF(-1) AF(0) CF(-1) CF(-2) CF(-2) CF(-2) CF(-1) AF(0) AF(1) AF(0) CF(-1) CF(-2) CF(-2) CF(-2) CF(-1) AF(0) AF(1) AF(0) CF(-1) CF(-1) CF(-1) CF(-1) CF(-1) AF(0) AF(1) AF(2) AF(1) AF(0) AF(0) AF(1) AF(1) AF(1) AF(1) AF(2) AF(3) AF(4) AF(3) AF(2) AF(4) AF(5) AF(6) AF(6) AF(6) AF(6) AF(7) AF(8) AF(9) AF(8) AF(7) AF(7) AF(8) AF(9) AF(10) AF(10) AF(10) AF(10) AF(11) AF(12) AF(13) AF(12) AF(11) AF(11) AF(10) AF(11) AF(12) AF(13) AF(13) AF(13) AF(13) AF(14) AF(15) AF(16) AF(15) AF(14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
print " AF($sub_sum) ";
$sub_start = $float_start;
} else {
print " CF($sub_sum) ";
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
CF(-2) CF(-2) CF(-3) CF(-3) CF(-2) CF(-3) CF(-2) CF(-3) CF(-2) CF(-3) CF(-2) CF(-1) CF(-2) CF(-1) CF(-2) CF(-2) CF(-2) CF(-1) CF(-2) CF(-1) CF(-2) CF(-4) CF(-3) CF(-3) CF(-2) CF(-3) CF(-2) CF(-3)
AF(2) AF(0) AF(1) CF(-1) CF(-2) CF(-1) CF(-3) CF(-4) CF(-5) CF(-4) CF(-6) CF(-7) CF(-8) CF(-9) CF(-8) CF(-8) CF(-9) CF(-10) CF(-11) CF(-12) CF(-11) CF(-9) CF(-10) CF(-11) CF(-12) CF(-13) CF(-14) CF(-13)
function result: 9
AF(4) AF(6) AF(7) AF(9) AF(10) AF(11) AF(13) AF(14) AF(15) AF(16) AF(16) AF(17) AF(18) AF(19) AF(20) AF(18) AF(19) AF(20) AF(21) AF(22) AF(23) AF(21) AF(20) AF(21) AF(22) AF(23) AF(24) AF(25) AF(23) AF(22) AF(21) AF(22) AF(23) AF(24) AF(25) AF(26)
function result: 3
AF(0) AF(0) AF(0)
function result: 1
CF(-4) CF(-6) CF(-7)
function result: 4
AF(0) AF(0) AF(0) AF(0) AF(0) AF(0)
function result: 1
CF(-4) CF(-6) CF(-7) CF(-7) CF(-8) CF(-9) CF(-7) CF(-8) CF(-9) CF(-10) CF(-8) CF(-7) CF(-8) CF(-9) CF(-10) CF(-8) CF(-7) CF(-6) CF(-7) CF(-8) CF(-9) CF(-7) CF(-6) CF(-5) CF(-4) CF(-5) CF(-6) CF(-7)
function result: 8
AF(4) AF(4) AF(5) AF(3) AF(4) AF(5) AF(3) AF(2) AF(3) AF(4) AF(3) AF(2) AF(1) AF(2) AF(3) AF(3) AF(2) AF(1) AF(0) AF(1) AF(2) AF(2) AF(2) AF(1) AF(0) CF(-1) AF(0) AF(1) AF(0) AF(0) AF(0) CF(-1) CF(-2) CF(-3) CF(-2) CF(-1) CF(-3) CF(-3) CF(-3) CF(-3) CF(-4) CF(-5) CF(-6) CF(-5) CF(-4) CF(-4) CF(-5) CF(-5) CF(-5) CF(-5) CF(-6) CF(-7) CF(-8) CF(-7) CF(-6) CF(-4) CF(-5) CF(-6) CF(-6) CF(-6) CF(-6) CF(-7) CF(-8) CF(-9) CF(-8) CF(-7) CF(-5) CF(-4) CF(-5) CF(-6) CF(-6) CF(-6) CF(-6) CF(-7) CF(-8) CF(-9) CF(-8) CF(-7)
function result: 8
AF(4) AF(4) AF(5) AF(3) AF(4) AF(5) AF(3) AF(2) AF(3) AF(4) AF(3) AF(2) AF(1) AF(2) AF(3) AF(3) AF(2) AF(1) AF(0) AF(1) AF(2) AF(2) AF(2) AF(1) AF(0) CF(-1) AF(0) AF(1) AF(0) AF(0) AF(0) CF(-1) CF(-2) CF(-3) CF(-2) CF(-1) CF(-3) CF(-3) CF(-3) CF(-3) CF(-4) CF(-5) CF(-6) CF(-5) CF(-4) CF(-6) CF(-7) CF(-7) CF(-7) CF(-7) CF(-8) CF(-9) CF(-10) CF(-9) CF(-8) CF(-8) CF(-9) CF(-10) CF(-10) CF(-10) CF(-10) CF(-11) CF(-12) CF(-13) CF(-12) CF(-11) CF(-9) CF(-10) CF(-11) CF(-12) CF(-12) CF(-12) CF(-12) CF(-13) CF(-14) CF(-15) CF(-14) CF(-13) CF(-11) CF(-10) CF(-11) CF(-12) CF(-13) CF(-13) CF(-13) CF(-13) CF(-14) CF(-15) CF(-16) CF(-15) CF(-14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
// Walk back on the previous values of the array
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
print " AF($sub_sum) ";
$sub_start = $float_start;
} else {
print " CF($sub_sum) ";
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
CF(-2) CF(-2) CF(-3) CF(-3) CF(-2) CF(-3) CF(-2) CF(-3) CF(-2) CF(-3) CF(-2) CF(-1) CF(-2) CF(-1) CF(-2) CF(-2) CF(-2) CF(-1) CF(-2) CF(-1) CF(-2) CF(-4) CF(-3) CF(-3) CF(-2) CF(-3) CF(-2) CF(-3)
AF(2) AF(0) AF(1) CF(-1) CF(-2) CF(-1) CF(-3) CF(-4) CF(-5) CF(-4) CF(-6) CF(-7) CF(-8) CF(-9) CF(-8) CF(-8) CF(-9) CF(-10) CF(-11) CF(-12) CF(-11) CF(-9) CF(-10) CF(-11) CF(-12) CF(-13) CF(-14) CF(-13)
function result: 9
AF(4) AF(6) AF(7) AF(9) AF(10) AF(11) AF(13) AF(14) AF(15) AF(16) AF(16) AF(17) AF(18) AF(19) AF(20) AF(18) AF(19) AF(20) AF(21) AF(22) AF(23) AF(21) AF(20) AF(21) AF(22) AF(23) AF(24) AF(25) AF(23) AF(22) AF(21) AF(22) AF(23) AF(24) AF(25) AF(26)
function result: 3
AF(0) AF(0) AF(0)
function result: 1
CF(-4) CF(-6) CF(-7)
function result: 4
AF(0) AF(0) AF(0) AF(0) AF(0) AF(0)
function result: 1
CF(-4) CF(-6) CF(-7) CF(-7) CF(-8) CF(-9) CF(-7) CF(-8) CF(-9) CF(-10) CF(-8) CF(-7) CF(-8) CF(-9) CF(-10) CF(-8) CF(-7) CF(-6) CF(-7) CF(-8) CF(-9) CF(-7) CF(-6) CF(-5) CF(-4) CF(-5) CF(-6) CF(-7)
function result: 8
AF(4) AF(4) AF(5) AF(3) AF(4) AF(5) AF(3) AF(2) AF(3) AF(4) AF(3) AF(2) AF(1) AF(2) AF(3) AF(3) AF(2) AF(1) AF(0) AF(1) AF(2) AF(2) AF(2) AF(1) AF(0) CF(-1) AF(0) AF(1) AF(0) AF(0) AF(0) CF(-1) CF(-2) CF(-3) CF(-2) CF(-1) CF(-3) CF(-3) CF(-3) CF(-3) CF(-4) CF(-5) CF(-6) CF(-5) CF(-4) CF(-4) CF(-5) CF(-5) CF(-5) CF(-5) CF(-6) CF(-7) CF(-8) CF(-7) CF(-6) CF(-4) CF(-5) CF(-6) CF(-6) CF(-6) CF(-6) CF(-7) CF(-8) CF(-9) CF(-8) CF(-7) CF(-5) CF(-4) CF(-5) CF(-6) CF(-6) CF(-6) CF(-6) CF(-7) CF(-8) CF(-9) CF(-8) CF(-7)
function result: 8
AF(4) AF(4) AF(5) AF(3) AF(4) AF(5) AF(3) AF(2) AF(3) AF(4) AF(3) AF(2) AF(1) AF(2) AF(3) AF(3) AF(2) AF(1) AF(0) AF(1) AF(2) AF(2) AF(2) AF(1) AF(0) CF(-1) AF(0) AF(1) AF(0) AF(0) AF(0) CF(-1) CF(-2) CF(-3) CF(-2) CF(-1) CF(-3) CF(-3) CF(-3) CF(-3) CF(-4) CF(-5) CF(-6) CF(-5) CF(-4) CF(-6) CF(-7) CF(-7) CF(-7) CF(-7) CF(-8) CF(-9) CF(-10) CF(-9) CF(-8) CF(-8) CF(-9) CF(-10) CF(-10) CF(-10) CF(-10) CF(-11) CF(-12) CF(-13) CF(-12) CF(-11) CF(-9) CF(-10) CF(-11) CF(-12) CF(-12) CF(-12) CF(-12) CF(-13) CF(-14) CF(-15) CF(-14) CF(-13) CF(-11) CF(-10) CF(-11) CF(-12) CF(-13) CF(-13) CF(-13) CF(-13) CF(-14) CF(-15) CF(-16) CF(-15) CF(-14)
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
// Walk back on the previous values of the array
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 9
function result: 3
function result: 1
function result: 4
function result: 1
function result: 8
function result: 8
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum += $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
// Walk back on the previous values of the array
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 3
function result: 3
function result: 1
function result: 4
function result: 8
function result: 13
function result: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
// Walk back on the previous values of the array
while ($float_start > 0) {
$sub_sum -= $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 6
function result: 3
function result: 3
function result: 4
function result: 8
function result: 13
function result: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v;
$sub_end = $k;
$float_start = $sub_start = $k - 1;
// Walk back on the previous values of the array
while ($float_start > 0) {
$sub_sum += $A[$float_start];
$float_start--;
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 9
function result: 3
function result: 1
function result: 4
function result: 8
function result: 13
function result: 9
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k - 1;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 9
function result: 3
function result: 1
function result: 4
function result: 8
function result: 13
function result: 8
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 9
function result: 3
function result: 0
function result: 4
function result: 8
function result: 13
function result: 14
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
if($sub_start != $sub_end && $v) {
$sub_end--;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 8
function result: 3
function result: 0
function result: 4
function result: 7
function result: 12
function result: 13
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
if($sub_start != $sub_end && $v) {
$sub_end++;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
function result: 10
function result: 3
function result: 0
function result: 4
function result: 9
function result: 14
function result: 15
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Filter out NO MATCH scenarios
if($sub_start != $sub_end && $v) {
$sub_end++;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
[-1, -1, -1, 0, -1, -1, -1]
function result: 10
function result: 3
function result: 0
function result: 4
function result: 9
function result: 14
function result: 15
function result: 0
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Filter out NO MATCH scenarios
if($sub_start != $sub_end && $v >= 0) {
$sub_end++;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
[-1, -1, -1, 0, -1, -1, -1]
function result: 9
function result: 4
function result: 0
function result: 5
function result: 9
function result: 14
function result: 15
function result: 0
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Filter out NO MATCH scenarios
if($sub_start != $sub_end || $v >= 0) {
$sub_end++;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
[-1, -1, -1, 0, -1, -1, -1]
function result: 10
function result: 4
function result: 0
function result: 5
function result: 9
function result: 14
function result: 15
function result: 1
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Filter out NO MATCH scenarios
if($sub_start != $sub_end || $v >= 0) {
$sub_end++;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[-1, -1, 1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
[-1, -1, -1, 0, -1, -1, -1]
function result: 10
function result: 4
function result: 0
function result: 5
function result: 9
function result: 14
function result: 15
function result: 1
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Filter out NO MATCH scenarios
if($sub_start != $sub_end || $v >= 0) {
$sub_end++;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
[1, 1, 1, 1, 1, 1, -1, -1, -1, -1]
[0, 0, 0, 0]
[-1, -1, -1, -1]
[0, 0, 0, 0, 0]
[-1, -1, -1, -1, 1, 1, 1, 1, 1]
[1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1]
[-1, -1, 1, 1, 1, -1, -1, -1, 0, 0, 0, -1, -1, -1, 1, 1, 1]
[-1, -1, -1, 0, -1, -1, -1]
function result: 10
function result: 4
function result: 0
function result: 5
function result: 9
function result: 14
function result: 15
function result: 1
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$sub_start = 0;
$sub_end = 0;
$sub_sum = 0;
$max_len = 0;
// Iterate through each position in the array as the end of the slice (Kadane's adapted)
foreach($A as $k => $v) {
$sub_sum = $v; // Set initial value
$sub_end = $k; // And we'll be starting with this position
// Now start a floating key placeholder for checking the previous values.
$float_start = $sub_start = $k;
// Walk back on the previous values of the array
while ($float_start > 0) {
$float_start--; // Update the key placeholder
$sub_sum += $A[$float_start]; // Add the previous value
// Add a floating check
if($sub_sum >= 0) {
$sub_start = $float_start;
}
}
// Filter out NO MATCH scenarios
if($sub_start != $sub_end || $v >= 0) {
$sub_end++;
}
// Set the new (or keep the old) max length value
$max_len = max($max_len, $sub_end - $sub_start);
}
return $max_len;
}
The following issues have been detected: timeout errors.
large array with all prefixes positive
running time: >7.00 sec., time limit: 1.79 sec.
random array N = 100000
running time: >9.00 sec., time limit: 3.65 sec.
array of fibonacci words N = 100000
running time: >9.00 sec., time limit: 3.70 sec.