Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
600 B
2
Indexable
Never
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);
 */
Leave a Comment