You are given an integer array nums of length n.
In one operation, choose any subarray nums[l...r] (0 <= l <= r < n) and replace each element in that subarray with the bitwise AND of all elements.
Return the minimum number of operations required to make all elements of nums equal.
Example 1:
Input: nums = [1,2]
Output: 1
Explanation:
Choose nums[0...1]: (1 AND 2) = 0, so the array becomes [0, 0] and all elements are equal in 1 operation.
Example 2:
Input: nums = [5,5,5]
Output: 0
Explanation:
nums is [5, 5, 5] which already has all elements equal, so 0 operations are required.
Constraints:
1 <= n == nums.length <= 1001 <= nums[i] <= 105Problem Overview: You are given an array of integers and need the minimum number of operations required to make every element identical. Each operation effectively changes individual bits of numbers, so the task reduces to deciding the optimal final bit configuration shared by all elements.
Approach 1: Bit Counting / Single Pass (O(n * B) time, O(1) space)
Look at the array bit-by-bit instead of value-by-value. For any bit position b, some numbers have that bit set while others do not. If all numbers must become equal, every element must end up with the same value at that bit. The cheapest choice is whichever already appears more frequently. Count how many elements have bit b set (ones) and how many do not (zeros). Converting the minority side requires min(ones, zeros) operations. Repeat this logic for every bit position and sum the costs.
This works because each bit position is independent. Flipping a bit in one number does not affect other bits, so the optimal strategy is simply minimizing flips per bit. While iterating through the array once, update counts for each bit using standard Bit Manipulation operations like (num >> b) & 1. After processing the array, compute the minimal operations across all bit positions.
The approach behaves like a frequency problem applied to bits. Instead of comparing full integers, you align the binary representation column-by-column. This keeps the implementation simple and avoids sorting or repeated transformations often seen in typical Array equalization problems.
Recommended for interviews: The single-pass bit counting approach is what interviewers expect. It demonstrates that you recognized the independence of bit positions and reduced the problem to a counting task. A naive idea of repeatedly modifying numbers until they match shows intuition, but the bit-frequency insight proves you understand how bitwise operations simplify array transformation problems.
If all elements in nums are equal, no operations are needed; otherwise, we can select the entire array as a subarray and perform one operation.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Equalization | O(n^2) | O(1) | Conceptual baseline when trying every target value |
| Bit Counting (Single Pass) | O(n * B) | O(1) | Optimal approach; iterate once and minimize flips per bit |
| Frequency-Based Target Selection | O(n) | O(n) | Useful if explicitly transforming to the most common value |
3674. Minimum Operations to Equalize Array (Leetcode Easy) • Programming Live with Larry • 441 views views
Watch 9 more video solutions →Practice Minimum Operations to Equalize Array with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor