Greatest subsequential sum: Difference between revisions
Content added Content deleted
m (added a word (OF) to the task's preamble.) |
(Added FreeBASIC) |
||
Line 1,184: | Line 1,184: | ||
end program MaxSubSeq</lang> |
end program MaxSubSeq</lang> |
||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>' FB 1.05.0 Win64 |
|||
Dim As Integer seq(10) = {-1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1} |
|||
Dim As Integer i, j, sum, maxSum, first, last |
|||
maxSum = 0 |
|||
For i = LBound(seq) To UBound(seq) |
|||
sum = 0 |
|||
For j = i To UBound(seq) |
|||
' only proper sub-sequences are considered |
|||
If i = LBound(seq) AndAlso j = UBound(seq) Then Exit For |
|||
sum += seq(j) |
|||
If sum > maxSum Then |
|||
maxSum = sum |
|||
first = i |
|||
last = j |
|||
End If |
|||
Next j |
|||
Next i |
|||
If maxSum > 0 Then |
|||
Print "Maximum subsequence is from indices"; first; " to"; last |
|||
Print "Elements are : "; |
|||
For i = first To last |
|||
Print seq(i); " "; |
|||
Next |
|||
Print |
|||
Print "Sum is"; maxSum |
|||
Else |
|||
Print "Maximum subsequence is the empty sequence which has a sum of 0" |
|||
End If |
|||
Print |
|||
Print "Press any key to quit" |
|||
Sleep</lang> |
|||
{{out}} |
|||
<pre> |
|||
Maximum subsequence is from indices 2 to 7 |
|||
Elements are : 3 5 6 -2 -1 4 |
|||
Sum is 15 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |