/*
Online Java - IDE, Code Editor, Compiler

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

//I can see straight away looking at the challenge and my notes....
//Requirement is to return the array as oppose to returning individual
//formatted population.

import java.util.*;

public class Main
{
    static long checkNegativeNumber;
    static long smallPortion;
    static int i;
    
    //static long roundingUnit = 3;
    //static long roundingToNearest=6;
  
    static long roundingToNearest=1000000;
    static long roundingUnit = roundingToNearest/2;
    
    static long smallPortionRounded;
    static long formattedRoundedNumber;
    static String millionsRoundingFormatted[][];
    static String millionsRounding[][];
    
    static long largePortion;
    static long divisbleByRoundingUnit;
    static long divisibleByRoundingToNearest;
    static long population;
    static long frequencyRoundedNumber;
    static long frequencyRoundedNumberInLargePortion;  
    
    public static void main(String[] args) 
    {
        //ORIGINAL TEST CASES AS PER EXAMPLE  ***PASS
        millionsRounding = new String[][]
                                        {
                                        {"Nice","942208"},
                                        {"Abu Dhabi", "1482816"},
                                        {"Naples", "2186852"},
                                        {"Vatican City", "572"}
                                        };
    
        //DO NOT COMMENT THIS OUT
        millionsRoundingFormatted = new String[][]
                                        {
                                        {"Nice",""},
                                        {"Abu Dhabi", ""},
                                        {"Naples", ""},
                                        {"Vatican City", ""}
                                        };
        
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("Original population:");
        getPopulation(millionsRounding);
        
        
        for (i=0; i<millionsRounding.length;i++)
        {
            //this will display populations
            System.out.println("\nProcessing population for: " + millionsRounding[i][0] + "("+millionsRounding[i][1]+")");
            population = Long.valueOf(millionsRounding[i][1]);
            frequencyRoundedNumber = population/roundingUnit;
        
            smallPortion =  population - (frequencyRoundedNumber * roundingUnit);
            largePortion = frequencyRoundedNumber * roundingUnit;  
            
            frequencyRoundedNumberInLargePortion = largePortion/roundingUnit;  
        
            //this part here is the logic identified when performing operation
            //on a number less than roundingUnit (see documentation for number 274,214)
	        //unfortunately this was error on my documented logic. It shows it is sometimes easy
	        //to confuse / with %.

            if (smallPortion < 0)
            {
                System.out.println("Small portion less than 0, should not enter here");
                System.out.println("Small portion being set to: " + population);
                smallPortion = population;
                largePortion = 0;
                frequencyRoundedNumber = 0;
            }
        
            roundSmallPortion();
        }
        
        System.out.println("\nPopulation rounded to nearest " + roundingToNearest);
        getPopulation(millionsRoundingFormatted);
    } //end of method
    
    public static void getPopulation(String [][] populationData)
    {
        for (String[]m:populationData)
        {
            System.out.println(Arrays.asList(m));
        }
    }
    
    //I can see that in my logic, I made a few critical mistakes.
    //But we are no longer dealing with number (formattedRoundedNumber = number + 500,000)
    //it has been partitioned into largePortion and smallPortion
    //also we know that 500,000 is considered to be part of largePortion
    //I will add it across to this part.
    //I will need to pay close attention know just to ensure it has no impact
    //elsewhere
    
    public static void roundSmallPortion()
    {
        divisbleByRoundingUnit = population%roundingUnit;
        divisibleByRoundingToNearest=population%roundingToNearest;
        System.out.println("Processing the small portion: " + smallPortion);
        System.out.println("This is large portion: " + largePortion);
        
	    //again I used this when it would have been simpler to compare
	    //smallPortion with roundingUnit (500,000). And set smallPortionRounded to 0.
	    //instead of performing if (checkNegativeNumber< (-1*roundingUnit)) since understanding
	    //less than on a negative number is subject to logical error...
        //but sometimes selecting a challenge categorized hard can instill
        //unecessary complex logic in order to exercise variables available.
        //on the other hand seeing a negative value for checkNegativeNumber is reassuring since
        //we know it is processing other formulas correctly.

        checkNegativeNumber = smallPortion - roundingToNearest;
        System.out.println("Checking negative number: " + checkNegativeNumber);
        
        //as per logic, this can occur numerous reasons
        if (divisibleByRoundingToNearest==0)
        {
            System.out.println("Population is exactly divisible by: " + roundingToNearest);
            //here we know number is 500,000, 1,500,000,  2,500,000
            // this is roundingUnit(500,000) and then cumulative total of
            //roundingUnit(500,000) + roundingToNearest (1,000,000);
            System.out.println(formattedRoundedNumber);
            formattedRoundedNumber=population;
        }
        
        else
        {
            System.out.println("Population is not divisible by: " + roundingUnit);
            ///we then know we are dealing with largePortion such as    
            //500,000,  1,500,000,   2,500,000  since each are
            //multiple of 500,000 and can be divided by it 1,3,5 times respectively
            //so the condition would be true
            //in the case of the Nice population, largePortion = 500,000. Likewise if the largePortion
            //was 1,500,000 I have no logic that I presented in my logic, ironically 
            //for a relatively fundemental process.
            //If ((frequencyRoundedNumberInLargePortion%2) == 1)  
        
        System.out.println("What is frequencyRoundedNumberInLargePortion: " + frequencyRoundedNumberInLargePortion);
        
        if(frequencyRoundedNumberInLargePortion%2==1)
        {
            System.out.println("Large portion population is in this sequence: " + roundingUnit+ ", " + (roundingUnit+roundingToNearest)+ ", " + (roundingUnit+roundingToNearest+roundingToNearest+" ....."));
            formattedRoundedNumber = largePortion + roundingUnit;
        }
        else
        {
            //the small portion is less than 500,000, so rounds downwards...
            if (checkNegativeNumber< (-1*roundingUnit))
            {
                System.out.println("Small portion is less than: " + roundingUnit);
                smallPortionRounded = 0;
                formattedRoundedNumber =  largePortion + smallPortionRounded;
            }
            
	        //it should not enter here, otherwise there is error somewhere.
            // since checkNegativeNumber = smallPortion - roundingToNearest;
	        //it would mean smallPortion is negative or smallPortion is zero.
	        //not possible at all unless calculation has failed elsewhere.

            else  //(if checkNegativeNumber>=  -500,000)
            {
                System.out.println("Small portion is greater than or equal to: " + roundingUnit);
                System.out.println("Potential error in calculations, small portion being rounded up to nearest " + roundingToNearest);
		        smallPortionRounded=roundingToNearest;
                formattedRoundedNumber = largePortion + smallPortionRounded;
            }
            
        }  //end of else
            
        }  // end of big else
        
        if (smallPortion==0 && largePortion==0 && divisbleByRoundingUnit==0)
        {
            System.out.println("The initial population is 0");
            formattedRoundedNumber = 0;
            //this is a way to validate if the population is 0
            //but since this method is no longer returning,
            //it will be moved just before the method end 
        }
        
        millionsRoundingFormatted[i][1] = String.valueOf(formattedRoundedNumber);
        System.out.println("Population: " + millionsRounding[i][1] + "=> " + millionsRoundingFormatted[i][1] + " (" + "rounded to nearest " + roundingToNearest + ")");
    
    }//end of method   
} //end of class 