vector<int> productExceptSelf(vector<int>& arr) {
// code here
vector<int> res(arr.size(), 0);
int totalProduct = 1;
int zero = 0, index=-1;
for(int i=0; i<arr.size(); i++)
{
if(arr[i] == 0) zero++, index = i;
else totalProduct *= arr[i];
}
if(zero == 0) {
for(int i=0; i<arr.size(); i++) {
res[i] = totalProduct / arr[i];
}
} else if(zero == 1) {
res[index] = totalProduct;
}
return res;
}
GFG PTOD | 21 Jan 2025 | Linked List Group Reverse
Recursive Solution : Iterative Solution :