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 | 20 Jan 2025 | Merge two sorted linked lists
Iterative Solution : TC = O(m+n), SC = O(1) Recursive Solution : TC = O(m+n), SC = O(1)