Ctrl K

Three-Way Split With a Cap

Easy
Three neighbors share total identical apples. Every apple must go to someone, but no neighbor will take more than cap apples. Someone may get none at all. How many ways are there to share the apples? Two ways differ if some particular neighbor gets a different number of apples, so (1, 0, 2) and (2, 0, 1) count separately.
Example 1
total=4, cap=2 → 6

The valid splits are (0, 2, 2), (2, 0, 2), (2, 2, 0), (1, 1, 2), (1, 2, 1) and (2, 1, 1): six ways.

Example 2
total=7, cap=2 → 0

Even 2 each adds up to only 6, so 7 cannot be handed out.

Example 3
total=2, cap=5 → 6
Constraints
  • 1 ≤ total ≤ 50
  • 1 ≤ cap ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsXOR of Every Subset EasyThree-Way Split With a Cap MediumShuffle into a Divisible Palindrome HardThe K-th Odd-Even Zigzag HardNearest Short Hex Color Easy
loading Python…
Input
total=4, cap=2
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 74 tests on the server, hidden ones included.