Skip to main content

Minimum Operations to Equalize Array - Solution & Explanation

Practice this problem

Problem Statement

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.

A subarray is a contiguous non-empty sequence of elements within an array.

 

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 <= 100
  • 1 <= nums[i] <= 105

Approach Overview

Problem 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.

Solution

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EqualizationO(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 SelectionO(n)O(n)Useful if explicitly transforming to the most common value

Video Solution

3674. Minimum Operations to Equalize Array (Leetcode Easy) • Programming Live with Larry • 452 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Operations to Equalize Array easy or hard?
Minimum Operations to Equalize Array is categorized as an Easy problem. The challenge lies in recognizing that each bit position can be optimized independently using a counting strategy.
Minimum Operations to Equalize Array Python/Java solution
The implementation iterates through the array, checks each bit using bit shifts, and counts how many elements contain that bit. After processing, the algorithm adds min(ones, zeros) for each bit position. The same logic works identically in Python, Java, C++, Go, and TypeScript.
How to solve Minimum Operations to Equalize Array in O(n)?
Traverse the array once while tracking how many numbers have each bit set. For each bit position, compute min(count_of_ones, n - count_of_ones) because converting the minority bits yields the fewest operations. Summing these values gives the minimum operations needed.
What is the best approach for Minimum Operations to Equalize Array?
The optimal approach counts set bits at each position across the array. For every bit position, choose the majority value (0 or 1) and flip the minority. The total operations equal the sum of min(ones, zeros) for each bit. This runs in O(n * B) time where B is the number of bits (usually 32).
Is Minimum Operations to Equalize Array asked at Google/Amazon/Meta?
Problems involving bit counting and minimizing transformations appear frequently in interviews at companies like Google, Amazon, and Meta. Variants of this question test understanding of bit manipulation and array frequency analysis.
What data structure is used in Minimum Operations to Equalize Array?
The solution primarily uses simple counters and bitwise operations. No complex data structure is required—just integer counters for each bit position and standard array iteration.
What is the time complexity of Minimum Operations to Equalize Array?
The optimal solution runs in O(n * B) time, where n is the array length and B is the number of bits in the integer representation (typically 32). Space complexity is O(1) since only a few counters are maintained.

Ready to solve this problem?

Practice Minimum Operations to Equalize Array with our built-in code editor and test cases.

Practice on FleetCode