/*
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 sequence;
    static int n=1;
    
    final static String str = "123456789";   //whole block ascending
    //final static String str = "123124125";  //each 3 block ascending and digit in each block ascending
    //final static String str = "123124215";    // each 3 block ascending ONLY   (215 not ascending)
    
    //final static String str = "101112131415";
    //final static String str = "32332432536";
    //final static String str = "326325324323";
    //final static String str = "666667";
    
    
    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 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 ((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 (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);
                        
                        return 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("");
                        
                        return 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);
                    System.out.println(strBackup + " consists of ascending 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(",");
            
            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 ascending number in any block size " + n + "(n): " + ascNums);
        System.exit(0);
        return null;
        
    }
}