Q:

Find the shortest range in the array which contains all the elements present in the array

Consider an array A[] which can contain non distinct integers, how can I find the shortest range which covers all the elements present in the array? 
A[]  =  7 3 1 7 3 1 3 4 1
index=  0 1 2 3 4 5 6 7 8

Elements in the range [3,7] form the shortest range present in the array. 
Hence the answer must be 5. 
How can I solve this problem using double pointer? 
P.S.:This question is not from any live contests. 
My try: I had taken a variable len which i would vary from 1 to length of array and for each len check whether it covers all the elements or not. 

A:

Let's array length be N and the number of distinctive elements be K, K<=N.
Let's assume the values are 0, 1, ..., K-1. 
(We can sort all elements, then replace elements with minimum value with 0, elements with second minimum value with 1, etc.,  then put them back to the original positions.  It requires O(N*log(N) complexity.) 
Make an array C[K], where C[i] should keep count of elements with value i in a segment [x, y]. 
Let D be a number of distinct elements in a segment. 
If we extend such segment by increasing y, we increase C[A[y+1]] by one. 
If C[A[y+1]] becomes 1, we increase D by 1. 
If we shrink the segment by increasing x, we decrease C[A[x+1]] by one. 
If C[A[x+1]] becomes 0, we decrease D by 1.  
So, if D==K we know that the segment contains all elements. 
Now start from [0, 0] and keep extending the segment till it contains all possible values.  Ley it be [0, q]. 
Then shrink it while the resulting segment still has elements with all possible values.  Let it be [p, q]. 
Move that [p, q] right by one position, i.e. to [p+1, q+1]. 
Again, try to shrink the segment.  Etc. 
So, roughly speaking, move the segment from left to right, and try shrinking it on every step.  When you reach the end and cannot move the left end any more, you are done.  It is O(N).  So all together (including sorting): O(N*log(N)).