title: Function returning the wrong kth value while using sets
body: <p>I was attempting to solve this question on some website where you have the find the kth smallest value in c++ so I came up with:</p>
  <pre><code>#include &lt;bits/stdc++.h&gt;
  using namespace std;
  int kthSmallest(int arr[], int l, int r, int k) {
  
      // l is the first index 
      // r is the index of the last element (size - 1)
      // k is the kth smallest value 
      set&lt;int&gt; s(arr, arr + r); 
  
      
      set&lt;int&gt;:: iterator itr = s.begin();
      advance(itr, (k - 1));
      
      return *itr;
  }
  int main() {
    int arr[] = {7, 10, 4, 20, 15};
    cout &lt;&lt; kthSmallest(arr, 0, 4, 4);
    return 0;
  }
  </code></pre>
  <p>This shows the output of 20 instead of 15 which is the right answer and I cannot figure out what I did wrong here.</p>