-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcalculateSumAndProduct.js
More file actions
57 lines (52 loc) · 1.64 KB
/
calculateSumAndProduct.js
File metadata and controls
57 lines (52 loc) · 1.64 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Calculate the sum and product of integers in a list
*
* Note: the "sum" is every number added together
* and the "product" is every number multiplied together
* so for example: [2, 3, 5] would return
* {
* "sum": 10, // 2 + 3 + 5
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
*/
// export function calculateSumAndProduct(numbers) {
// let sum = 0;
// for (const num of numbers) {
// sum += num;
// }
// let product = 1;
// for (const num of numbers) {
// product *= num;
// }
// return {
// sum: sum,
// product: product,
// };
// }
//My Analysis report
// the function have two loops which looks similar and can be simplified, they makes the time complexity of the function
// a numbers.length times which is O(n). for the two loops time complexity becomes O(n) + O(n) = O(2n)
// after looping it stores the results on the two variables i.e. sum and product which have O(1) + O(1) space complexity.
// the space complexity related to arr size is unavoidable/ unchangeable and is optimal in this case , which is O(n)
// And The area of inefficiency for this code is on the loop (i.e. looping twice)
//refactored code for better efficiency.
//what i have done here is i use a single loop to change the efficiency from o(2n) to o(n)
export function calculateSumAndProduct(numbers) {
let sum = 0;
let product = 1;
for (const num of numbers) {
sum += num;
product *= num;
}
return {
sum: sum,
product: product,
};
}