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
{
    static boolean completeOnce=false;
    static int []nums = new int[]{100,4,200,1,3,2};
    static int[][] Store = new int[nums.length][nums.length];
    static String[] temp = new String[nums.length];
    
    static int differenceCheck=1;
    static int count=0; // this keeps track consecutive numbers.
    static int firstNum;
    
    
    
    static boolean multipleMaxLength=false;
    
    static int nextNum;
    static int q;
    static int p;
    
    
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
      
      
     
        
    boolean nextnumberconsecutive;
        
    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 loops have not succeeded, it will start here again since the consecutive streak is broken...
    //so it should have a fresh store.....
    
    {
        //Store = new int[nums.length];
        
        Store[i][count]=nums[i];
        
        System.out.println("\nThis is the number being checked: " + nums[i]);
        
        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 another nested loop required for array elements k
        {
            
            
            if (j==i) // this will prevent same number being compared and increment inner loop by 1. 
            //However not critical since identical array element can not interfere
            {
                j++;
                
            }
            
            if (j!=nums.length) //The whole process will continue as long as the inner loop does not exceed the array length
            {
                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... it is guaranteed to fill this.....
                    
                    do // perform this loop whilst elements of the array are searched in order to find j element residing at   i + differenceCheck;
                    {
                        
                        // seeking the next consecutive number
                        
                        System.out.println("***Next consecutive number has appeared:***" + (nums[i]+differenceCheck) + "This should be same as nums[j]");
                        //nextnumberconsecutive = nextnumbercheck(nums[i]+differenceCheck, nums, count, Store);
                     
                        
                        Store[i][count]= nums[i]+differenceCheck;  //stores next consecutive number
                        //note Store[0] 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 element differenceCheck from initial array element
                        
                       
                        
                        
                        //System.out.println("this is the boolean output:" + nextnumberconsecutive);
                        
                        //this is needed so that it is ready to start fresh again...
                        //otherwise for instance if the array consisted of   1,2,3,6,5  
                        //it would continue to process from 6 onwards..
                        //it would also fail to clear the store repository of consecutive numbers...
                        
                        j=nums.length-1;
                    
                    //this has become a bit messy since the differenceCheck was referenced from i
                    //if it was done against j, might have been a bit tidier..
                    }while(nextnumberconsecutive);
                    
                }
                }
            
        }  //end of inner for loop
        
    }  //end of for loop going through all nums
   
   System.out.println("Length of store: " + Store.length);
   int counter=0;
   int consecutiveNumbers=0;

System.out.println("********These are all entries*********");

for (int max[]: Store)   //for each row in store...
{
    
    System.out.println(Arrays.toString(max));

}


int currentConsectiveNumbers=0;
StringJoiner sj = new StringJoiner(",");
String numtoString;
boolean conditionOnce=false;
int currentI=0;
int j=0;
int zerFoundLocation=0;
boolean multipleSame=false;


for (int i=0; i<Store.length; i++)   //each row
{
    System.out.println("******NEW NUMBER*****");
    
    sj=new StringJoiner(",");  //creates StringJoiner
    
    conditionOnce=false;  // it will set this on start of new row...
    // this will enable it to check for 0's again.....
    
    counter=0;
    
    for (j=0; j<Store[0].length; j++)  //each column in row....
    {
        numtoString=Integer.toString(Store[i][j]); //this gets overwritten 
        
        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 && !conditionOnce)
        {
            System.out.println("zero found at: " + j); // this is correct
            counter++;
            conditionOnce=true;
            zerFoundLocation=j;
        }
        
        //if position of the 0 is greater than previous entry.
        //note due to 0 indexing, the position of the first 0 determine the length of the consecutive numbers
        //prior to it..
        
        if (zerFoundLocation>currentConsectiveNumbers && conditionOnce)
    {
         System.out.println("this is current highest consecutiveNumbers: " + currentConsectiveNumbers);
         System.out.println("this is identified consecutive Numbers: " + zerFoundLocation);
        
        currentConsectiveNumbers=j;
        temp[i]=sj.toString();
        System.out.println("string: " + temp[i]);
        currentI=i;
        counter++;
        
        break;
        //j=nums.length;
    }
    
    
    //this will create indefinite loop.. hence it checks if i has changed...
    if (consecutiveNumbers==currentConsectiveNumbers && i!=currentI)
    {
        currentConsectiveNumbers=consecutiveNumbers;
        temp[i]=sj.toString();
        counter++;
        multipleSame=true;
        
        break;
        //j=nums.length;
     }
     
     
     //issue is that the first set of numbers stored in repository will exceed currentConsectiveNumbers
     //and then if there is another greater, it will be stored.
     //so there are two rows...
     //can discard all the rows with null...
     
     if (counter==2)
     {
         
     }
        
        
    }
    
    
}

