Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given an integer array arr, consisting of N integers. Find the maximum possible sum of consecutive numbers
- in arr. For example: if the array arr consists of the numbers 1, 6, -9, 4, 4, -2, 10, -1, the maximum possible sum
- of consecutive numbers is 16 (the consecutive numbers are 4, 4, -2 and 10). Element 0 of the array is the number N.
- Next N elements (from 1 to N) construct the array arr. Your method should return a single number -
- the maximum possible sum of consecutive numbers.*/
- "use strict";
- function solve(args) {
- var maxSum = -2000001;
- for (var i = 1; i < args.length; i += 1) {
- var sum = 0;
- for (var j = i; j < args.length; j += 1) {
- sum += parseInt(args[j]);
- if (sum > maxSum) {
- maxSum = sum;
- }
- }
- }
- console.log(maxSum);
- }
- solve([ '8', '1', '6', '-9', '4', '4', '-2', '10', '-1' ]);
- solve([ '6', '1', '3', '-5', '8', '7', '-6' ]);
- solve([ '9', '-9', '-8', '-8', '-7', '-6', '-5', '-1', '-7', '-6' ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement