title: Find index of n number of small elements next to each others in array
body: <p>I am stuck in a simple problem. I need to find index of n numbers of  smallest elements next to each others in an array. So if my array is [-1,-3,4,2,-5,-6] and n=3 my answer would be 5,4 and 1(because first 3 smallest elements are -6(index 5),-5(index 4) and -3(index 1)). What would be the best way to do it in C++ without any library function ? This is what i came up with :    </p>
  
  <pre><code>int getPos(int* arr,int n,int y)
  {
      for(int i=0;i&lt;n;i++)
      {
           if(arr[i]==y)
              return i;
      }
  }  
  
  
  int* sorted=new int[n];
  for(int i=0;i&lt;n;i++)
  {
      sorted[i]=diff[i];
  }
  sort(sorted,sorted+n);
  for(int i=0;i&lt;d;i++)
  {
      int y=sorted[i];
      int k=getPos(diff,n,y);
  }                      
  </code></pre>
  
  <p>Here diff is the unsorted array and sorted is the sorted array. This function works fine when there is no repeated element. But if my diff array is [-1,-1,2,-2,-2] and sorted array is [-2,-2,-1,-1,2] So in 1st iteration it grabs index for -2. Now in 2nd iteration it will again grab index of index of -2(1st -2 in my array not the 2nd) . So how to make my code work for repeating elements ?  </p>