System.out.println("Longest consecutive: " + currentConsectiveNumbers  + "(" + counter + "entries)");
for (String t: temp)
{
    System.out.println(t);
    
}



/*


            
    for (int max[]: Store)   //for each row in store...
{
    System.out.println("FOR EACH ROW");
    
    for (int t: max)  //it will check each number in the row
    {
        if (t==0 && counter!=0)  //looks for a 0 in Store array.. This is indicator that this is 
        //end of consecutive numbers... It excludes first index, since it could constitute a genuine 0 in the initial
        //presented array... Anywhere else but here would be invalid...
        {
            consecutiveNumbers=nums.length-counter;   //this is now length of consecutive numbers...
        }
        counter++;
    }
    
    counter=0;
    
    System.out.println("q here: " + q);
    System.out.println("p here: " + p);
    
    //this is still examining each row......
    if (consecutiveNumbers>maxConsecutive)
    {
        System.out.println("current max length: " + consecutiveNumbers);
        
        for (int t: max)
        {
            temp[q][p]=t;
            p++;
            
        }
        
    }
    q++;
    p=0;
    
    if(consecutiveNumbers==maxConsecutive)
    {
        System.out.println("BEACAUSE");
        for (int t: max)
        {
            temp[q][p]=t;
            p++;
        }
        
        maxConsecutive++;
        multipleMaxLength=true;
        
    }
    q++;
    p=0;
    
    if (multipleMaxLength)
    {
        System.out.println("These are " + maxConsecutive+  " maximum consecutive numbers in the array");
        
        for (int[] arr: temp)
        {
            System.out.println("****************");
            System.out.println("\nLongest length consecutive sequence: " + Arrays.toString(arr));
            System.out.println("****************");
            
        }
        
    }
    
    //it will be here if there is no multiple instances of maxConsecutive  numbers...
    //since it will supersede previous entry whether it will be greater than maxConsecutive=0 (default)
    //or if it supersedes previous maxConsecutive
    
    else
    {
        System.out.println("****************");
        System.out.println("\nLongest length consecutive sequence: " + Arrays.toString(temp[0]));
        System.out.println("****************");
        
    }
    
    
    
}
completeOnce=true;
*/

}
  //                            (nums[i]+differenceCheck, nums,  differenceCheck (2),   contains array for differenceCheck)
//static boolean nextnumbercheck(int nextNum, int[] nums, int differenceCheck, int[] Store)
static boolean nextnumbercheck(int firstNum)
{
    //int [][]temp = new int[nums.length][nums.length];
    
    // this is doing all of nums again..... since the numbers are out of order...
    //it is comparing the j value (which is i+ differenceCheck and hoping to find   next value at  i+differenceCheck (which has been incremented...))

    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 (i!=nums.length)  // why is this condition here, its not in a position to exceed array?
        //{
            if (nextNum==nums[i])   //if it finds a match....
            {
            System.out.println("Found next consecutive number: " + nextNum);
            
            System.out.println("COUNT!!!!: " + count);
            
            count++;
            Store[firstNum][count]=nextNum;   // number stored in next free position...
            
            return true;
                
            }
        
    }
    
    count=0;
    differenceCheck=1;
    
    return false;
    
}
    
}