Skip to main content

Minimize OR of Remaining Elements Using Operations - Solution & Explanation

HardArrayGreedyBit Manipulation13 min readAsked at: Aon
Practice this problem

Problem Statement

You are given a 0-indexed integer array nums and an integer k.

In one operation, you can pick any index i of nums such that 0 <= i < nums.length - 1 and replace nums[i] and nums[i + 1] with a single occurrence of nums[i] & nums[i + 1], where & represents the bitwise AND operator.

Return the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.

 

Example 1:

Input: nums = [3,5,3,2,7], k = 2
Output: 3
Explanation: Let's do the following operations:
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [1,3,2,7].
2. Replace nums[2] and nums[3] with (nums[2] & nums[3]) so that nums becomes equal to [1,3,2].
The bitwise-or of the final array is 3.
It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.

Example 2:

Input: nums = [7,3,15,14,2,8], k = 4
Output: 2
Explanation: Let's do the following operations:
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,15,14,2,8]. 
2. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,14,2,8].
3. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [2,2,8].
4. Replace nums[1] and nums[2] with (nums[1] & nums[2]) so that nums becomes equal to [2,0].
The bitwise-or of the final array is 2.
It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.

Example 3:

Input: nums = [10,7,10,3,9,14,9,4], k = 1
Output: 15
Explanation: Without applying any operations, the bitwise-or of nums is 15.
It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] < 230
  • 0 <= k < nums.length

Approach Overview

Problem Overview: You are given an array and allowed to perform up to k operations where two adjacent elements can be replaced with their bitwise AND. After all operations, compute the bitwise OR of the remaining elements. The goal is to minimize this final OR value.

Approach 1: Greedy Bit Elimination Using AND Segments (O(n log M) time, O(1) space)

The key observation: if a bit appears in the final OR, at least one remaining element must contain that bit. To minimize the OR, you try to eliminate higher bits first. Iterate bits from most significant to least significant and check whether it's possible to keep that bit 0 in the result. During the check, scan the array and greedily combine adjacent numbers using AND. Once the running AND removes the target bit, you form a segment. If the number of required merges stays within k, that bit can remain 0. Otherwise the bit must be set in the answer. This works because AND operations only remove bits, never introduce them. The technique relies heavily on properties of bit manipulation and a greedy feasibility check.

Approach 2: Optimize by Pairing Strategically (O(n log M) time, O(1) space)

Another way to think about the problem is grouping numbers into segments whose cumulative AND eliminates unwanted bits. While scanning the array, maintain a running AND value. If the current segment still contains a forbidden bit, extend the segment and keep applying AND with the next element. When the bit disappears, finalize the segment and start a new one. Each finalized segment corresponds to merges performed inside it. The greedy decision is to close a segment as soon as it becomes valid so you minimize the number of operations used. This approach effectively simulates optimal pairings and avoids exploring exponential combinations. The logic aligns with techniques commonly used in greedy algorithms and bitwise aggregation in array problems.

Recommended for interviews: The greedy bit elimination approach. Interviewers expect candidates to reason about bitwise behavior: AND only clears bits, OR accumulates bits. Demonstrating the feasibility check for each bit shows strong understanding of greedy strategies combined with bit manipulation. Brute reasoning about pair combinations shows intuition, but the bitwise greedy solution proves algorithmic maturity.

Approach 1: Greedy Approach based on AND and OR operations

This approach entails making strategic AND operations to gradually minimize the elements in the array. By iterating through the list and focusing on adjacent pairs, we can reduce the number of elements while minimizing alterations to the overall OR value of the list.

The strategy involves iterating through the list nums. If possible, i.e., if k>0, replace each element by the conjunction of itself and the next element. After these operations, compute the bitwise OR of the resulting array.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of elements in nums, as we iterate through the list once.
Space Complexity: O(1), because operations are performed in-place.

Try this approach in the editor →

Approach 2: Optimize by Pairing Strategically

This approach focuses on extensively using AND operations between strategically chosen pairs. The key idea is using operations on elements yielding the largest reduction in OR, thus minimizing the result.

Every possible AND sequence used maximizes bit cancellation over adjacent pairs to lower OR, scanning neighborhood proximity repeatedly for optimal combination.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * k), nested iteration controlled by k auto-limiting.
Space Complexity: O(1), absence of dynamically allocated accompanying supplemental storage.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Approach based on AND and OR operations

Time Complexity: O(n), where n is the number of elements in nums, as we iterate through the list once.
Space Complexity: O(1), because operations are performed in-place.

Optimize by Pairing Strategically

Time Complexity: O(n * k), nested iteration controlled by k auto-limiting.
Space Complexity: O(1), absence of dynamically allocated accompanying supplemental storage.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy Bit Elimination with Feasibility CheckO(n log M)O(1)Best general solution. Efficient for large arrays and typical interview constraints.
Strategic Pairing with Running AND SegmentsO(n log M)O(1)Useful for understanding how segment merges simulate optimal pair operations.

Video Solution

3022. Minimize OR of Remaining Elements Using Operations | Weekly Leetcode 382 • codingMohan • 2,698 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Minimize OR of Remaining Elements Using Operations easy or hard?
The problem is rated Hard because it requires combining greedy reasoning with bit manipulation and a feasibility check. Understanding that AND operations only remove bits while OR aggregates them is the key insight needed to design the optimal algorithm.
Minimize OR of Remaining Elements Using Operations Python/Java solution
Implement the greedy bit-check approach. Iterate bits from high to low, assume the bit is removable, then scan the array while building AND segments. If more than k merges are required, the bit must be present in the final OR. The same logic works in Python, Java, C++, C#, and JavaScript.
How to solve Minimize OR of Remaining Elements Using Operations in O(n)?
Pure O(n) is not typical because each bit of the result must be validated. However, the practical optimal solution is O(n log M). For each candidate bit, you simulate merges with a single pass through the array and track segment AND values to determine whether the bit can be removed.
What is the best approach for Minimize OR of Remaining Elements Using Operations?
The most effective approach uses greedy bit manipulation. Iterate bits from most significant to least significant and check if that bit can be eliminated from the final OR by merging adjacent elements using AND operations. A linear scan verifies whether the required merges stay within k. This produces the minimum OR value in O(n log M) time where M is the maximum element value.
Is Minimize OR of Remaining Elements Using Operations asked at Google/Amazon/Meta?
Problems combining greedy decisions with bit manipulation are common in interviews at companies like Google, Amazon, and Meta. Variants involving minimizing OR or maximizing AND frequently appear because they test understanding of bitwise properties and greedy feasibility checks.
What data structure is used in Minimize OR of Remaining Elements Using Operations?
The problem mainly relies on arrays and bitwise operations. No complex data structures are required. The algorithm scans the array while maintaining a running AND value and counting merges needed to satisfy the bit constraints.
What is the time complexity of Minimize OR of Remaining Elements Using Operations?
The optimal solution runs in O(n log M) time and O(1) space. The algorithm checks each bit of the result (up to ~30 for typical integer constraints) and performs a linear scan of the array to verify feasibility using greedy AND merging.

Ready to solve this problem?

Practice Minimize OR of Remaining Elements Using Operations with our built-in code editor and test cases.

Practice on FleetCode