# OROPS - Editorial

**URL:** <https://discuss.codechef.com/t/orops-editorial/125485>\
**Category:** editorial\
**Tags:** editorial, simple, start243\
**Created:** [June 17, 2026, 4:30pm UTC](https://discuss.codechef.com/t/orops-editorial/125485 "2026-06-17T16:30:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [June 17, 2026, 4:30pm UTC](https://discuss.codechef.com/t/orops-editorial/125485/1 "2026-06-17T16:30:16Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/OROPS)  
[Contest: Division 1](https://www.codechef.com/START243A/problems/OROPS)  
[Contest: Division 2](https://www.codechef.com/START243B/problems/OROPS)  
[Contest: Division 3](https://www.codechef.com/START243C/problems/OROPS)  
[Contest: Division 4](https://www.codechef.com/START243D/problems/OROPS)

_ **Author:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Tester:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

Simple

# PREREQUISITES:

None

# PROBLEM:

Given an array A, you can do the following:

- Choose an index i (1 \le i \lt |A|), delete A\_i and A\_{i+1}, and insert A\_i \mid A\_{i+1} in their place.  
Here, \mid denotes the bitwise OR operation.

Find the minimum number of operations needed to make all the elements of the array equal.

# EXPLANATION:

Since each operation deletes two elements but inserts their bitwise OR in their place, at any point of time, every remaining element will equal the bitwise OR of some subarray of the original array.

More formally, if we perform several operations and the current array is B = [B\_1, \ldots, B\_k], then there will exist k ranges [l\_1, r\_1], [l\_2, r\_2], \ldots, [l\_k, r\_k] such that:

- l\_1 = 1 and r\_k = N
- l\_i = r\_{i-1} + 1 for each 2 \le i \le k
- B\_i = A\_{l\_i} \mid \ldots\mid A\_{r\_i} for each 1 \le i \le k  
That is, B\_i equals the bitwise OR of the subarray from index l\_i to r\_i of A.

Further, observe that performing an operation doesn’t change the bitwise OR of the overall array; because we’re merging some elements together and that doesn’t lose any information.

So, if all elements have to be equal in the end, they all have to be equal to the bitwise OR of the array A - there’s no other choice!  
This is because x\mid x = x for any x, so if all the elements are equal then their bitwise OR also equals that element.

* * *

Let M = A\_1\mid A\_2\mid\ldots\mid A\_N denote the bitwise OR of the whole array.

We know that in the final array, we must have several copies of M.  
Each operation reduces the array length by 1, so minimizing the number of operations is equivalent to maximizing the number of copies of M we can create.

Let’s look at the leftmost element.  
It must be M, and it must come from a subarray starting at index 1, i.e. be of the form [1, i] for some i.

Let i\_1 be the _smallest_ index such that A\_1\mid\ldots\mid A\_{i\_1} = M.  
Note that any larger prefix than i\_1 will also have a bitwise OR of M.  
Now, it’s optimal to just take the subarray [1, i\_1] as our first subarray.

To see why, observe that if we don’t, then we’ve taken a larger prefix - say [1, i] where i \gt i\_1.  
But then we can just take [1, i\_1] instead and give the range [i\_1+1, i] to the second subarray instead - this won’t affect the bitwise OR of the second subarray since it’s already M anyway.

Thus, it’s optimal to simply cut out the shortest prefix we possibly can.  
This can then be repeated: starting from index i\_1+1, find the smallest index i\_2 such that the bitwise OR of the range [i\_1+1, i\_2] equals M, cut this subarray out and continue, and so on and so forth.

This greedy strategy can be easily implemented in \mathcal{O}(N) time by simply scanning through the array.

Note that in the end, you might end up with a small suffix of the array whose bitwise OR is \lt M.  
This is fine - this suffix can just be merged with the last cut subarray.

* * *

In the end, if you managed to cut out K subarrays, it means the final array will have K copies of M.  
Our initial length is N and final length is K, so that means we require N-K operations to reach this state.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n = int(input())
> a = list(map(int, input().split()))
>     
> target = 0
> for x in a: target |= x
>     
> ans = 0
> cur = 0
> for x in a:
> cur |= x
> if cur == target:
> ans += 1
> cur = 0
> print(n - ans)
> 
> ```
