Finding an array product except itself
[2,3,4,5]:
for above example to find a vector in which we can take product of all except the element itself.
so here :
for ist element arr[0] the product will be 3×4×5,for 2nd element the product will be 2×4×5 ,for third 2×3×5 and for last element the product will be 2×3×4
taking other examples like an array [2,4,6,0,8,0,5];
for this the whole array will be zero because if take for ist element the product will be zero similarly for the other elements as well.
// special case when an array is [40,0] :
for this example the resultant array will be [0,40]
to solve this problem here are the steps:
1.take a prod variable which will keep track of product of nonzeros elements,zero_cnt variable ,zero_idx,is_zero boolean to keep track of is zero present in the array or not .
2.iterating through the array we will get product of non zero element as well as zero count .
3.if zero present we will check for two conditions 1.if zero_cnt>1 then whole array becomes zero otherwise if zero count is 1 then only non zero becomes zero.
4.if not zero then for each element will update the value as prod/element