import java.util.*;

/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/
public class Main
{
    // This was used initially whilst extracting the longest integer array out of the 2D array.
    //But I decided not to use it eventually.
    //static boolean completeOnce=false;
    
    
    //This is the defined set of numbers....
    //static int []nums = new int[]{100,4,200,1,2,6,7,8,9,100,101,102,103};
    //static int []nums = new int[]{100,4};
    static int []nums = new int[]{100,4,200,1,3,2};
    //static int []nums = new int[]{1,1,1,1,1,1};
    static int []nums = new int[]{0,1,2,4,5,6};
    
    //this is 2D array used to store all the consecutive arrays created...
    static int[][] Store = new int[nums.length][nums.length];
   
   //this is used to store all relevant array(s) with longest streak consecutive numbers...
    static String [] temp = new String[nums.length];
    
    //the question states consecutive numbers, so the difference will always be 1 between adjacent numbers...
    static int differenceCheck;
    
    
    static int count=0; // this keeps track consecutive numbers.
    
    //this runs parallel with the variable i... i is the first number selected in each array.
    static int firstNum;
    
    //this was used also iniitally as a flag if two arrays had same maximum length, eventually phased out..
    //static boolean multipleMaxLength=false;
    
    //next consecutive number expected...
    static int nextNum;
    
    //firstNum variable ensures it has visibility of i  (from main method)
    //this is required since in most standard practices in for loop, int is initialised there...
    //so the scope is narrow to the for loop.
    
    static boolean nextnumbercheck(int firstNum)
    {
        
        //it is comparing the first j value (which is i+ differenceCheck 
        //and hoping to find   next value at  j+differenceCheck, j + differenceCheck  until 
        //j!=nums.length
    
        //although I should have used a different variable letter here instead of i in for loop. 
        //But there were few issues since firstNum was available for i loop already....
        //count variable increments with i.. So in practice it could be phased out
       //also firstNum variable is index is a bit misleading..
       //but  Store[firstNum][0] is  Store[i][0], in which first number of sequence is stored..
    
    for (int i=0; i<nums.length; i++)   
    {
            //System.out.println(Store[firstNum][count] + " will be checked against numbers for next consecutive number: " + nextNum);
            if (nextNum==nums[i])   //if it finds a match....
            //reminder that     nextNum = nums[i]+differenceCheck (1)
            
            {
                //System.out.println("Found next consecutive number: " + nextNum);
                count++;   //creates next free position
                Store[firstNum][count]=nextNum;   // number stored in next free position...
                
                return true;  //ensures boolean method keeps going for the sequence...
            }
    }
    
    count=0;  // there are no more consecutive numbers starting from  nums[i] onwards..
    //it will prepare to start next nums[i]
    
    return false;
    }
    
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
      
        //very critical variable, it is used in the do while loop.
        //the default value is false.... but it has also been set just incase...
        boolean nextnumberconsecutive=false;
    
    //phased out.    
    //int maxConsecutive=0;
    
    for (int i=0; i< nums.length; i++) // this will go through each element in array
    //it is safe to say when the inner for and do while loop have not succeeded, it will start here again 
    //since the consecutive streak is broken...
    //so it should have a fresh entry in the store.....
    
    {
        //the first entry in each row Store[i][0] will always be nums[i]
        Store[i][count]=nums[i];
        
        System.out.println("\nThis is the number being checked: " + nums[i]);
        
        //the variable has been declared here, also could be processed on variable initialisation.
        differenceCheck=1;

        for (int j=0; j<nums.length; j++) 
        // this is used to compare each element to array element in previous loop
        // However this is not a sufficient loop since if the next consecutive integer is found,
        // it would not continue process to check for next consecutive.
        //Hence recursion introduced later on to support Store arrays such as
        //{i, j, j}  or  {i}  or  {i,j} 
        
        {
            if (j==i) // this will prevent same number being compared
            //with each other... 
            //and increment inner loop by 1. 
            //However not critical since identical array element can not interfere since difference would be 0...
            {
                j++;
                
            }
            
            if (j!=nums.length) 
            //The whole process will continue as long as the inner loop does not exceed the array length
            //which would cause ArrayIndexOutOfBoundsException
            {
                if (nums[j]==nums[i]+differenceCheck) 
                // if the value in element j  is next consecutive number to value at num[i]
                {
                    count++;  
                    //it is setting next vacant position in the store... 
                    //at this point, there is guarantee to be two consecutive numbers minimum...
                    
                    do 
                    // perform this loop whilst elements of the array are searched in order to find j element residing at   
                    //i + differenceCheck,  element at (i + differenceCheck) with j .........
                    {
                        
                        // seeking the next consecutive number.. This will always be the second number...
                        System.out.println("Next consecutive number has appeared: " + (nums[i]+differenceCheck));
                        
                        
                        Store[i][count]= nums[i]+differenceCheck;  
                        //stores next consecutive number
                        //note Store[i][count] is reserved for nums[i]
                        
                        differenceCheck++; 
                        // this will ensure next time this loop is entered, the initial number will be compared
                        
                        nextNum = nums[i]+differenceCheck;  //next consecutive number expected....
                        
                        //method call...
                        nextnumberconsecutive = nextnumbercheck(i);
                        //this calls a method. It will ensure that next searching of array elements will 
                        //find next consecutive number from current element stored at Store[][count]
                        
                        //this is code that was phased out..
                        // it makes no difference if the code is kept or not...
                        //since the j loop is controlled by recursion and if the streak consecutive number ends,
                        //nextnumberconsecutive would be false and the j loop would break out..
                       // either one of these would be valid in situations where recursion not used....
                        //break;
                       //j=nums.length-1;
                    
                    }while(nextnumberconsecutive);
                    
                }
                }
            
        }  //end of inner for loop
        
    }  //end of for loop going through all nums
   
   //this counts all the rows in the Store..
   //its user information to ensure that there is an try for all numbers in num[]
   System.out.println("\n\nLength of store: " + Store.length);
  
   
//user output showing all entries....

System.out.println("********These are all entries*********");

for (int max[]: Store)   //for each row in store...
{
    
    System.out.println(Arrays.toString(max));

}



StringJoiner sj = new StringJoiner(",");  //used to store consecutive numbers into a String...
int currentMaximumConsectiveNumbers=0;  //keeps track of number elements in longest maximum number sequence..
String numtoString; // required before the number can be stored in StringJoiner..
boolean conditionOnce=false;
//this is used in the j loop to support this:
//(Store[i][j]==0 && j!=0)

int currentI=0;  //used to ensure same nested loop can not execute again on same element i.
int j=0;
int zeroFoundLocation=0;   //location of zero in the processed array in Store..

boolean zeroFound=false;  //this is required since information is stored in the temp array 
//(containing all arrays which are maximum length).. Otherwise it will replicate all those from
//Store array, which would be pointless.

int posZero=0;
//keeps track of where there is 0. It keeps value of j.

int oldConsecutiveNumbers=0;  //difficult to explain logic, but it was required...

String backupStringJoiner;  //it has to keep inside j loop before it adds existing StringJoiner
//reason is that only place to grow StringJoiner of numbers is in j loop.. (for each item in row i)
//if it finds that a 0 is added, it has a chance to roll back....
//note it will exclude a 0 at Store[i][j] where j=0 since it might be a genuine 0 from num[] array.  

//using alternatives such as this in the j loop will not work since it will fail to write leading 0 as per aforementioned.
// if (Store[i][j]!=0)
//        {
//        sj.add(numtoString); //keeping all numbers into a new StringJoiner...
//        }



for (int i=0; i<Store.length; i++)   //each row
{
    System.out.println("\n******NEW NUMBER*****:  " + Arrays.toString(Store[i]));
    System.out.println("This is current highest consecutiveNumbers: " + currentMaximumConsectiveNumbers);
    
    sj=new StringJoiner(",");  //creates StringJoiner
    
    
    for (j=0; j<Store[0].length; j++)  //each column in row.....
    {
        numtoString=Integer.toString(Store[i][j]); //this gets overwritten 
        zeroFound=false;  //as explained earlier..
        conditionOnce=true; //as explained earlier...
        
        //need to keep a backup, in event that it finds a 0 in the consecutive numbers... 
        backupStringJoiner = sj.toString();
        
        sj.add(numtoString); //keeping all numbers into a new StringJoiner...
        
        //if it finds a 0 and not at first location (since it could be a genuine 0 in the array)
        if (Store[i][j]==0 && j!=0)
        {
            System.out.println("Zero found at index: " + j);
            
            //it is now resetting StringJoiner and re-instating the backup.
            //The backup was before it wrote the identified 0.
            sj=new StringJoiner(",");
            sj.add(backupStringJoiner);
            
            posZero=j;   // posZero keeps track of location of 0. This would be anywhere EXCEPT index 0 for j 
            
            //stores into variable if higher than previous
            if (j>zeroFoundLocation)
            {
            zeroFoundLocation=j;
            }
            
            zeroFound=true;
            
            
            //break;
            //can not use break statement since it has to execute below in if statement...
            //this ensures that it will be ready to start next row in Store array...
            j=nums.length;
            
            //this has to be executed once...
            // since it is interested in the zero found location of the integer array of Store..
            //we know that consecutive numbers were populated in a 2D integer array consisting of all zeros...
            //if it continuously processing the rows, it might find additional zero...
            //it would be incorrect to zeroFoundLocation any further than the first occurence....
            //since consecutive numbers would be processed already...
            
            //It could be taken out of the if condition since j has been set to j=nums.length, so its guaranteed to not
            //process this multiple times...
            
            if (conditionOnce)
            {
                
            //this is slightly trickier part in the code, it has to keep a copy of the currentMaximumConsectiveNumbers
            //so that it can make comparison to see if zeroFoundLocation is larger or same to ensure
            // it can populate temp array correctly....
            oldConsecutiveNumbers = currentMaximumConsectiveNumbers;
            
            //this sets the existing maximum length of consecutive numbers to length in which zero was found
            
            currentMaximumConsectiveNumbers=zeroFoundLocation;
            
            //prevents this loop from entering again....
            conditionOnce=false;
            }
        }
        
        
        //it can only do this if the consecutive number sequence is longer than previous.
        //and of course if there is a 0 (zeroFound)..
        
        //we know already that all the end 0's have been removed as process of creating StringJoiner....
        //the zeroFound flag is important, since zeroFound flag was set to true when
        // it found a line of consecutive numbers in which j was greater than zeroFoundLocation...
        //if  (&&zeroFound) was removed from here, it would process this for index j irrespective of length of consecutive numbers.
        // this in effect would cause it to keep removing the existing highest maximum row stored
        // in temp array...
        //eventually it would come to a point that number stored would be the last entry in num[]!
        
        if (zeroFoundLocation>oldConsecutiveNumbers &&zeroFound)
    {
        //System.out.println("************HERE1***********");
         System.out.println("This is current highest streak of consecutiveNumbers: " + oldConsecutiveNumbers);
         System.out.println("This is newly identified streak of consecutive Numbers: " + zeroFoundLocation);
        
        temp = new String[nums.length];  //clears all existing entries... 
        
        //each time if it supersedes the previous maximum length sequence consecutive numbers, 
        //it has to store in first location and overwrite..
        temp[0]=sj.toString();
        
        System.out.println("Highest consecutive number sequence so far: " + temp[0]);
        
        //it has to break since it has dealth with above...
        //again if it continued running, the outcome would not be affected..
        //but it would process same logic j times! 
        break;
    }
    
    //it is here since the length of consecutive numbers is exactly same as the current maximum
    //so its a valid solution and needs to be stored
    
    if (posZero==currentMaximumConsectiveNumbers && zeroFound)
    {
        //System.out.println("************HERE2***********");
        System.out.println("The following: " + sj.toString() + "      has been stored. \nIt is also equal to current maximum sequence of consecutive numbers: " + currentMaximumConsectiveNumbers);
        
        //on this instance, it does not overwrite temp. It adds at next position i.
        temp[i]=sj.toString();
       
        //to prevent repeat execution...
        break;
        
     }
     
    
    }
     
     
}

System.out.println("\n\n****************************");
System.out.println("Longest consecutive sequence: " + currentMaximumConsectiveNumbers);
System.out.println("****************************");
         
         for (String s: temp)
         {
             if (s!=null)
             {
                 System.out.println(s);
                 
             }
         }
        
    }
}