79.3k views
5 votes
In a birthday party, there are ’n’ children invited and all of them are seated in a line. Initially, few candies were distributed to each child and some children were missed and did not receive any candy. Our aim is to give an equal number of candies to each child. In order to do so, we can choose each move such that any ‘m’ (1 <= m <= n) children can pass one of their candy to their neighboring child at the same time. Given an integer array ‘candies’ representing the number of candies with each child from left to right on the line, return the minimum number of moves to make all the children have the same number of candies. If it is not possible to do, return -1.

User Arias
by
7.6k points

1 Answer

3 votes

Final answer:

In mathematics, to ensure an equal distribution of candies among children seated in a line, the sum of candies must be divisible by the number of children. If divisible, the minimum moves for equal distribution is determined by calculating the excess candies each child has relative to the average, with the maximum excess dictating the minimum number of moves.

Step-by-step explanation:

The question deals with a problem in the field of mathematics, more specifically within combinatorics and the arithmetic of distribution. The challenge is to ensure an equal distribution of candies among 'n' children seated in a line with various starting quantities of candies.

Firstly, to determine if equal distribution is possible, the sum of all the candies must be divisible by the number of children (n). If it's not divisible, then it is impossible to have all children with the same amount of candy, and the answer would be -1.

If equal distribution is possible, the next step is to determine the minimum number of moves to achieve this distribution. A move consists of transferring one candy from any child to their immediate neighbor. The number of moves required will depend on the differences between the number of candies each child has and the average number of candies they should have after the distribution.

Given the integer array 'candies', you calculate the average number of candies per child (total candies/n). Then, iterate through the array, keeping track of the excess candies that need to be passed to achieve the average. The number of moves will be the cumulative sum of these excess values. The child with the maximum excess will dictate the minimum number of moves needed for distribution.

User Muthuraj
by
7.7k points