Given a list of integers, find the largest sum of a contiguous subsequence
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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]
list-manipulation array
add a comment |Â
up vote
1
down vote
favorite
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]
list-manipulation array
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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]
list-manipulation array
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]
list-manipulation array
list-manipulation array
edited 1 hour ago
m0nhawk
2,49811531
2,49811531
asked 1 hour ago
Bignya ranjan Pathi
445
445
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
f = a [Function]
With[A = UpperTriangularize[Outer[Plus, Join[0, -Most[#]], #, 1] &[Accumulate[a]]],
MaximalBy[
Flatten[MapIndexed[x, i [Function] x, i, A, 2], 1],
First
][[1]]
];
sum, pos = f[1, 2, 3, 4, 5, -1, 7, -4, -2]
21, 1, 7
add a comment |Â
up vote
1
down vote
Here is a brute force approach modeled after your initial approach but with Do
rather than For
:
LargestContiguousSum[numbers_List] := Module[n, mSum, index, sum, y, accumulate,
n = Length[numbers];
accumulate = Accumulate[numbers];
mSum = Max[accumulate];
index = 1;
Do[sum = Max[accumulate[[i ;; n]]] - accumulate[[i - 1]];
If[sum > mSum, mSum = sum; index = i], i, 2, n];
y = accumulate[[index ;; n]] - If[index == 1, 0, accumulate[[index - 1]]];
Max[y], numbers[[index ;; index + Position[y, Max[y]][[1, 1]] - 1]]]
maxSum[1, 2, 3, 4, 5, -1, 7, -4, -2]
(* 21,1,2,3,4,5,-1,7 *)
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
f = a [Function]
With[A = UpperTriangularize[Outer[Plus, Join[0, -Most[#]], #, 1] &[Accumulate[a]]],
MaximalBy[
Flatten[MapIndexed[x, i [Function] x, i, A, 2], 1],
First
][[1]]
];
sum, pos = f[1, 2, 3, 4, 5, -1, 7, -4, -2]
21, 1, 7
add a comment |Â
up vote
2
down vote
accepted
f = a [Function]
With[A = UpperTriangularize[Outer[Plus, Join[0, -Most[#]], #, 1] &[Accumulate[a]]],
MaximalBy[
Flatten[MapIndexed[x, i [Function] x, i, A, 2], 1],
First
][[1]]
];
sum, pos = f[1, 2, 3, 4, 5, -1, 7, -4, -2]
21, 1, 7
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
f = a [Function]
With[A = UpperTriangularize[Outer[Plus, Join[0, -Most[#]], #, 1] &[Accumulate[a]]],
MaximalBy[
Flatten[MapIndexed[x, i [Function] x, i, A, 2], 1],
First
][[1]]
];
sum, pos = f[1, 2, 3, 4, 5, -1, 7, -4, -2]
21, 1, 7
f = a [Function]
With[A = UpperTriangularize[Outer[Plus, Join[0, -Most[#]], #, 1] &[Accumulate[a]]],
MaximalBy[
Flatten[MapIndexed[x, i [Function] x, i, A, 2], 1],
First
][[1]]
];
sum, pos = f[1, 2, 3, 4, 5, -1, 7, -4, -2]
21, 1, 7
edited 1 hour ago
answered 1 hour ago
Henrik Schumacher
40.1k255120
40.1k255120
add a comment |Â
add a comment |Â
up vote
1
down vote
Here is a brute force approach modeled after your initial approach but with Do
rather than For
:
LargestContiguousSum[numbers_List] := Module[n, mSum, index, sum, y, accumulate,
n = Length[numbers];
accumulate = Accumulate[numbers];
mSum = Max[accumulate];
index = 1;
Do[sum = Max[accumulate[[i ;; n]]] - accumulate[[i - 1]];
If[sum > mSum, mSum = sum; index = i], i, 2, n];
y = accumulate[[index ;; n]] - If[index == 1, 0, accumulate[[index - 1]]];
Max[y], numbers[[index ;; index + Position[y, Max[y]][[1, 1]] - 1]]]
maxSum[1, 2, 3, 4, 5, -1, 7, -4, -2]
(* 21,1,2,3,4,5,-1,7 *)
add a comment |Â
up vote
1
down vote
Here is a brute force approach modeled after your initial approach but with Do
rather than For
:
LargestContiguousSum[numbers_List] := Module[n, mSum, index, sum, y, accumulate,
n = Length[numbers];
accumulate = Accumulate[numbers];
mSum = Max[accumulate];
index = 1;
Do[sum = Max[accumulate[[i ;; n]]] - accumulate[[i - 1]];
If[sum > mSum, mSum = sum; index = i], i, 2, n];
y = accumulate[[index ;; n]] - If[index == 1, 0, accumulate[[index - 1]]];
Max[y], numbers[[index ;; index + Position[y, Max[y]][[1, 1]] - 1]]]
maxSum[1, 2, 3, 4, 5, -1, 7, -4, -2]
(* 21,1,2,3,4,5,-1,7 *)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here is a brute force approach modeled after your initial approach but with Do
rather than For
:
LargestContiguousSum[numbers_List] := Module[n, mSum, index, sum, y, accumulate,
n = Length[numbers];
accumulate = Accumulate[numbers];
mSum = Max[accumulate];
index = 1;
Do[sum = Max[accumulate[[i ;; n]]] - accumulate[[i - 1]];
If[sum > mSum, mSum = sum; index = i], i, 2, n];
y = accumulate[[index ;; n]] - If[index == 1, 0, accumulate[[index - 1]]];
Max[y], numbers[[index ;; index + Position[y, Max[y]][[1, 1]] - 1]]]
maxSum[1, 2, 3, 4, 5, -1, 7, -4, -2]
(* 21,1,2,3,4,5,-1,7 *)
Here is a brute force approach modeled after your initial approach but with Do
rather than For
:
LargestContiguousSum[numbers_List] := Module[n, mSum, index, sum, y, accumulate,
n = Length[numbers];
accumulate = Accumulate[numbers];
mSum = Max[accumulate];
index = 1;
Do[sum = Max[accumulate[[i ;; n]]] - accumulate[[i - 1]];
If[sum > mSum, mSum = sum; index = i], i, 2, n];
y = accumulate[[index ;; n]] - If[index == 1, 0, accumulate[[index - 1]]];
Max[y], numbers[[index ;; index + Position[y, Max[y]][[1, 1]] - 1]]]
maxSum[1, 2, 3, 4, 5, -1, 7, -4, -2]
(* 21,1,2,3,4,5,-1,7 *)
answered 30 mins ago
JimB
15.3k12657
15.3k12657
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182882%2fgiven-a-list-of-integers-find-the-largest-sum-of-a-contiguous-subsequence%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password