MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / ProductOfNumbers

Class ProductOfNumbers

ProductOfTheLastKNumbers.java:1–22  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class ProductOfNumbers {
2 ArrayList<Integer> list = new ArrayList<>();
3 public ProductOfNumbers() {
4 list.clear();
5 }
6
7 public void add(int num) {
8 if(num==0){
9 list.clear();
10 return;
11 }
12 int prev = (list.size()==0)?1:list.get(list.size()-1);
13 list.add(prev * num);
14 }
15
16 public int getProduct(int k) {
17 int s = list.size();
18 if(s<k) return 0;
19 else if(s == k) return list.get(s-1);
20 else return (list.get(s-1) / list.get(s-1-k));
21 }
22}
23
24/**
25 * Your ProductOfNumbers object will be instantiated and called as such:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected