dimanche 18 août 2019

Given a vector of integers , return a vector with products of all other integers than ith integer [on hold]

Given vector q : 1,4,2,5,3

Given vector q : 1,0,2,4

I can't understand what's wrong with my code ,1 test case wouldn't pass. My Code:

vector<int> Products(vector<int> q) {
  int size = q.size();

  vector<int> vec = q;
  for (int i = 0; i < size - 1; i++) {
    int product = 1;
    auto it = q.begin();
    while (it != q.end()) {
      if (*it == q[i]) {
        it++;
      }
      product *= (*it);
      it++;
    }
    vec.push_back(product);
  }
  return vec;
}

Expected output 1: 120,30,60,24,40

Expected output 2: 0,8,0,0

Aucun commentaire:

Enregistrer un commentaire