/*
Online Java - IDE, Code Editor, Compiler

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

//We know that all that is required for the descending example to work is to 
//flip the > to <  (in this example)  and also change the onscreen comments  ascending => descending
//but we know it required the same entire code to function
//so we also know that we do not need to perform any reversal of Strings whatsoever

//we know it is not possible to change this symbol...
//only option is to perform:

//temp = blockBefore;
//blockBefore = block;
//block=temp;
//

import java.util.*;

public class Main
{
    static String sequence;
    static int n=1;
    static int recursionCall;
    
    static String initialState;
    
    //****TEST CASES AS PER THE CHALLENGE *******************
    //final static String str = "123124125";
    //final static String str = "101112131415";
    //final static String str = "32332432536";
    //final static String str = "323324325326";
    //final static String str = "666667";
    
    
    //****ASCENDING BLOCKS  *******************
    //final static String str = "123456789";   //whole block ascending                  PASS
    //final static String str = "123124125";  //each 3 block ascending and digit in each block ascending     PASS
     //final static String str = "123124215";    // each 3 block ascending ONLY   (215 not ascending)     PASS
    
    //*******further test cases
    //final static String str = "123212421543452";    // each 5 block ascending ONLY  
    //final static String str = "123452467956789";    // each 5 block ascending and digits in each block ascending   PASS
    
    
    //****DESCENDING BLOCKS  *******************  
     //final static String str = "98765431";   //whole block descending     PASS 
     //final static String str =  "543542541532";   //each 3 block descending and digit in each block descending    PASS
     //final static String str = "542541526";    // each 3 block descending ONLY   (516 not descending)    FAIL
    
    //******further test cases
    //final static String str = "434522421512321";    // each 5 block descending  ONLY  
     //final static String str = "987348765276543";    // each 5 block descending and digits in each block descending
     
     final static String str =  "235461";   //each 3 block ascending and digits each block ascending except last
    
     //final static String str =  "8769";
    
    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 String ascending(String str)
    {
        String state="";
        String temp="";
        String block="";
        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
                {
                    
                    System.out.println("**** IN TRY STATEMENT ****");
                    blockBefore = str.substring((str.length() - (2*n)),(str.length()-n));
                    
                    if (recursionCall>0)
                    {
                        if (Long.valueOf(blockBefore)<Long.valueOf(block))
                        {
                            state="ASCENDING";
                        }
                        if (Long.valueOf(blockBefore)>Long.valueOf(block))
                        {
                            state="DESCENDING";
                        }
                    }
                    
                    
                    
                    
                    
                    
                    if ((Long.valueOf(blockBefore))<Long.valueOf(block) && recursionCall==0)
                    {
                        System.out.println("ASCENDING BLOCK");
                        initialState="ascending";
                    }
                    
                    if (recursionCall==0)
                    {
                    temp = blockBefore;
                    blockBefore = block;
                    block=temp;
                    }
                
                    if ((Long.valueOf(blockBefore))<Long.valueOf(block) && recursionCall==0)
                    {
                        System.out.println("DESCENDING BLOCK");
                        initialState="descending";
                    }
                
                    if (initialState=="ascending")
                    {
                        block = str.substring(str.length()-n, str.length());
                        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++;
                        
                        String blockBeforeBlock = str.substring((str.length() - (3*n)),(str.length()-(2*n)));
                        
                        if (state.equals("DESCENDING"))
                        {
                            System.out.println("INITAL STATE WAS DECENDING");
                            blockBefore=block;
                        }
                        
                        
                        System.out.println("Performing recursion, examining block before current " + "("+blockBefore+")" +" with block before it" + "("+blockBeforeBlock+")");
                        sj=new StringJoiner(",");
                        
                        if (state.equals("ASCENDING"))
                        {
                            if (ascNums.length()==0)
                            {
                                sj.add(blockBeforeBlock);
                                sj.add(blockBefore);
                                sj.add(block);
                            }
                            else
                            {
                                System.out.println(sequence);
                                System.out.println("ascending sequence: " + sequence);
                                sj.add(blockBeforeBlock);
                            }
                        }
                        
                        if (state=="descending")
                        {
                            if (ascNums.length()==0)
                            {
                                sj.add(blockBeforeBlock);
                                sj.add(block);
                                sj.add(blockBefore);
                            }
                            else
                            {
                                System.out.println("desceding sequence: " + sequence);
                                sj.add(blockBeforeBlock);
                            }
                        }
                        
                        //recursionCall=0;
                        sequence = sj.toString()+",";
                        
                        ascNums.insert(0,sequence);
                        
                        System.out.println("*****THIS IS THE SEQUENCE: " + ascNums);
                        
                        return ascending(str.substring(0,(str.length()-n)));
                    }
                    else
                    {
                        n=n+1;
                        System.out.println("Performing recursion, restoring original String" + "("+strBackup+")");
                        sj=new StringJoiner(",");
                        ascNums=new StringBuilder("");
                        
                        return ascending(strBackup);
                    }
                }
                catch (StringIndexOutOfBoundsException | ArrayIndexOutOfBoundsException e)
                {
                    System.out.println("**** IN CATCH STATEMENT ****");
                    
                    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);
                    System.out.println(strBackup + " consists of " + state + " numbers of group size" +"("+n+"): " + ascNums);
                   
                    System.exit(0);
                    return null;
                }
            }
        }
        
        else
        {
            n++;
            System.out.println("Value of block size has been increased to: " + n);
            sequence="";
            ascNums=new StringBuilder("");
            sj=new StringJoiner(",");
            recursionCall=0;
            
            str=strBackup;
            
            System.out.println("String length: " + str.length());
            System.out.println("Block size: " + n);
            
            if (str.length()%n==0)
            {
                return ascending(str);
            }
        }
        
        System.out.println(strBackup + " does not consist of " + state + " number in any block size " + n + "(n): " + ascNums);
        System.exit(0);
        return null;
    }
}