-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductOfNumbers.java
More file actions
37 lines (31 loc) · 1.06 KB
/
Copy pathProductOfNumbers.java
File metadata and controls
37 lines (31 loc) · 1.06 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
import java.util.ArrayList;
import java.util.List;
public class ProductOfNumbers {
private List<Integer> list;
public ProductOfNumbers() {
list = new ArrayList<>();
}
public void add(int num) {
list.add(num);
}
public int getProduct(int k) {
int num=1, size= list.size();
for(int i=size-1; i>=size-k;i--) {
num = num* list.get(i);
}
return num;
}
public static void main(String[] args) {
ProductOfNumbers productOfNumbers = new ProductOfNumbers();
productOfNumbers.add(3); // [3]
productOfNumbers.add(0); // [3,0]
productOfNumbers.add(2); // [3,0,2]
productOfNumbers.add(5); // [3,0,2,5]
productOfNumbers.add(4); // [3,0,2,5,4]
System.out.println(productOfNumbers.getProduct(2));
System.out.println(productOfNumbers.getProduct(3));
System.out.println(productOfNumbers.getProduct(4));
productOfNumbers.add(8);
System.out.println(productOfNumbers.getProduct(2));
}
}