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.
You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums.
Return the minimum number of operations to make all elements of nums divisible by 3.
Example 1:
Input: nums = [1,2,3,4]
Output: 3
Explanation:
All array elements can be made divisible by 3 using 3 operations:
Example 2:
Input: nums = [3,6,9]
Output: 0
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 50Problem summary: You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums. Return the minimum number of operations to make all elements of nums divisible by 3.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,2,3,4]
[3,6,9]
minimum-moves-to-equal-array-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3190: Find Minimum Operations to Make All Elements Divisible by Three
class Solution {
public int minimumOperations(int[] nums) {
int ans = 0;
for (int x : nums) {
ans += x % 3 != 0 ? 1 : 0;
}
return ans;
}
}
// Accepted solution for LeetCode #3190: Find Minimum Operations to Make All Elements Divisible by Three
func minimumOperations(nums []int) (ans int) {
for _, x := range nums {
if x%3 != 0 {
ans++
}
}
return
}
# Accepted solution for LeetCode #3190: Find Minimum Operations to Make All Elements Divisible by Three
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
return sum(x % 3 != 0 for x in nums)
// Accepted solution for LeetCode #3190: Find Minimum Operations to Make All Elements Divisible by Three
impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
nums.iter().filter(|&&x| x % 3 != 0).count() as i32
}
}
// Accepted solution for LeetCode #3190: Find Minimum Operations to Make All Elements Divisible by Three
function minimumOperations(nums: number[]): number {
return nums.reduce((acc, x) => acc + (x % 3 !== 0 ? 1 : 0), 0);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.