Ctrl K

Apples in Three Baskets

Hard
You need to put total identical apples into three numbered baskets. Each basket holds at most cap apples, a basket may stay empty, and every apple must be placed. How many ways are there? Two ways differ if some basket gets a different number of apples.
Example 1
total=4, cap=2 → 6

The options are (2,2,0), (2,0,2), (0,2,2), (2,1,1), (1,2,1), (1,1,2), six ways.

Example 2
total=2, cap=5 → 6

The cap never matters here: (2,0,0), (0,2,0), (0,0,2), (1,1,0), (1,0,1), (0,1,1).

Example 3
total=10, cap=3 → 0
Constraints
  • 1 ≤ total ≤ 10⁸
  • 1 ≤ cap ≤ 10⁸
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTasters and the Spoiled Cask HardRadio Show Without Quick Repeats HardParcel Run Orders HardA Fair Split of Colored Beads HardOther Orders for the Same Search Tree Hard
loading Python…
Input
total=4, cap=2
Expected
6

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