-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumOf1DArray.js
More file actions
29 lines (23 loc) · 915 Bytes
/
sumOf1DArray.js
File metadata and controls
29 lines (23 loc) · 915 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
/*
Optimized Attempt
Time Complexity = O(N) - Because we have to itrate on each and every element in array
Space Complexity = O(N) - Space Complexity should be constant because we are creating a new result array//
*/
function sumOf1DArray(array){
let result = [];
result.push(array[0]);
for (let i = 1; i < array.length; i++) {
result[i] = result[i - 1] + array[i];
}
return result;
}
/*
Steps For Optimized Solution
1 - Create new result Array
2 - Push input array 1st element into that final array
3 - Itrate over the rest of the remaining elements in input array
4 - Push in final result variable from itrator ( from 1 ).
5 - Get the previous value from result variable the one we push ( from i - 1 ) add current input array value to that +array[i]
6 - return the final result array when itration ends
*/
console.log(sumOf1DArray([1,2,3,4]));