-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1352.java
More file actions
36 lines (33 loc) · 872 Bytes
/
Problem1352.java
File metadata and controls
36 lines (33 loc) · 872 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
30
31
32
33
34
35
36
import java.util.ArrayList;
class ProductOfNumbers {
ArrayList<Integer> list=new ArrayList<>();
public ProductOfNumbers() {
}
public void add(int num) {
list.add(num);
}
public int getProduct(int k) {
int ans=1;
int n=list.size()-1;
for(int i=1;i<=k;i++){
ans=ans*list.get(n--);
}
return ans;
}
}
public class Problem1352 {
// Product of the Last K Numbers
public static void main(String[] args) {
ProductOfNumbers np=new ProductOfNumbers();
np.add(3);
np.add(0);
np.add(2);
np.add(5);
np.add(4);
System.out.println(np.getProduct(2));
System.out.println(np.getProduct(3));
System.out.println(np.getProduct(4));
np.add(8);
System.out.println(np.getProduct(2));
}
}