Bursting the Balloons
InterviewTime limit0.25sMemory limit512 MB
Given N balls and K baskets, decide whether N can be split into K distinct positive integers and, if so, print the minimum possible difference between the largest and smallest part.
- Level
Medium5 of 10
- Topics
- Math, Greedy, Binary search, Implementation
- Solved
- No attempts yet
Problem
teams play a balloon bursting game. Each team has one basket, and must throw the balls in its basket to burst its own balloon.
To prepare the game, we must distribute balls into the baskets. For the game to be fun, the numbers of balls in the baskets must all be different. That is, the balls must be distributed into the baskets with none left over, each basket must hold at least 1 ball, and the numbers of balls in the baskets must all be distinct.
To reduce the unfairness of the game, we will distribute the balls so that the difference between the number of balls in the fullest basket and the number in the emptiest basket is minimized.
The rules for distributing the balls into the baskets are as follows.
- The balls are distributed into the baskets with none left over.
- Each basket holds at least 1 ball.
- The numbers of balls in the baskets are all distinct.
- The difference between the number of balls in the fullest basket and the number in the emptiest basket is minimized.
Write a program that, when distributing balls into baskets in a way that satisfies all the rules above, determines whether such a distribution is possible, and if it is, computes and prints the difference between the number of balls in the fullest basket and the number in the emptiest basket.
Input
The first line gives , the number of balls, and the integer , the number of teams.
Output
If the balls can be distributed into the baskets in a way that satisfies the rules, print the difference between the number of balls in the fullest basket and the number in the emptiest basket. If they cannot be distributed, print -1.