Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
We have n chips, where the position of the ith chip is position[i].
We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to:
position[i] + 2 or position[i] - 2 with cost = 0.position[i] + 1 or position[i] - 1 with cost = 1.Return the minimum cost needed to move all the chips to the same position.
Example 1:
Input: position = [1,2,3] Output: 1 Explanation: First step: Move the chip at position 3 to position 1 with cost = 0. Second step: Move the chip at position 2 to position 1 with cost = 1. Total cost is 1.
Example 2:
Input: position = [2,2,2,3,3] Output: 2 Explanation: We can move the two chips at position 3 to position 2. Each move has cost = 1. The total cost = 2.
Example 3:
Input: position = [1,1000000000] Output: 1
Constraints:
1 <= position.length <= 1001 <= position[i] <= 10^9Problem summary: We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to: position[i] + 2 or position[i] - 2 with cost = 0. position[i] + 1 or position[i] - 1 with cost = 1. Return the minimum cost needed to move all the chips to the same position.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[1,2,3]
[2,2,2,3,3]
[1,1000000000]
minimum-number-of-operations-to-move-all-balls-to-each-box)split-with-minimum-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1217: Minimum Cost to Move Chips to The Same Position
class Solution {
public int minCostToMoveChips(int[] position) {
int a = 0;
for (int p : position) {
a += p % 2;
}
int b = position.length - a;
return Math.min(a, b);
}
}
// Accepted solution for LeetCode #1217: Minimum Cost to Move Chips to The Same Position
func minCostToMoveChips(position []int) int {
a := 0
for _, p := range position {
a += p & 1
}
b := len(position) - a
if a < b {
return a
}
return b
}
# Accepted solution for LeetCode #1217: Minimum Cost to Move Chips to The Same Position
class Solution:
def minCostToMoveChips(self, position: List[int]) -> int:
a = sum(p % 2 for p in position)
b = len(position) - a
return min(a, b)
// Accepted solution for LeetCode #1217: Minimum Cost to Move Chips to The Same Position
struct Solution;
impl Solution {
fn min_cost_to_move_chips(chips: Vec<i32>) -> i32 {
let n = chips.len();
let mut a = 0;
let mut b = 0;
for i in 0..n {
if chips[i] % 2 == 0 {
a += 1;
} else {
b += 1;
}
}
i32::min(a, b)
}
}
#[test]
fn test() {
let chips = vec![1, 2, 3];
assert_eq!(Solution::min_cost_to_move_chips(chips), 1);
let chips = vec![2, 2, 2, 3, 3];
assert_eq!(Solution::min_cost_to_move_chips(chips), 2);
}
// Accepted solution for LeetCode #1217: Minimum Cost to Move Chips to The Same Position
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1217: Minimum Cost to Move Chips to The Same Position
// class Solution {
// public int minCostToMoveChips(int[] position) {
// int a = 0;
// for (int p : position) {
// a += p % 2;
// }
// int b = position.length - a;
// return Math.min(a, b);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.