public static int gaps(int[] start,int[] end){
  Arrays.sort(start);
  Arrays.sort(end);
  int overlap=0;
  int gaps=0;
  for (int i=0, j=0; j < end.length; ) {
    if (i < start.length && start[i] < end[j]) {
      overlap++;
      i++;
    }
 else {
      j++;
      overlap--;
    }
    if (overlap == 0) {
      gaps++;
    }
  }
  return gaps;
}

public static int gaps ( int [ ] start , int [ ] end ) { Arrays . sort ( start ) ; Arrays . sort ( end ) ; int overlap = 0 ; int gaps = 0 ; for ( int i = 0 , j = 0 ; j < end . length ; ) { if ( i < start . length && start [ i ] < end [ j ] ) { overlap ++ ; i ++ ; } else { j ++ ; overlap -- ; } if ( overlap == 0 ) { gaps ++ ; } } return gaps ; }
calculate the number of gaps + 1 given a start and end range
