# XORSUB7 - Editorial

**URL:** <https://discuss.codechef.com/t/xorsub7-editorial/124663>\
**Category:** editorial\
**Tags:** dynamic-programming, editorial, simple, start216\
**Created:** [December 10, 2025, 4:30pm UTC](https://discuss.codechef.com/t/xorsub7-editorial/124663 "2025-12-10T16:30:32Z")\
**Posts on this page:** 2\
**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:** [December 10, 2025, 4:30pm UTC](https://discuss.codechef.com/t/xorsub7-editorial/124663/1 "2025-12-10T16:30:32Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/XORSUB7)  
[Contest: Division 1](https://www.codechef.com/START216A/problems/XORSUB7)  
[Contest: Division 2](https://www.codechef.com/START216B/problems/XORSUB7)  
[Contest: Division 3](https://www.codechef.com/START216C/problems/XORSUB7)  
[Contest: Division 4](https://www.codechef.com/START216D/problems/XORSUB7)

_ **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:

Dynamic Programming

# PROBLEM:

An array B is said to be _good_ if B\_i \oplus B\_j = |B\_i - B\_j| for every pair of indices (i, j).

Given an array A of length N, find the length of its longest _good_ subsequence.

# EXPLANATION:

First, let’s understand when two integers X and Y can satisfy X\oplus Y = |X-Y|.

Specifically, let’s look at any single bit b.  
Without loss of generality, let X \ge Y, so that |X-Y| = X-Y.  
There are four options:

1. b is not set in the binary representations of both X and Y.  
Then, b will not be set in X\oplus Y, and also 2^b will not contribute to X-Y in any way.
2. b is set in the binary representations of both X and Y.  
Once again, b will not be set in X\oplus Y, and 2^b will not contribute to X-Y since X adds 2^b and Y subtracts 2^b.
3. b is set in X but not in Y.  
Here, b will be set in X\oplus Y, and also 2^b will be added to X-Y since X adds 2^b while Y subtracts nothing.  
Note that the contribution to both X\oplus Y and X-Y is still equal.
4. b is not set in X, but is set in Y.  
Here, b will be set in X\oplus Y, but 2^b will be **subtracted from** X-Y; since X adds 0 while Y subtracts 2^b.

From the above cases, we make the following observations:

1. For any bit b, its contribution to X\oplus Y is not smaller than its contribution to X-Y.  
In particular, this means X\oplus Y \ge X-Y always.
2. Next, the only case when the contributions are unequal are in the last case, i.e. a bit set in Y but not in X.

Thus, X\oplus Y = X-Y is possible only when no bit ever triggers the fourth case - that is, there must not exist _any_ bit that is set in Y but not in X.

Note that is equivalent to saying that every bit set in Y must be set in X, i.e. Y must be a submask of X.

* * *

With the above knowledge, it’s now quite easy to classify _good_ arrays.  
Since every pair of elements must have XOR equal to difference, this would mean that for every pair of elements, one must be a submask of the other.

An alternate way of stating this is that for every element, it must be a submask of every larger element.  
Because “is a submask of” is a transitive property, this is equivalent to just every element being a submask of its next larger element.

That is, we have the following simple method to check if an array B is _good_:

- Sort B in non-decreasing order.
- Then, check if B\_i is a submask of B\_{i+1} for each 1 \le i \lt |B|.  
Equivalently, the bitwise AND of B\_i and B\_{i+1} must equal B\_i.

With this criterion, it’s now quite easy to compute the longest good subsequence for the given array A.

Let’s first sort the array A, since that clearly doesn’t change the answer (and our easy check relies on sortedness).  
From now on, we assume A\_i \le A\_{i+1} for all 1 \le i\lt N.

Now, define dp\_i to be the maximum possible length of a good subsequence that includes A\_i.  
Since only adjacent elements need to be checked, we only care about the previous chosen element being a submask of A\_i.  
Thus, we obtain dp\_i = \max(1 + dp\_j) across all j such that A\_j is a submask of A\_i.

The answer is then \max(dp\_1, dp\_2, \ldots, dp\_N).

This gives a solution in \mathcal{O}(N^2) which is easily fast enough for N \le 100.

# TIME COMPLEXITY:

\mathcal{O}(N^2) per testcase.

# CODE:

[details = Editorialist’s code (PyPy3)]

```python
for _ in range(int(input())):
    n = int(input())
    a = sorted(list(map(int, input().split())))
    dp = [0]*n
    for i in range(n):
        for j in range(i):
            if a[i] & a[j] == a[j]: dp[i] = max(dp[i], dp[j])
        dp[i] += 1
    print(max(dp))

```

[/details]

---

<div class="post-metadata">

**Author:** ![aupo14](https://discuss.codechef.com/user_avatar/discuss.codechef.com/aupo14/32/208155_2.png) [@aupo14](https://discuss.codechef.com/u/aupo14)\
**Post date:** [December 11, 2025, 6:06am UTC](https://discuss.codechef.com/t/xorsub7-editorial/124663/2 "2025-12-11T06:06:38Z")

</div>

Can we do the dp part using take/ not -take approach with memoization?
