#3674
Easy Algorithms Minimum operations to equalize array
Array Bit Manipulation Brainteaser
60.1% acceptance
Feb 25, 2026
58
12
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn min_operations(nums: Vec<i32>) -> i32 {
// Insight: applying one operation on the entire array sets every element
// to AND(nums[0..n-1]), making all elements equal in a single step.
// Therefore the answer is:
// 0 – all elements are already equal
// 1 – otherwise
if nums.iter().all(|&x| x == nums[0]) { 0 } else { 1 }
}
}