Product of array except self
Java
Hard
7 views
Problem Description
Task: return array where res[i] = product of all elements except a[i] (no division).
Output Format
Return value
Official Solution
static long[] productExceptSelf(int[] a){int n=a.length;long[] res=new long[n];long pref=1;for(int i=0;i<n;i++){res[i]=pref;pref*=a[i];}long suf=1;for(int i=n-1;i>=0;i--){res[i]*=suf;suf*=a[i];}return res;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!