/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.util.*;

public class Main
{
    
    static int []nums = new int[]{100,4,200,1,3,2};
    static int[][] Store = new int[nums.length][nums.length];
    static int differenceCheck=1;
    static int count=0; // this keeps track consecutive numbers.
    
    static int nextNum;
    
    
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
       
        int []fillZero = new int[]{0};
        
        boolean nextnumberconsecutive;
        
        int[] temp = new int[nums.length];
    
    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[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);
                    
                }
                }
            
        }
        
    }
    
    //test to see whats in repository.
    
    StringJoiner sj= new StringJoiner(",");
    
    for (int [] f: Store)
    {
        for (int g: f)
        {
            sj.add(Integer.toString(g));
            
        }
        System.out.println(sj.toString());
        sj=new StringJoiner(",");
    }
    
    
    
    int counter=0;
    
    for (int max[]: Store) 
{
    
    if (max.length>temp.length)
    {
        temp=new int[nums.length];   //clears out temp.....
        
        for (int t: max)
        {
            temp[counter]=max[counter];
            counter++;
        }
        
    }
    
}

System.out.println("****************");
System.out.println("\nLongest length consecutive sequence: " + Arrays.toString(temp));
System.out.println("****************");
}
  //                            (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 firstnumberChain)
{
    // 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 k=0; k<nums.length; k++)   
    {
        
        System.out.println(Store[firstnumberChain][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[firstnumberChain])   //if it finds a match....
            {
            System.out.println("Found next consecutive number: " + nextNum);
            
            System.out.println("COUNT!!!!: " + count);
            
            count++;
            Store[firstnumberChain][count]=nextNum;   // number stored in next free position...
            
            
            return true;
                
            }
        
    }
    
    count=0;
    differenceCheck=1;
    
    return false;
    
}
    
}