hackerrank

A Very Big Sum solution using JavaScript

Below is my JavaScript solution to the A Very Big Sum challenge on HackerRank.

Time Complexity: Because each element in the array will be visited only one time, the time complexity is O(n), where n represents the number of elements in the array.

Space Complexity: This approach will use a constant amount of space, making the space complexity O(1).

/**
 * Calculate and print the sum of the elements in an array
 *
 *   Time Complexity: O(n)
 *   Space Complexity: O(1)
 *
 *   Input: [1, 2, 3]
 *   Output: 6
 */
function aVeryBigSum(ar) {
    return ar.reduce((acc, num) => acc += num);
}

more HackerRank posts

more JavaScript posts