Ctrl K

Pies With Nothing Left Over

Medium
A bakery makes two kinds of pies: - a large pie takes 4 apples and 1 crust; - a small pie takes 2 apples and 1 crust. The storeroom has apples apples and crusts crusts. The goal is to bake pies that use up every last apple and crust. Return [large, small], the number of large and small pies to bake. If there is no way to use everything up exactly, return an empty list.
Example 1
apples=10, crusts=3 → [2, 1]

Two large pies (8 apples, 2 crusts) and one small pie (2 apples, 1 crust) use exactly 10 apples and 3 crusts.

Example 2
apples=9, crusts=3 → []

Every pie takes an even number of apples and 9 is odd, so nothing works.

Example 3
apples=2, crusts=3 → []

Three crusts need at least 6 apples, and there are only 2.

Constraints
  • 0 ≤ apples ≤ 10⁸
  • 0 ≤ crusts ≤ 10⁸
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumBackwards Digits in 32 Bits MediumNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings Medium
loading Python…
Input
apples=10, crusts=3
Expected
[2, 1]

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