Skip to main content
edited tags
Link
bbgodfrey
  • 62.1k
  • 18
  • 92
  • 160
Tweeted twitter.com/StackMma/status/1046505287115526144
edited tags
Link
added 20 characters in body
Source Link
m0nhawk
  • 3.9k
  • 1
  • 21
  • 35

Given a list of integers, find the largest sum of a contiguous subsequencesub-sequence.

{1, 2, 3, 4, 5, -1, 7, -4, -2}
(* 21 *)

Input= {1,2,3,4,5,-1,7,-4,-2}, Output=21 andAnd print the sub-array which is {1,2,3,4,5,-1,7}{1,2,3,4,5,-1,7}.

I am getting 15.

LargestContiguousSum[numbers_List] :=

LargestContiguousSum[numbers_List] := 
  Module[{maxcurrent = numbers[[1]], maximumglobal = numbers[[1]], i},
    For[i = 2, i < Length[numbers], i++, 
    maxcurrent = Max[numbers[[i]], maxcurrent + numbers[[i]]]; 
    If[maxcurrent > maximumglobal, maximumglobal = maxcurrent]];
   maximumglobal];
LargestContiguousSum[{1, 2, 3, 4, 5, -1, 7}]

maximumglobal]; LargestContiguousSum[{1, 2, 3, 4, 5, -1, 7}]

Given a list of integers, find the largest sum of a contiguous subsequence.

Input= {1,2,3,4,5,-1,7,-4,-2}, Output=21 and print the sub-array which is {1,2,3,4,5,-1,7}

I am getting 15.

LargestContiguousSum[numbers_List] :=

Module[{maxcurrent = numbers[[1]], maximumglobal = numbers[[1]], i},
For[i = 2, i < Length[numbers], i++, 
maxcurrent = Max[numbers[[i]], maxcurrent + numbers[[i]]]; 
If[maxcurrent > maximumglobal, maximumglobal = maxcurrent]]; 

maximumglobal]; LargestContiguousSum[{1, 2, 3, 4, 5, -1, 7}]

Given a list of integers, find the largest sum of a contiguous sub-sequence.

{1, 2, 3, 4, 5, -1, 7, -4, -2}
(* 21 *)

And print the sub-array which is {1,2,3,4,5,-1,7}.

I am getting 15.

LargestContiguousSum[numbers_List] := 
  Module[{maxcurrent = numbers[[1]], maximumglobal = numbers[[1]], i},
    For[i = 2, i < Length[numbers], i++, 
    maxcurrent = Max[numbers[[i]], maxcurrent + numbers[[i]]];
    If[maxcurrent > maximumglobal, maximumglobal = maxcurrent]];
   maximumglobal];
LargestContiguousSum[{1, 2, 3, 4, 5, -1, 7}]
added 383 characters in body
Source Link
user59769
user59769
Loading
Source Link
user59769
user59769
Loading