title: Finding contiguous ranges in arrays
body: <p>You are given an array of integers. You have to output the largest range so that all numbers in the range are present in the array. The numbers might be present in any order.  For example, suppose that the array is</p>
  
  <pre><code>{2, 10, 3, 12, 5, 4, 11, 8, 7, 6, 15}
  </code></pre>
  
  <p>Here we find two (nontrivial) ranges for which all the integers in these ranges are present in the array, namely [2,8] and [10,12].  Out of these [2,8] is the longer one. So we need to output that.</p>
  
  <p>When I was given this question, I was asked to do this in linear time and without using any sorting.  I thought that there might be a hash-based solution, but I couldn't come up with anything.</p>
  
  <p>Here's my attempt at a solution:</p>
  
  <pre><code>void printRange(int arr[])
  {
      int n=sizeof(arr)/sizeof(int);
      int size=2;
      int tempans[2]; 
  
      int answer[2];// the range is stored in another array
      for(int i =0;i&lt;n;i++)
      {
          if(arr[0]&lt;arr[1])
          {
               answer[0]=arr[0];
               answer[1]=arr[1];
          }
          if(arr[1]&lt;arr[0])
          {
              answer[0]=arr[1];
              answer[1]=arr[0];
          }
  
          if(arr[i] &lt; answer[1])
              size += 1;
          else if(arr[i]&gt;answer[1]) {
              initialize tempans to new range;
               size2=2;
          }
          else { 
              initialize tempans  to new range
          }
  }
  
  //I have to check when the count becomes equal to the diff of the range
  </code></pre>
  
  <p>I am stuck at this part... I can't figure out how many tempanswer[] arrays should be  used.</p>