Leetcode 3190. Find Minimum Operations to Make All Elements Divisible by Three
[ OK ]google-code-82 — full content available
[ INFO ]category: Coding difficulty: medium freq: Optional first seen: 2026-03-13
[MEDIUM][CODING][OPTIONAL]
$catproblem.md
Practice/Google/Leetcode 3190. Find Minimum Operations to Make All Elements Divisible by Three
Leetcode 3190. Find Minimum Operations to Make All Elements Divisible by Three
CodingOptional
Problem
You are given an array of positive integers. In one operation, you can increment or decrement any element by exactly 1. Your goal is to make every element in the array divisible by three.
Return the minimum number of operations required to achieve this goal.
Requirements
Count how many operations are needed to make all elements divisible by 3
Each operation changes an element by exactly +1 or -1
An element is considered "done" when it's divisible by 3 (remainder is 0)
The solution should work for any positive integer array
Constraints
1 ≤ nums.length ≤ 10⁴
1 ≤ nums[i] ≤ 10⁹
Time complexity should be O(n)
Space complexity should be O(1)
Examples
Example 1:
`
Input: nums = [1, 2, 3]
Output: 2
Explanation:
Element 1: remainder is 1, need 1 operation (change to 0 or 3)
Element 2: remainder is 2, need 1 operation (change to 3)
Element 3: remainder is 0, need 0 operations
Total: 2 operations
`
Example 2:
Input: nums = [3, 6, 9] Output: 0 Explanation: All elements are already divisible by 3