/*
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 String block;
    static String initialState; 
    static int recursionCall;
    static String state;
    static String sequence;
    static int n=1;
    
    final static String str = "123124125";   // this is ascending blocks of 3  (PASS)
    final static String str = "125124123";    // this is all descending blocks of 3   (FAILS)
    //final static String str = "101112131415";  //PASS
    //final static String str = "32332432536";  //PASS
    //final static String str = "326325324323";  //PASS
    //final static String str = "666667";   //PASS
    
    final static String strBackup=str;
    static StringJoiner sj = new StringJoiner(",");
    static StringBuilder ascNums = new StringBuilder("");
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("Number to be examined: " + str);
        ascending(str);
    }
    
    public static boolean ascending(String str)
    {
        String blockBefore="";
        System.out.println("This is the block size: " + n);
        
        if((str.length()%n==0))
        {
            if (!(str.length()<(2*n)))
            {
                System.out.println("There are minimum of two blocks of size: " + n);
                block = str.substring(str.length()-n, str.length());
                
                try
                {
                    blockBefore = str.substring((str.length() - (2*n)),(str.length()-n));
                    
                    System.out.println("This is block: " + block);
                    System.out.println("This is block before: " + blockBefore);
                    
                    if ((Long.valueOf(blockBefore))<Long.valueOf(block) && recursionCall==0)
                    {
                        initialState="ascending";
                    }
                    
                    
                    if ((Long.valueOf(blockBefore))<Long.valueOf(block))
                    {
                        if (recursionCall>0)
                        {
                            state="ascending";
                        }
                        
                        System.out.println("INITIAL: " + initialState);
                        System.out.println("CURRENT: " + state);
                        
                        recursionCall++;
                        
                        
                        String blockBeforeBlock = str.substring((str.length() - (3*n)),(str.length()-(2*n)));
                        String currentBlock = str.substring(str.length()-n,str.length());
                        
                        System.out.println("Performing recursion, examining block " + "("+blockBefore+")" +" with block before it" + "("+blockBeforeBlock+")");
                        sj=new StringJoiner(",");
                       
                        if (ascNums.length()==0)
                        {
                            sj.add(blockBeforeBlock);
                            sj.add(blockBefore);
                            sj.add(block);
                        }
                        else
                        {
                            sj.add(blockBeforeBlock);
                        }
                        
                        sequence = sj.toString()+",";
                        
                        ascNums.insert(0,sequence);
                        
                        System.out.println("*****THIS IS THE SEQUENCE: " + ascNums);
                        
                        ascending(str.substring(0,(str.length()-n)));
                    }
                    else
                    {
                        n=n+1;
                        System.out.println("Performing recursion, restoring original String");
                        sj=new StringJoiner(",");
                        ascNums=new StringBuilder("");
                        state="";
                        
                        ascending(strBackup);
                    }
                }
                catch (StringIndexOutOfBoundsException | ArrayIndexOutOfBoundsException e)
                {

		    if (ascNums.length()==0)
                    {
                    	sj.add(blockBefore);
                    	sj.add(block);
                    	ascNums.insert(0,sj.toString());
                    }

                    System.out.println(blockBefore + " are digit(s) of first block n"+"("+n+")" + " of " + strBackup);
                    
                    if (state=="ascending")
                    {
                        System.out.println(strBackup + " consists of ascending numbers of group size" +"("+n+"): " + ascNums);
                    }
                    else
                    {
                        System.out.println(strBackup + " consists of descending numbers of group size" +"("+n+"): " + ascNums);
                    }
                    
                
                    System.exit(0);
                    return true;
                }
            }
        }
        
        else
        {
            System.out.println("!!!!!!!");
            System.out.println(blockBefore);
            System.out.println(block);
            
            if (blockBefore!="")
            {
            
            if ((Long.valueOf(blockBefore))>Long.valueOf(block) && recursionCall==0)
            {
                
                initialState="descending";
            }
            
            }
              
            if (recursionCall>0)
            {
                state="ascending";
            }
            
            System.out.println("INITIAL: " + initialState);
            System.out.println("CURRENT: " + state);
                        
            recursionCall++;
            
            //we know at this point that if the initialState does not allign with the state, it is neither ascending or descending blocks
            //so we can run the below execution ONLY if both align
            
            if (state==initialState)
            {
                n++;
                System.out.println("Value of block size has been increased to: " + n);
                sequence="";
                ascNums=new StringBuilder("");
                sj=new StringJoiner(",");
            
                str=strBackup;
            
                System.out.println("String length: " + str.length());
                System.out.println("Block size: " + n);
            
                //it appears both if else are exactly the same recursive call 
                if (str.length()%2==0)
                {
                    if (n<(str.length()/2))
                    {
                        ascending(str);
                    }
                }
                else
                {
                    if (n<((str.length()+1)/2))
                    {
                        ascending(str);
                    }
                }
            }
        }
        
        System.out.println(strBackup + " does not consist of ascending/descending number in any block size " + n + "(n): " + ascNums);
        System.exit(0);
        return false;
    }
}