# Online algorithm to find shortest subarray containing all k-elements

We have given array of $n$ integers in the range $[1, k]$. We want to design algorithm that can modify the array and also find the length of the shortest subarray containing at least one of each integers in the range $[1,k]$. $k$ can go up to 50, but $n$ can be large number, up to 100000.

I know that the offline version with fixed array can be solved in $O(NK)$ with sliding window across the array, but I don't know how to extend this or another algorithm to make it work online.

Please give me some hints where to start.

• I guess I was a bit too quick about the specific time complexity. But do try to implement the sliding window algorithm in an online version. – Yuval Filmus Apr 1 '18 at 9:33
• I don't have much experience with sliding window algorithms, is it possible to do the sliding window in online fashion, because it should scan the whole array for each update, since one number can change a lot, for example if on beginning the interval is very big, after one update it will become smaller. – someone12321 Apr 1 '18 at 17:22
• For interval, I mean the subarray containing all integers from 1 to K – someone12321 Apr 1 '18 at 17:23
• You should store some additional information that would help you update the data structure. – Yuval Filmus Apr 1 '18 at 17:29
• But still, when you update one element it changes a lot, I don't see how can we reduce to updating only one single element – someone12321 Apr 1 '18 at 18:10