title: Reducing the overlapping count number of buses running from a city to different stopage in straight line, and can my current approach be optimised?
body: <p>Given pair of distance of start distance and end distance, reduces the number of buses if there is an overlapping in two stopage.
  for e.g:</p>
  <pre><code>input n=4
  {2,8},{6,10},{12,14},{12,20}
  output:2
  explanation:route {2,8} and {6,10} overlap so it can be reduced to 1 route as {2,10}
  similarly route {12,14} and {12,20} overlap it can be merged to 1.
  
  input n=4
  {1,3} ,{7,9}, {4,6},{10,13}
  output=4 since there is no overlapping
  </code></pre>
  <p>my approach: I have tried to sort <code>vector&lt;pair&lt;int,int&gt;&gt;</code> in descending order,and push into <code>stack&lt;pair&lt;int,int&gt;&gt;</code> and have used a counter <code>count</code> that counts the unique route and pops the stack checking the condition,till its gets empty. <strong>Despite my code appears 100% correct to me but it does not output anything when there is no overlapping in route.</strong>, for e.g for input <code>n=4</code>, route: <code>{1,3} ,{7,9}, {4,6},{10,13}</code> it does not output anything. looking for help to tackle such problem.</p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;algorithm&gt;
  #include&lt;vector&gt;
  #include&lt;stack&gt;
  using namespace std;
  bool compare(pair&lt;int,int&gt;p1,pair&lt;int,int&gt;p2){
      return p1.first&gt;p2.first;
  }
  int main(){
      int n;cin&gt;&gt;n;
      vector&lt;pair&lt;int,int&gt;&gt;v;
      for(int i=0;i&lt;n;i++){
          int u,w;
          cin&gt;&gt;u&gt;&gt;w;
          v.push_back(make_pair(u,w));
      }
      sort(v.begin(),v.end(),compare);
      stack&lt;pair&lt;int,int&gt;&gt;st;
      for(int i=0;i&lt;v.size();i++){
          st.push(v[i]);
      }
      //looking for help from here ,basically i dont understands why my code in stack fails
      int count=0;
      while (!st.empty()){
          pair&lt;int,int&gt;y=st.top();
          st.pop();
          count++;
          if(y.second&gt;=st.top().first){
              if(st.size()&gt;0){
                   st.pop();
              }
          }
  
      }
      cout&lt;&lt;count;
  }
  </code></pre>