Header Ads Widget

Responsive Advertisement

Finding the Largest Sum of Consecutive Elements | Javascript

  Finding the Largest Sum of Consecutive Elements  


find largest pair sum


Today we have another problem to discuss and try to solve in a better approach


Q:) We are given an array of integers arr and a positive integer n. Our task is to find the largest sum of n consecutive elements in the array arr.
 For example, given the array [1, 2, 3, 4, 3, 5, 4, 6, 7, 8] and n = 4, the largest sum of 4 consecutive elements is 26, which corresponds to the sum of [6, 7, 8].

Input:

An array of integers arr where 1 <= arr.length <= 10^4.

A positive integer n where 1 <= n <= arr.length.

Output:

Return a single integer representing the largest sum of n consecutive elements in the array arr.

Examples:

For arr = [1, 2, 3, 4, 3, 5, 4, 6, 7, 8] and n = 4, the output should be 26.

For arr = [2, 3, 4, 6, 5, 7, 9, 81, 343, 77, 34234] and n = 3, the output should be 343.

Note:

The function should handle edge cases where the length of the array is less than n.

The function should return 0 if the array arr is empty.

Sol:)

function sol(arr, n) {
    if (n > arr.length) {
        return 'num shoud be less than arr.length'
    } else {
        let result = 0;
        for (let i = 0; i < arr.length - n + 1; i++) {
            let tmp = 0;//
            for (let j = 0; j < n; j++) {
                tmp += arr[i + j]                 // console.log(tmp, "=000000000")
            }
            console.log(tmp,"asd")
            if (tmp > result) {
                result = tmp;
            }
            // console.log(tmp)
        }
        return result
    }
}
let result2 = sol([1, 2, 3, 4, 3, 5, 4, 6, 7, 8], 4)
console.log(result2,"largest value")

 Above code we explain step by step:

1: Function Declaration:

The function sol takes two parameters: arr, which is an array of numbers, and n, which is an integer.

2: Check if n is Valid:

if (n > arr.length) { ... }:
 This condition checks if the value of n is greater than the length of the array arr. If n is greater than the length of arr, it means n specifies a range larger than the array size, and the function returns a message indicating that the number should be less than the length of arr.

3: Initialization:

let result = 0;

 If n is valid, a variable result is initialized to 0. This variable will store the largest sum found during the calculation.

4: Nested Loop to Calculate Sums:

for (let i = 0; i < arr.length - n + 1; i++) { ... }:
This loop iterates through the array arr starting from index 0 up to arr.length - n. The loop is set up to calculate the sum of n consecutive elements.

5: Inside this loop, another loop runs n times:

for (let j = 0; j < n; j++) { ... }: This loop calculates the sum of n consecutive elements by iterating through n elements starting from index i. It accumulates the sum in the variable tmp.

6: Updating result:

if (tmp > result) { result = tmp; }:
 After calculating the sum of n consecutive elements, if the sum (tmp) is greater than the current result, result is updated to hold this larger sum. This way, the result always stores the largest sum found so far.

7: Return the Result:

After the nested loop completes, the function returns the final value of result, which represents the largest sum of n consecutive elements in the array arr.

8: Function Invocation:

The function sol is called with the array [1, 2, 3, 4, 3, 5, 4, 6, 7, 8] and the value 4 for n.

The result of the function call is stored in the variable result2.

9: Logging the Result:

console.log(result2, "largest value"): 

The largest sum of n consecutive elements is logged to the console along with a descriptive message.

Summary,

This code efficiently finds the largest sum of n consecutive elements in an array by iterating through the array and calculating the sum for every possible sequence of n consecutive elements. It returns the largest sum found.feel free to reach out and drop your query.
if you have a better idea to solve this question then let us know in the comment section

Post a Comment

0 Comments