4.3k views
25 votes
Write a function solution that given an array a of n integers (between -100 and 100), returns the sign (-1,0,1) of product of all the numbers in the array multiplied

User Rakesh L
by
5.1k points

1 Answer

7 votes

left[0]=a[0];

for(int i=1;i<=n-1;i++)

left[i]=(left[i-1]*a[i])%M;

right[n-1]=a[n-1];

for(int i=n-2;i>=0;i--)

right[i]=(right[i-1]*a[i])%M;

for query q

if(q==0)

return right[1]%M;

if(q==n-1)

return left[n-2]%M;

return (left[q-1]*right[q+1])%M;

User Duncan McGregor
by
4.4k points