## Candy Distribution Problem in JavaIt is a problem frequently asked in interviews of top IT companies like ## Problem StatementThe problem states that distribute the minimum number of candies to N children such that: - Each child must have at least one candy.
- The children with a higher ranking will get more candies than their neighbors.
Note that each child has a rating. The task is to the minimum number of candies you must give. Let's understand the problem through an example. ## Example 1:Suppose, there are four children A, B, C, and D having 1, 2, 3, and 4 rankings, respectively. Let's distribute the candy one by one to each child. Let's take another example.
Suppose, there are four children A, B, C, and D having 4, 3, 2, and 1 ranking, respectively. In the first round, we will distribute 1 candy to each child. We observe that child A and child B have the same number of candies while child A has a greater ranking than child B. Therefore, it violates the condition. So, we will distribute 1 more candy to child A. We observe that child A and B satisfies the condition but child B and C does not satisfy the condition. Because B and C are neighbors and have 1-1 candy. Again, we observe that children A and B have the same number of candies while child A has a greater ranking than child B. So, we will distribute 1 more candy to child A. We observe that children A and B satisfies the condition but child C and D does not satisfy the condition. Because C and D are neighbors and have 1-1 candy. Having the same number of candies with different ranking is not allowed. So, we will distribute one more candy to child C. Again, we observe that children B and C have the same number of candies while child B has a greater ranking than child C. So, we will distribute 1 more candy to child B. We observe that children A and B have the same number of candy while child A has a greater ranking than child B. Therefore, it violates the condition. So, we will distribute 1 more candy to child A. Now, we have distributed the candies according to the conditions specified in the problem. Hence, we require 10 (4 + 3 + 2 + 1) candies to distribute each child. In the above two examples, the ranking of the child was already sorted. So, what if ranking is not in a sorted manner.
Consider the following arrangement of child ranking. 1, 2, 6, 5, 4, 3, 1 Distribute 1-1 candy to each child. We observe that child B has a greater ranking than child A but has the same number of candies which is not fair. So, we will distribute 1 more candy to child B. We observe that child C has a greater ranking than child B but child B has more candy than child C which is not fair. So, we will distribute 1 more candy to child C. We observe that children B and C have the same number of candies while the ranking of the child is C is greater so it is not fair. Distribute 1 more candy to child C. Here, a point to note that children D, E, F, and G have ranks 5, 4, 3, and 1, respectively that are in decreasing order. These arrangements never form a good arrangement. So, in such a case first, distribute candies from
Therefore, the final distribution will be ## Solution to The ProblemThe problem can be solved by using the following four approaches: - Using Brute Force Approach
- Using Two Arrays
- Using One Array
- Single Pass Approach with Constant Space
## Using Brute Force ApproachIt is the simplest approach for solving the problem. In this approach, we have used a 1D array. An array named candies keeps track over the candies distributed to the children. Initially, we distribute one candy to each child. After that, start tracing the array from left to right fashion. During the tracing of the array, we check the following: - If the current element's (i-th) ranking (
**rakning[i]**) is greater than the previous element's ranking (**ranking[i-1**]) and**candies[i]**is less than equal to**candies[i-1]**, update the number of candies for the current element (i-th) as**candies[i - 1] + 1.**
Therefore, the candy distribution for current and previous elements becomes according to the condition specified in the problem statement. In the above step, we have checked the current element with the previous element. Now, we will also check the current element with the next element, in the same step. - If the current element's (i-th) ranking (
**rakning[i]**) is greater than the next element's ranking (**ranking[i+1**]) becomes true, update the number of candies by 1 for the current element as**candies[i + 1] + 1.**
Repeat the above conditions for the whole ranking array. If the above tracing does not update the number of candies in the At last, Let's implement the above logic in a Java program.
The minimum number of candies require to distribute is: 18
For the above approach, the time complexity is O(n) because we have not used any extra space.## Using Two ArraysIn this approach, we have used two arrays (left2right and right2left). These two arrays store the number of candies required by the current child taking care of the distribution relative to the left and right neighbors, respectively. For The child with a higher ranking than their left neighbor should always get more candies than their left neighbor. For The child with a higher ranking than their right neighbor should always get more candies than their right neighbor. Initially distribute 1 candy to each child, as we have done in the previous approach. For the same, we have filled the array with 1 by invoking the After that, start traversing over the arrays. First, we perform traversal over left2right array. If the current element's (i-th) ranking is greater than the it's left neighbor, update the current element's number of candies in the left2right array as Since the current element's (child) candies are always less than or equal candies than its left neighbor before updating. After traversing the left2right array, traverse over the When we have done with both the traversal (left to right and right to left), find the The above functionality can also be represented in mathematical function as follows: Where n, represents the length of the ranking array. Let's implement the above logic in a Java program.
The minimum number of candies require to distribute is: 28
The time complexity for the above approach is ## Using One ArrayThe approach is very similar to the previous one. The only difference is in the number of arrays we have used. Previously, we have used two arrays that make code more complex and require more memory. In order to reduce this problem, we have used a single array named So, in this approach too, we will distribute 1 candy to each child. After that, start traversing over the ranking array from It means, if the current element's (i-th) ranking is greater than it's left neighbor and has a less or an equal number of candies, update the current child's number of candies in the candies[] array as ## Note: While update array, do not compare the current element (candies[i]) with the previous elements (candies[i-1]) because before updation candies[i] <= candies[i - 1].After that, traverse over the ranking array in In backward traversal, if ra Update the number of candies of the current element's only if It is because this time we have already updated the candies array during the left to right traversal. Therefore, candies[i] is not necessarily less than or equal to candies[i + 1]. Consequently, if When we have done with both the traversal (left to right and right to left), find the The above functionality can also be represented in mathematical function as follows: Where n, represents the length of the ranking array. Let's implement the above logic in a Java program.
The minimum number of candies require to distribute is: 21
The time complexity for the above approach is ## Single Pass Approach with Constant SpaceThe approach is different form the above three approaches. It depends on the observations. In order to distribute candies according to the problem statement, always distribute candies in terms of increments of 1. The minimum number of candies each child will get is 1. So, in this case, the distribution may take the form like 1, 2, 3, …., n or n, …. , 3, 2, 1. We can add these number of candies that gives the minimum number of candies to distribute. The same can be achieved by using the following mathematical formula: Now, we can see the given ranking that may either in increasing or decreasing order. Whenever the ranking slope will rise, the distribution will take the form 1, 2, 3, …., m. on the other hand, falling slope will take the form k, …. , 3, 2, 1. Here, a challenge arises that the local peak point can be included in only one of the slopes either in rising or falling slope. So, we have to decide in which slope we must include local peak point (n). The peak point should be max of the count determine by the rising and falling slope that satisfies both left and right neighbor criteria. In order to determine the number of candies required for distribution, the peak point should be included in the slope which contains a greater number of points. Let's see the implementation of the approach. In the following Java program, we have used two variables Update the total count of candies at the end of a falling slope. If the ranks are in a level of the point, means end of mountains in the graph. While, we reach at the end of the mountain, determine where we have to include peak point. It can be determined by comparing the variables Thus, the count assigned to the peak element becomes The following figure shows the cases that need to be handled for this example: rankings: [1 2 3 4 5 3 2 1 2 6 5 4 3 3 2 1 1 3 3 3 4 2] Let's implement the above approach in a Java program.
The minimum number of candies require to distribute is: 20
The time complexity for the above approach is |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]