You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 410e893
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js
+8-7Lines changed: 8 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -9,21 +9,22 @@
9
9
* "product": 30 // 2 * 3 * 5
10
10
* }
11
11
*
12
-
* Time Complexity:
13
-
* Space Complexity:
14
-
* Optimal Time Complexity:
12
+
* Time Complexity: O(n)
13
+
* Space Complexity:O(1)
14
+
* Optimal Time Complexity:O(n)
15
15
*
16
16
* @param {Array<number>} numbers - Numbers to process
17
17
* @returns {Object} Object containing running total and product
18
18
*/
19
19
exportfunctioncalculateSumAndProduct(numbers){
20
20
letsum=0;
21
-
for(constnumofnumbers){
22
-
sum+=num;
23
-
}
24
-
25
21
letproduct=1;
22
+
23
+
// Initial code looped twice through the array, i.e. 2 operations (n + n) = O(2n)operations => O(n). Limiting the loop to just one operation makes it n operation => O(n).
24
+
// The space complexity is constant as the variables do not grow when the function is called.
0 commit comments