Sliding sum of size k
Java
Medium
5 views
Problem Description
Task: return sum of every window of size k as long array.
Output Format
Return value
Constraints
k between 1..n.
Official Solution
static long[] windowSums(int[] a,int k){int n=a.length;long[] r=new long[n-k+1];long s=0;for(int i=0;i<n;i++){s+=a[i];if(i>=k) s-=a[i-k];if(i>=k-1) r[i-k+1]=s;}return r;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!