Skip to content

Commit e3c0a53

Browse files
committed
complexity analysis to find sum and product of array
1 parent bd74d53 commit e3c0a53

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package BasicPractice;
2+
3+
// complexity: O(N)
4+
// This will take 0 (N) time. The fact that we iterate through the array twice doesn't matter.
5+
6+
public class complexityAnaysis1 {
7+
8+
public static void main(String[] args) {
9+
10+
int[] arr = { 1, 2, 3, 4, 5, 6 };
11+
SumProduct(arr);
12+
}
13+
14+
public static void SumProduct(int[] arr) {
15+
int sum = 0;
16+
int product = 1;
17+
18+
for (int i = 0; i < arr.length; i++) {
19+
sum += arr[i];
20+
}
21+
for (int i = 0; i < arr.length; i++) {
22+
product *= arr[i];
23+
}
24+
25+
System.out.println("sum: " + sum + " product: " + product);
26+
}
27+
}
28+
29+
// output: sum: 21 product: 720

0 commit comments

Comments
 (0)