/*
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 boolean isAscending;
    static String sequence;
    static int n=1;
    
    //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
      
     //final static String str1 = "98765431";   //whole block descending     PASS 
     final static String str1 =  "543542541";   //each 3 block descending and digit in each block descending    PASS
     //final static String str1 = "542541516";    // each 3 block descending ONLY   (516 not descending)    PASS
    
    
    //final static String str = "101112131415";
    //final static String str = "32332432536";
    //final static String str = "326325324323";
    //final static String str = "666667";
    
    static String strBackup;
    static StringJoiner sj = new StringJoiner(",");
    static StringBuilder ascDescNums = new StringBuilder("");
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("\n\nNumber to be examined: " + str);
        strBackup=str;
        isAscending=true;
        System.out.println("********************************");
        checkAscendingDescending(str,"ascending");
        System.out.println("********************************\n");
        
        System.out.println("Number to be examined: " + str1);
        
        strBackup=str1;
        n=1;
        ascDescNums=new StringBuilder("");
        
        isAscending=false;
        System.out.println("********************************");
        checkAscendingDescending(str1,"descending");
        System.out.println("********************************\n");
    }
    
    public static String checkAscendingDescending(String str,String direction)
    {
        System.out.println("Checking  " + direction + " block sizes");
        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);
                String 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 ((isAscending?  Long.valueOf(blockBefore)<Long.valueOf(block): Long.valueOf(blockBefore)>Long.valueOf(block)))
                    {   
                        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 (ascDescNums.length()==0)
                        {
                            sj.add(blockBeforeBlock);
                            sj.add(blockBefore);
                            sj.add(block);
                        }
                        else
                        {
                            sj.add(blockBeforeBlock);
                        }
                        
                        sequence = sj.toString()+",";
                        
                        ascDescNums.insert(0,sequence);
                        
                        System.out.println("*****THIS IS THE SEQUENCE: " + ascDescNums);
                        
                        return checkAscendingDescending(str.substring(0,(str.length()-n)),direction);
                    }
                    else
                    {
                        n=n+1;
                        System.out.println("Performing recursion, restoring original String");
                        sj=new StringJoiner(",");
                        ascDescNums=new StringBuilder("");
                        
                        return checkAscendingDescending(strBackup,direction);
                    }
                }
                catch (StringIndexOutOfBoundsException | ArrayIndexOutOfBoundsException e)
                {

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

                    System.out.println(blockBefore + " are digit(s) of first block n"+"("+n+")" + " of " + strBackup);
                    System.out.println(strBackup + " consists of " + direction + " numbers of group size" +"("+n+"): " + ascDescNums);
                   
                    //System.exit(0);
                    return null;
                }
            }
        }
        
        else
        {
            n++;
            System.out.println("Value of block size has been increased to: " + n);
            sequence="";
            ascDescNums=new StringBuilder("");
            sj=new StringJoiner(",");
            
            str=strBackup;
            
            System.out.println("String length: " + str.length());
            System.out.println("Block size: " + n);
            
            if (str.length()%2==0)
            {
                if (n<(str.length()/2))
                {
                    return checkAscendingDescending(str,direction);
                }
            }
            else
            {
                if (n<((str.length()+1)/2))
                {
                    return checkAscendingDescending(str,direction);
                }
            }
        }
        
        System.out.println(strBackup + " does not consist of " + direction + " number in any block size " + n + "(n): " + ascDescNums);
        //System.exit(0);
        return null;
        
    }
}