Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many non-negative integer solutions (x, y, z) of x + y + z = 10 satisfy x <= 4? Run the code.
Problem: How many non-negative integer solutions (x, y, z) of x + y + z = 10 satisfy x <= 4? Run the code.
print(sum(1 for x in range(0, 11) for y in range(0, 11) for z in range(0, 11) if x + y + z == 10 and x <= 4))What is printed?
- A. 66
- B. 55
- C. 35
- D. 45
Correct answer: D. 45
For each x from 0 to 4 there are 11 - x choices for (y, z), giving 11 + 10 + 9 + 8 + 7 = 45.
More Coding Interview Problems questions
- Problem: How many pairs (x, y) with x <= y and x + y = 12 lie in 1..11? Run the code. python print(sum(1 for x in range(1, 12) for y in…
- Problem: How many inversions does the array [4, 3, 2, 1, 0] have? Run the code. python a = [4, 3, 2, 1, 0] print(sum(1 for i in…
- Problem: How many pairs (i < j) of [5, 6, 7, 9, 8] satisfy |ai - aj| = 1? Run the code. python a = [5, 6, 7, 9, 8] print(sum(1 for i in…
- Problem: How many integers in [1, 500] are multiples of 3 but not of 5? Run the code. python print(sum(1 for x in range(1, 501) if x % 3…
- Problem: How many non-negative integer pairs (x, y) satisfy 2x + 3y = 12? Run the code. python print(sum(1 for x in range(0, 7) for y in…
- Problem: Which algorithm solves the minimum-cost perfect assignment problem (bipartite, n x n cost matrix) in O(n^3) time?