> 
> **Problem Statement**
> 
> 
> In a school, chocolate bars have to be distributed to children waiting
>  in a queue. Each Chocolate bar is rectangular in shape. Consider its
>  side lengths are integer values.
> 
> 
> The distribution procedure is as follows:
> 
> 
> 1. If a bar is not square in shape, then the largest possible square piece of Chocolate is broken and given to the first child in queue.
> 2. If bar is square in shape, then complete bar is given to the first child in queue.
> 
> 
> Once a child receives his share of Chocolate, he leaves the queue. The
>  remaining portion of the Chocolate bar is dealt in same fashion and
>  the whole or a portion of it is given to the next child in the queue.
> 
> 
> School has a carton of Chocolate bars to be distributed among the
>  children all over the School. The Chocolate bars in the carton are of
>  different sizes. A bar of length i and breadth j is considered to be
>  different from a bar of length j and breadth i.
> 
> 
> For every i such that M<=i<=N and every j such that P<=j<=Q (where M,
>  N, P and Q are integers). Each Chocolate bar in carton is unique in
>  length (i) and breath(j).
> 
> 
> Given the values of M, N, P and Q (where M, N values are the ranges
>  for length of Chocolate and P, Q values are the ranges for breadth of
>  the chocolate). Find the number of children who will receive Chocolate
>  from the carton. 
> 
> 
> **Input Specification:**
> 
> 
> M, N, P, Q are of integer type (M, N values are the ranges for length
>  of chocolate bar. P, Q values are the ranges for breadth of chocolate
>  bar).
> 
> 
> **Output Specification:**
> 
> 
> Number of children who will receive Cadbury bar from the carton.
> 
> 
> M = 5, N = 6, P = 3, Q=4 Here, i can be from 5 to 6 and j can be from
>  3 to 4. So the four bars will be in carton of sizes 5x3, 5x4, 6x3,
>  6x4. 
> 
> 
> First we choose a cadbury bar of size 5x3:
> 
> 
> * first child would receive 3x3 portion (remaining 2x3 portion)
> * next child would receive 2x2 portion (remaining 2x1 portion)
> * now the remaining portion are 2 square pieces of (1x1), which can be given to 2 more children
> 
> 
> So the Cadbury bar with the size of 5x3 can be distributed to 4
>  children.
> 
> 
> Similarly we can find out number of children for rest of the
>  combinations (i.e. 5x4, 6x3, 6x4) in the given range as follows:
> 
> 
> [![Chocolate Distribution Explanation!](https://i.stack.imgur.com/In7sJ.png)](https://i.stack.imgur.com/In7sJ.png)
> 
> 
> 

Please let me know the corrections that I can make to improve the code.

```
public class CandidateCode {

    public int distributeChocolate(int input1,int input2,int input3,int input4){
        int[] chocolatelengthLimits = {input1,input2};
        int[] chocolatewidthLimits = {input3,input4};
        Set<Chocolate> chocolateCarton = makeSetOfChocolatesOutOfTheLimits(chocolatelengthLimits, chocolatewidthLimits);
        return getTotalNumberofChildrenThatCanBeFed(chocolateCarton);
    }

    private int getTotalNumberofChildrenThatCanBeFed(Set<Chocolate> chocolateCarton){
        int totalNumberOfChildrenThatCanBeFed = 0;
        for (Chocolate chocolate : chocolateCarton) {
            int childrenFedFromTheChocolate = numberOfChildrenThatCanBeFedFromTheChocolate(chocolate);
            totalNumberOfChildrenThatCanBeFed+=childrenFedFromTheChocolate;
        }
        return totalNumberOfChildrenThatCanBeFed;
    }

    private Set<Chocolate> makeSetOfChocolatesOutOfTheLimits(int[] lengthLimits,int[] widthLimits){
        Set<Chocolate> chocolates = new HashSet<Chocolate>();
        for(int i=0;i<lengthLimits.length;i++){
            for(int j=0;j<widthLimits.length;j++){
                Chocolate rectangle = new Chocolate(lengthLimits[i], widthLimits[j]);
                chocolates.add(rectangle);
            }
        }
        return chocolates;
    }

    private int numberOfChildrenThatCanBeFedFromTheChocolate(Chocolate chocolate){
        int numberOfChildren = 0;
        int chocolateBlocksToExhaust = chocolate.getNumberOfChocolateBlocks();
        while (chocolateBlocksToExhaust!=0){
            int chocolateBlocksThatCanBeExhausted = chocolate.numberOfBlocksOfTheLargestSquareThatCanbeFormedFromTheChocolate();
            chocolateBlocksToExhaust=chocolateBlocksToExhaust-chocolateBlocksThatCanBeExhausted;
            numberOfChildren++;
        }
        return numberOfChildren;
    }
}

class Chocolate {

    int length;
    int width;

    public Chocolate(int length,int width){
        this.length=length;
        this.width=width;
    }

    public int numberOfBlocksOfTheLargestSquareThatCanbeFormedFromTheChocolate(){
        return decreaseDimensionsMaximumMinusMinimum();
    }

    private int decreaseDimensionsMaximumMinusMinimum(){
        if(this.length>this.width) {
            this.length=this.length-this.width;
            return this.width*this.width;
        }
        if(this.width>this.length) {
            this.width=this.width-this.length;
            return this.length*this.length;
        }
        if(this.length==this.width){
            return this.length*this.width;
        }
        return 0;
    }

    public int getNumberOfChocolateBlocks(){
        return this.length*this.width;
    }

    //HashCode and Equals Methods here      
}

```