50,077 views
34 votes
34 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 Aman Mahajan
by
2.7k points

1 Answer

16 votes
16 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 Gallaxhar
by
2.9k points