Skip to content

Instantly share code, notes, and snippets.

@IngeFrodo
Last active August 18, 2020 19:44
Show Gist options
  • Save IngeFrodo/c5fcc636e722b2abf071cd2a65cd62cc to your computer and use it in GitHub Desktop.
Save IngeFrodo/c5fcc636e722b2abf071cd2a65cd62cc to your computer and use it in GitHub Desktop.
class DistributeCandiesToPeople {
public int[] distributeCandies(int candies, int num_people) {
int n = num_people;
int turns = 0;
int sum = n * (n + 1) / 2;
int n2 = n * n;
while (sum <= candies) {
candies -= sum;
sum += n2;
turns++;
}
int columns = (turns - 1) * turns / 2;
int[] people = new int[n];
for (int i = 0; i < n; i++) {
people[i] = columns * n + (i + 1) * turns;
}
int candie = Math.min(candies, turns * n + 1);
for (int i = 0; candies > 0; i++) {
people[i] += candie;
candies -= candie;
candie = Math.min(candies, candie + 1);
}
return people;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment