Untitled

 avatar
unknown
plain_text
2 years ago
600 B
5
Indexable
class ProductOfNumbers {
public:
    vector<int>pdt;
    ProductOfNumbers() {
        pdt={1};
    }
    
    void add(int num) {
        if(num==0){
            pdt={1};
        }
        else{
            pdt.push_back(pdt.back()*num);
        }   
    }

    int getProduct(int k) {
        int n=pdt.size();
        if(k<n) return pdt.back()/pdt[n-k-1];
        return 0;
    }
};

/**
 * Your ProductOfNumbers object will be instantiated and called as such:
 * ProductOfNumbers* obj = new ProductOfNumbers();
 * obj->add(num);
 * int param_2 = obj->getProduct(k);
 */
Editor is loading...
Leave a Comment