import java.util.*;
public class Solution {
    
    public static void main (String[] args)
    {
            //Test case 1 
            List<List<Object>> intervals = new ArrayList<>();
            
            List<Object> entries = new ArrayList<>();
            //entries.add("[1, 4]");
            //entries.add("[2, 4]");
            
            System.out.println("{1,3}, {2,8}, {6,10}, {11,14}");
            entries.add("[1, 3]");
            entries.add("[2, 8]");
            entries.add("[6, 10]");
            entries.add("[11, 14]");
            
            
            intervals.add(entries);
            
            System.out.println(mergeIntervals(intervals));
            
            
        //mergeIntervals(new ArrayList<>(Arrays.asList(["[1,4]", "[4,5]"])));
    }
    
    public static List<List<Object>> mergeIntervals(List<List<Object>> intervals) {

        List<List<Object>> outerList = new ArrayList<>();

        List<Object> innerList = new ArrayList<>();  //this is the list inside list to satisfy return type

        int counter=0;

        String odd="";
        String even="";

        int endFirstRange;
        int startSecondRange;

        String evenSubStringStartInterval="";
        String oddSubStringEndInteveral="";

        String oddSubStringStartInterval="";
        String evenSubStringEndInterval="";
        
        String newInterval="";
        
        boolean overlap=false;
        
        boolean newPairRangesMerged=false;
        
        //first need to understand that return type is List < List<Integer> >
        //I am not sure if System.out.println() will print this since I am typically not used to having a collection within a collection..
        //Since we are returning variable of this datatype, I presume the test environment will take care of this scenario..
        // It will only become an issue once I configure the driver class with main method

        //Also require an iterator through the list

        Iterator<List<Object>> it = intervals.iterator();

        //since the data inside mergeIntervals is a List, I think the hasNext() might not be applicable
        //since this would refer to a String....
        
        //need to iterate through the List items differently
        
        System.out.println("REACH HERE");
        
        for (List <Object> tt: intervals)
        {  
            for (Object s:tt)  //process each Object in the innerList   {X,Y}, {X,Z}......
            {
                System.out.println("\nThis is the object: " + s);
                Object temp = s;
                
                
                counter++;
                System.out.println("This is counter: " + counter);
                
                 if (counter%2==1)
                {

            odd = String.valueOf(temp);
            oddSubStringEndInteveral=odd.substring((odd.indexOf(" ")+1),odd.indexOf("]"));
            
            oddSubStringStartInterval = odd.substring(1,odd.indexOf(","));

            System.out.println("RANGE1 start:" + oddSubStringStartInterval);
            System.out.println("RANGE1 end:" + oddSubStringEndInteveral);
            

                }
                
                
                 if (counter%2==0)
            {
                even = String.valueOf(temp);

                evenSubStringStartInterval = even.substring(1,even.indexOf(","));
                evenSubStringEndInterval = even.substring((even.indexOf(" ")+1),even.indexOf("]"));

                System.out.println("RANGE2 start:" + evenSubStringStartInterval);
                System.out.println("RANGE2 end:" + evenSubStringEndInterval);
            } 
            
            //counter=0;
            //this ensures odd and even process starts again
            System.out.println(even);
            System.out.println(odd);
            
            
             if (odd!="" && even!="")
            {
                System.out.println("UUUUUUUU");
                if (Integer.valueOf(oddSubStringEndInteveral)>Integer.valueOf(evenSubStringStartInterval))
                {
                    System.out.println("OVERLAP");
                    
                    newInterval = "["+oddSubStringStartInterval+", "+evenSubStringEndInterval+"]";
                    innerList.add(newInterval);
                    System.out.println("New interval into outer List :" + newInterval);
                    overlap=true;
                    
                    newPairRangesMerged=true;
                    //overlap
                   // {1,3}, {2,8}, {6,10}, {11,14}
                   
                    //we know the next iteration will be odd {6,10}
                    //but this has to be compared against  even {2,8} - note this has already been processed from the for each loop
                    
                    //we know that existing entry in the outerList [1,8] => [1,10]
                    
                    //so we have to compare the last entry in the outerList [1,8] with next innerlist item [6,10]
                    
                    //outerList.add(newInterval);
                    
                }
                
                else
                {
                    System.out.println("HERE1");

                    //only required to add odd since the next range (even) might overlap with next odd
                    
                     //ODD
                    //{1,3}, {2,8}, {6,10}, {11,14}

                    //odd;

                    innerList.add(odd);
                }
                
                
                
                
            }
                
                
               
                //need also a control on here
                //since if the flag is set above, it will enter in here again
                //this loop designed for check of three or more multiple overlap occurrences
                
                
                if (overlap && !newPairRangesMerged)
                {
                    //it would perform for each an additional time...
                    //it would then compare against the outerList last item
                    //this should always be the newInterval since this variable has been set when the flag was set
                    
                    //this seems to be more straightforward...
                    // if it does not overlap, it will set the flag to false....
                    
                    
                    String endNewInterval = newInterval.substring(newInterval.indexOf(" ")+1,even.indexOf("]"));
                    System.out.println("REVISED ENDNEWINTERVAL: " + endNewInterval);
                    
                    String subStringStartInterval = String.valueOf(temp).substring(1,even.indexOf(","));
                    System.out.println("REVISED startinterval: " + subStringStartInterval);
                    
                    
                    if (Integer.valueOf(endNewInterval)>Integer.valueOf(subStringStartInterval))
                    {
                        System.out.println("************OVERLAP: ");
                        System.out.println(even);
                        System.out.println(odd);
                        
                            
                        //we would need to overwrite the last item in the innerList with new range
                        //list is zero index based
                        
                        //this is the intervals 
                        //{1,3}, {2,8}, {6,10}, {11,14}
                        
                        //this is the innerlist. need to remember might be other items before it.
                        //{X,X},{1,8}
                        //this entry will be removed, so need to keep a copy of it since '1' is of interest.
                        // 1 is currently stored in oddSubStringStartInterval
                        //we need to remember the merging will initiate from pairs and this variable will remain valid
                        
                        innerList.remove(innerList.size()-1);
                        
                        newInterval = "["+oddSubStringStartInterval+", " + endNewInterval;
                        innerList.add(newInterval);     //{1,10}
                        System.out.println("NEW INTERVAL SET => " + newInterval);
                        
                        
                        //now need to think from the perspective of it entering up to here again....
                        
                        //taking this to be the new data.
                        //it would process  {9,14}
                        //{1,3}, {2,8}, {6,10}, {9,14}
                        
                        //it would perform this:
                        //String endNewInterval = newInterval.substring(newInterval.indexOf(" ")+1,even.indexOf("]"));
                        //It would obtain 10  from {1,10}
                        
                        //it would perform String subStringStartInterval = temp.substring(1,even.indexOf(","));
                        //we know temp is {9,14}
                        //it would obtain 9 and identify overlap with 10.
                        //it would then remove last item in the innerList and store
                        
                        //newInterval = "["+oddSubStringStartInterval+"," + " " + endNewInterval;
                        //since it is a continuous overlap of ranges,  oddSubStringStartInterval = 1 is still valid
                        
                        //endNewInterval {14} is obtained via:
                        //String endNewInterval = newInterval.substring(newInterval.indexOf(" ")+1,even.indexOf("]"));
                        //and we know the newInterval is configured to {1,14}
                    }
                    
                }
                
                 else
                {
                    //we can set this status so that it stays clear of the associated if loop...
                    overlap=false;
                    
                    //it requires more thought again what code will do on the next execution.....
                    
                    //lets assume
                    //{1,3},{4,6},{5,7}, {6,9}
                    //it would have finished executing {1,3},{4,6}
                    //and commencing {5,7} 
                    //but there is a statement to start process when odd and even are populated.
                    //but we still need to focus on {4,6},{5,7}
                    
                    //so the option is to set the counter to 1
                    //counter would then become  2  when it processes  {5,7}  but it would store this in even
                    //this would overwrite the existing even {4,6}
                    
                    //we need to make the counter =0
                    //it will populate String odd
                    //we know the overlap process is part of pairs
                    //the String even is the comparing range.
                    
                    
                    //only reset the counter once the counter has reached 2, otherwise all entries from the intervals will reach here since there will be no
                    //overlap after retrieving the first range.. And then it will execute again and simply set String odd and overwrite it....
                    
                    if (counter==2)
                    {
                        counter=0;
                        System.out.println("counter being reset");
                        
                        //it does not matter if we clear the odd, but best it is actioned...
                        //BUT Definitely not clear the even
                        odd="";
                    }
                    
                    
                    
                    
                }
                
                
            }
        }
        
        //the final step would be to add the innerList into the outerList
        
        System.out.println("checking");
        //System.out.println(innerList.get(0));
        //System.out.println(innerList.get(1));
        //System.out.println(innerList.get(2));
        
        outerList.add(innerList);
        
        
        return outerList;
    }

}