# Product of Array Except Self

Nov 08, 2017

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Follow up: Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

int Div(unsigned  a,unsigned b) {
int x,y;
int ans = 0;
while(a >= b) {
x = b;
y = 1;
while( a >= (x<<1)) {
x <<= 1;
y <<= 1;
}
a -= x;
ans += y;
}
return ans;
}
int div(int a,int b) {
if (a > 0 && b > 0) return Div(a,b);
else if (a < 0 && b < 0) return Div(-a,-b);
else if (a < 0) return -Div(-a,b);
else return -Div(a,-b);
}


vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ret(nums.size(),0);
long long product = 1;
int zero_count = 0;
for(auto i:nums)
if (i != 0) product*=i;
else zero_count++;
cout << zero_count << endl;

if (zero_count > 1) return ret;

if (zero_count == 1) {
for(int i = 0;i < nums.size();i++) {
if (nums[i] != 0) ret[i] = 0;
else ret[i] = product;
}
return ret;
}

for(int i = 0;i < ret.size();i++) {
//    if (nums[i] == 0) ret[i] = product[i];
ret[i] = div((int)product,nums[i]);
}
return ret;
}


def productExceptSelf(self, nums):
p = 1
n = len(nums)
output = []
for i in range(0,n):
output.append(p)
p = p * nums[i]
p = 1
for i in range(n-1,-1,-1):
output[i] = output[i] * p
p = p * nums[i]
return output


LeetCodeLeetCode

Move-Zeroes

Lowest Common Ancestor of a Binary Tree