-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathBasicPrefixSum.js
More file actions
30 lines (25 loc) · 700 Bytes
/
BasicPrefixSum.js
File metadata and controls
30 lines (25 loc) · 700 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* BasicPrefixSum.js
* Implementation of Prefix Sum array.
*
* @param {number[]} arr - Input array of numbers.
* @returns {number[]} Prefix sum array.
* @throws {TypeError} If input is not an array of numbers.
*
* Explanation:
* Given [1,2,3,4], returns [1,3,6,10]
*/
export function basicPrefixSum(arr) {
// Validate input
if (!Array.isArray(arr) || arr.some((x) => typeof x !== 'number')) {
throw new TypeError('Input must be an array of numbers')
}
// Handle empty array
if (arr.length === 0) return []
const prefix = new Array(arr.length)
prefix[0] = arr[0]
for (let i = 1; i < arr.length; i++) {
prefix[i] = prefix[i - 1] + arr[i]
}
return prefix
}