title: Merge Overlapping Intervals
body: <p>Question : Given a set of time intervals in any order, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for simplicity. 
  For example, let the given set of intervals be {{1,3}, {2,4}, {5,7}, {6,8} }. The intervals {1,3} and {2,4} overlap with each other, so they should be merged and become {1, 4}. Similarly {5, 7} and {6, 8} should be merged and become {5, 8}</p>
  
  <p>Write a function which produces the set of merged intervals for the given set of intervals.</p>
  
  <p>My code:</p>
  
  <pre><code>import java.util.*;
  import java.lang.*;
  import java.io.*;
  
  class Interval 
  {
      int start;
      int end;
  
      Interval() {
          start = 0;
          end = 0;
      }
  
      Interval(int s, int e) 
      {
          start = s;
          end = e;
      }
  }
  
  class Ideone
  {
      public ArrayList&lt;Interval&gt; merge(ArrayList&lt;Interval&gt; intervals) {
  
          if(intervals.size() == 0)
              return intervals;
          if(intervals.size() == 1)
              return intervals;
  
          Collections.sort(intervals, new IntervalComparator());
  
          Interval first = intervals.get(0);
          int start = first.start;
          int end = first.end;
  
          ArrayList&lt;Interval&gt; result = new ArrayList&lt;Interval&gt;();
  
          for(int i = 1; i &lt; intervals.size(); i++){
              Interval current = intervals.get(i);
              if(current.start &lt;= end){
                  end = Math.max(current.end, end);
              }else{
                  result.add(new Interval(start, end));
                  start = current.start;
                  end = current.end;
              }
  
          }
  
          result.add(new Interval(start, end));
  
          return result;
  
      }
  }
  
  class IntervalComparator implements Comparator
  {
      public int compare(Object o1, Object o2)
      {
          Interval i1 = (Interval)o1;
          Interval i2 = (Interval)o2;
          return i1.start - i2.start;
      }
  }
  public static void main (String[] args) throws java.lang.Exception
  {
      ArrayList&lt;Interval&gt; x = new ArrayList&lt;Interval&gt;();
      Interval i1 = new Interval(1,3);
      Interval i2 = new Interval(2,6);
      Interval i3 = new Interval(8,10);
      Interval i4 = new Interval(15,18);
  
      x.add(i1);x.add(i2);x.add(i3);x.add(i4);
  
      ArrayList&lt;Interval&gt; r = merge(x);
  
      for(Interval i : r)
      {
          System.out.println(i.start+" "+i.end);
      }
  
  }
  }
  </code></pre>
  
  <p>These are the errors which i got after compiling, can anyone please explain me how to rectify it?</p>
  
  <pre><code>Main.java:69: error: class, interface, or enum expected
  public static void main (String[] args) throws java.lang.Exception
                ^
  Main.java:72: error: class, interface, or enum expected
      Interval i1 = new Interval(1,3);
      ^
  Main.java:73: error: class, interface, or enum expected
      Interval i2 = new Interval(2,6);
      ^
  Main.java:74: error: class, interface, or enum expected
      Interval i3 = new Interval(8,10);
      ^
  Main.java:75: error: class, interface, or enum expected
      Interval i4 = new Interval(15,18);
      ^
  Main.java:77: error: class, interface, or enum expected
      x.add(i1);x.add(i2);x.add(i3);x.add(i4);
      ^
  Main.java:77: error: class, interface, or enum expected
      x.add(i1);x.add(i2);x.add(i3);x.add(i4);
                ^
  Main.java:77: error: class, interface, or enum expected
      x.add(i1);x.add(i2);x.add(i3);x.add(i4);
                          ^
  Main.java:77: error: class, interface, or enum expected
      x.add(i1);x.add(i2);x.add(i3);x.add(i4);
                                    ^
  Main.java:79: error: class, interface, or enum expected
          ArrayList&lt;Interval&gt; r = merge(x);
          ^
  Main.java:81: error: class, interface, or enum expected
          for(Interval i : r)
          ^
  Main.java:84: error: class, interface, or enum expected
          }
          ^
  12 errors
  </code></pre>