Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code.
Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code.
a = [4, 7, 1] m = min(a) print(sum(x - m for x in a))What is printed?
- A. 12
- B. 6
- C. 9
- D. 2
Correct answer: C. 9
Costs are (4 − 1) + (7 − 1) + 0 = 9. Decrementing to 0 gives 12, using only max − min gives 6, and 2 counts the elements that change.
More Coding Interview Problems questions
- Problem: What is the number of paths from (1,1) to (3,3) moving only right or up in a grid with all cells free? Run the DP. python g =…
- Problem: How many distinct values does a XOR b take for a, b in {0, 1, 2, 3}? Run the code. python print(len({a ^ b for a in range(4) for…
- Problem: What is the sum of all integers in [1, 200] that are both odd and multiples of 5? Run the code. python print(sum(x for x in…
- Problem: What is the maximum number of distinct characters in a substring of length 4 of 'abcabcabc'? Run the code. python s = 'abcabcabc'…
- Problem: How many ways are there to tile a 2 x 6 board with 2 x 1 dominoes? Run the DP. python f = [1, 1] for n in range(2, 7)…
- Problem: What is the minimum number of edges to remove from a 5-vertex complete graph to leave a spanning tree? Run the code (answer = E…