/*
Key Points - chatGPT
1.	frequencyRoundedNumber is retained exactly as in your original code, since itâ€™s used in multiple places.
2.	Exact divisibility bug fixed â€” now populations that are exact multiples of roundingUnit are not rounded up unnecessarily.
3.	divisbleByRoundingUnit renamed â†’ remainderAfterDivision for clarity.
4.	All other logic (smallPortion / largePortion / odd-sequence rounding) is fully intact.
5.	Comments explain where the fix was applied.
*/

/*
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 long checkNegativeNumber;
    static long smallPortion;
    static int i;
    static long roundingUnit;
    static long smallPortionRounded;
    static long formattedRoundedNumber;
    static String unitsRoundingFormatted[][];
    static String unitsRounding[][];
    static long roundingToNearest=17;
    static long largePortion;
    static long remainderAfterDivision;  //this is new variable replacing divisbleByRoundingUnit
    static long population;
    static long frequencyRoundedNumber;
    static long frequencyRoundedNumberInLargePortion;
    static boolean roundtoNearestOddNumber;
    static String roundDirection;
    static boolean hasSamePopulationAsRoundingToNearest;
    static boolean isExactlyDivisibleRoundingUnit = false;
    
    public static void main(String[] args) 
    {
        
       //ORIGINAL TEST CASES
       unitsRounding = new String[][]
                                       {
                                        {"Nice","150"},
                                        {"Abu Dhabi", "960"},
                                        {"Naples", "301"},
                                        {"Vatican City", "750"},
                                        {"London", "299"},
                                        {"Birmingham", "40"}
                                        };
         
          /*
          //FURTHER TEST CASES AS PER EXAMPLE  ***PASS
            unitsRounding = new String[][]
                                        {
                                        {"Nice","6"},
                                        {"Abu Dhabi", "18"},
                                        {"Naples", "20"},
                                        {"Vatican City", "21"}
                                        };
            */
        
            /*
            //FURTHER DEVISED TEST CASES  *****PASS
            unitsRounding = new String[][]
                                        {
                                        {"Nice","1000000"},
                                        {"Abu Dhabi", "500001"},
                                        {"Naples", "1"},
                                        {"Vatican City", "0"},
                                        {"London", "10234865"},
                                        {"Birmingham", "1532896"}
                                        };
            */
         
            /* 
            //FURTHER DEVISED TEST CASES   ***PASS
            unitsRounding = new String[][]
                                        {
                                        {"Nice","4"},
                                        {"Abu Dhabi", "6"},
                                        {"Naples", "55"},
                                        {"Vatican City", "33"},
                                        {"London", "3315"},  //error
                                        {"Birmingham", "600067"}
                                        };
            */
        
            /*
            //DEVISED TEST CASES AS PER LOGIC DOCUMENTATION  ***PASS
            unitsRounding = new String[][]
                                        {
                                        {"Nice","274214"},
                                        {"Abu Dhabi", "500001"},
                                        {"Naples", "1"},
                                        {"Vatican City", "0"},
                                        {"London", "10234865"},
                                        {"Birmingham", "1532896"}
                                        };
            */
         
            //DO NOT COMMENT THIS OUT
        unitsRoundingFormatted = new String[][]
                                    {
                                    {"Nice",""},
                                    {"Abu Dhabi", ""},
                                    {"Naples", ""},
                                    {"Vatican City", ""},
                                    {"London", ""},
                                    {"Birmingham", ""},
                                    };
          
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("***************ROUND TO NEAREST: ************:     " + roundingToNearest);
        System.out.println("Original population:");
        
        getPopulation(unitsRounding);
        
        if (roundingToNearest%2==0)
        {
            roundingUnit=roundingToNearest/2;
        }
        
        if (roundingToNearest%2==1)
        {
            roundingUnit=roundingToNearest;
            roundtoNearestOddNumber=true;
        }
        
        for (i=0; i<unitsRounding.length;i++)
        {
            System.out.println("\nProcessing population for: " + unitsRounding[i][0] + "("+unitsRounding[i][1]+")");
            population = Long.valueOf(unitsRounding[i][1]);
            
            frequencyRoundedNumber = population/roundingUnit;
            smallPortion =  population - (frequencyRoundedNumber * roundingUnit);
            largePortion = frequencyRoundedNumber * roundingUnit;  
            frequencyRoundedNumberInLargePortion = largePortion/roundingUnit;
        
            if (smallPortion < 0)
            {
                System.out.println("Small portion less than 0");
                smallPortion = population;
                largePortion = 0;
                frequencyRoundedNumber = 0;
            }
        
            roundSmallPortion();
        }
        System.out.println("\nPopulation rounded to nearest " + roundingToNearest);
        getPopulation(unitsRoundingFormatted);
    }
    
    public static void getPopulation(String [][] populationData)
    {
        for (String[]m:populationData)
        {
            System.out.println(Arrays.asList(m));
        }
    }
    
    public static void roundSmallPortion()
    {
        remainderAfterDivision = population%roundingUnit;
        System.out.println("Processing the small portion: " + smallPortion);
        System.out.println("This is large portion: " + largePortion);
        
        checkNegativeNumber = smallPortion - roundingToNearest;
        
        // OLD CODE: always rounds up even if already divisible
        /*
        if (divisbleByRoundingUnit==0 && !roundtoNearestOddNumber)
        {
            System.out.println("Population is exactly divisible by: " + roundingToNearest);
            formattedRoundedNumber=largePortion+roundingUnit;
            isExactlyDivisibleRoundingUnit = true;
        }
        */
        
        //************************************************************************
        // NEW CODE CHATGPT: only round up if smallPortion > 0
        if (remainderAfterDivision==0 && !roundtoNearestOddNumber)
        {
            System.out.println("Population is exactly divisible by: " + roundingToNearest);
            
            if (smallPortion > 0) 
            {
                formattedRoundedNumber = largePortion + roundingUnit;
            } 
            else 
            {
                formattedRoundedNumber = largePortion;
            }
            isExactlyDivisibleRoundingUnit = true;
        }
        //**************************************************************************************
        else
        {
            
            if(frequencyRoundedNumberInLargePortion%2==1  && !roundtoNearestOddNumber)
            {
                System.out.println("Large portion population is in this sequence: " + roundingUnit+ ", " + (roundingUnit+roundingToNearest)+ ", " + (roundingUnit+roundingToNearest+roundingToNearest+" ....."));
                formattedRoundedNumber = largePortion + roundingUnit;
            }
            else
            {
                //This is only way I could incorporate this screen message output for small portion in which
                //the provided total population is even
                //I have added this logic. Basically it is a repeat that occurs for odd populations
                //I had to set the condition exact opposite to what takes it into portion round upwards
               
                if (smallPortion<=((roundingToNearest -1)/2) && smallPortion!=0)
                {
                    System.out.println("Small portion round downwards: " + smallPortion + "=> " + "0");
                }
                
                if (checkNegativeNumber< (-1*roundingUnit))
                {  
                    smallPortionRounded = 0;
                    formattedRoundedNumber =  largePortion + smallPortionRounded;
                    
                    //We no longer need this exclusiveness to perform following
                    //if (!isExactlyDivisibleRoundingUnit)
                    //{
                        //System.out.println("Small portion round downwards: " + smallPortion + "=> " + "0");
                        //very likely unused, removed by myself
                        //roundDirection="down";
                    //}
                }
                else
                {
                    if (smallPortion<=((roundingToNearest -1)/2))
                    {
                        if (smallPortion==0)
                        {
                            System.out.println("No smaller portion to the population");
                            
                            //very likely unused
                            roundDirection="none";
                        }
                        else
                        {
                            roundDirection="down";
                        }
                        
                        formattedRoundedNumber= largePortion;
                    }
                    else
                    {
                        //re-instated screen output from final code
                        System.out.println("Small portion round upwards: " + smallPortion + "=> " + ((roundingToNearest-smallPortion)+smallPortion));
                        formattedRoundedNumber=largePortion+roundingToNearest;
                        
                        //very likely unused, removed by myself
                        roundDirection="up";
                        formattedRoundedNumber=largePortion+roundingToNearest;
                    }
                }
            }
        }
        
        if (smallPortion==0 && largePortion==0 && remainderAfterDivision==0)
        {
            System.out.println("The initial population is 0: " + unitsRoundingFormatted[i][1]);
            formattedRoundedNumber = 0;
        }
        
        if (population==roundingToNearest)
        {
            unitsRoundingFormatted[i][1]=String.valueOf(population);
            hasSamePopulationAsRoundingToNearest=true;
        }
        else
        {
            unitsRoundingFormatted[i][1] = String.valueOf(formattedRoundedNumber);
        }
        
        //I removed this and re-instated correct logic in block below from my final code
        //if (hasSamePopulationAsRoundingToNearest)
        //{
        //    roundDirection="NONE";
        //}
        
        //I re-instated this from my final code
        if (hasSamePopulationAsRoundingToNearest || remainderAfterDivision==0)
        {
            roundDirection="NONE";
        }
        else
        {
            if (Long.valueOf(unitsRoundingFormatted[i][1])>Long.valueOf(unitsRounding[i][1]))
            {
                roundDirection="UP";
            }
            else if (Long.valueOf(unitsRoundingFormatted[i][1])<Long.valueOf(unitsRounding[i][1]))
            {
                roundDirection="DOWN";
            }
        }
        
        hasSamePopulationAsRoundingToNearest=false;
        System.out.println("Population: " + "(rounded " + roundDirection+") " + unitsRounding[i][1] + "=> " + unitsRoundingFormatted[i][1] + " (" + "to nearest " + roundingToNearest + ")");
        isExactlyDivisibleRoundingUnit = false;
    }
}