Skip to content

Instantly share code, notes, and snippets.

@jonasfrey
Last active February 27, 2024 16:02
Show Gist options
  • Save jonasfrey/ca0329057c087d258e46c9aea76e7470 to your computer and use it in GitHub Desktop.
Save jonasfrey/ca0329057c087d258e46c9aea76e7470 to your computer and use it in GitHub Desktop.
maximale_teilsumme.js
let a_n = [
1, -2, -3, -10, 10, -10,-3, -2, -3, 322, -322,-4, -4, 1234
];
let n_sum_max = 0;
let n_idx_start_sum_max = 0;
let n_idx_end_sum_max = 0;
let n_idx_start = 0;
let n_idx_end = a_n.length;
for(let n1 = n_idx_start; n1 < a_n.length; n1+=1){
for(let n2 = a_n.length; n2 >= n1; n2-=1){
let n_sum = a_n.slice(n1, n2).reduce((n_prev, n_curr)=>n_prev + n_curr, 0)
// console.log(n_sum)
if(n_sum > n_sum_max){
n_sum_max = n_sum;
n_idx_start_sum_max = n1;
n_idx_end_sum_max = n2
}
}
}
console.log(
{
n_sum_max,
n_idx_start_sum_max,
n_idx_end_sum_max
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment