Jump to content

Greatest subsequential sum: Difference between revisions

No edit summary
Line 164:
}
=={{header|D}}==
<pre>module submax;
import std.stdio;
 
T subsum(T = int)(T[] a) {
T sum = 0 ;
foreach(e ; a)
sum += e ;
return sum ;
}
 
T[] submax(T = int)(T[] a) {
T[] maxsub ;
T maxsum = T.min ;
for(int i = 0 ; i < a.length ; i++)
for(int j = i + 1 ; j <= a.length ; j++)
if (maxsum < a[i..j].subsum()){
maxsum = a[i..j].subsum() ;
maxsub= a[i..j].dup ;
}
return maxsub ;
}
 
void main() {
auto array = [-1L , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1] ;
writefln("sum of %s = %s", array.submax(), array.submax().subsum()) ;
}</pre>
 
=={{header|Forth}}==
2variable best
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.