# P6235 - Editorial

**URL:** <https://discuss.codechef.com/t/p6235-editorial/125266>\
**Category:** editorial\
**Tags:** easy, editorial, sorting, bitwise-operation, start235\
**Created:** [April 22, 2026, 4:30pm UTC](https://discuss.codechef.com/t/p6235-editorial/125266 "2026-04-22T16:30:54Z")\
**Posts on this page:** 7\
**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:** [April 22, 2026, 4:30pm UTC](https://discuss.codechef.com/t/p6235-editorial/125266/1 "2026-04-22T16:30:54Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/P6235)  
[Contest: Division 1](https://www.codechef.com/START235A/problems/P6235)  
[Contest: Division 2](https://www.codechef.com/START235B/problems/P6235)  
[Contest: Division 3](https://www.codechef.com/START235C/problems/P6235)  
[Contest: Division 4](https://www.codechef.com/START235D/problems/P6235)

_ **Author:** _ [pols\_agyi\_pols](https://www.codechef.com/users/pols_agyi_pols)  
_ **Tester:** _ [kingmessi](https://www.codechef.com/users/kingmessi)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

Easy

# PREREQUISITES:

Sorting, prefix XORs

# PROBLEM:

You have an array A and an index P.  
You can modify A as follows:

- Choose an index i (1 \lt i \lt N)
- Then XOR both A\_{i-1} and A\_{i+1} by A\_i.

Find the minimum possible final value of A\_P.

# EXPLANATION:

The first course of action is, naturally, to figure out exactly how A\_P can be modified.

When dealing with operations of this kind (as in, modifying a small number of indices in close proximity), it’s often useful to work with **prefixes**.

So, let’s define X\_i = A\_1 \oplus A\_2 \oplus\ldots\oplus A\_i to be the i-th prefix XOR of A.  
We also define X\_0 = 0.

Let’s see how the array X changes when we apply the operation on index i.  
The only values of A that change are A\_{i-1} and A\_{i+1}.  
Let Y denote the new prefix XOR array.

For all 0 \le j \lt i-1, note that there are no changes to elements whatsoever.  
So we simply have Y\_j = X\_j.

For index i-1 itself, note that the only change comes from the value at index i-1, which has been XOR-ed by A\_i.  
So, Y\_{i-1} = X\_{i-1} \oplus A\_i.  
However, observe that this is exactly X\_i.  
Thus, we simply have Y\_{i-1} = X\_i.

For index i, again the only change is that A\_{i-1} has been XOR-ed by A\_i, because the value at index i didn’t change.  
So, Y\_i = X\_i \oplus A\_i.  
But X\_i = X\_{i-1} \oplus A\_i, so we obtain Y\_i = X\_{i-1} \oplus A\_i \oplus A\_i = X\_{i-1} because of the involutory property of bitwise XOR (i.e. x\oplus x= 0 for any x.)  
Thus, Y\_i = X\_{i-1}.

For indices j\ge i+1, the changes are that both A\_{i-1} and A\_{i+1} have been XOR-ed by A\_i.  
This means that we have Y\_j = X\_j \oplus A\_i \oplus A\_i = X\_j, i.e. the prefix XOR remains unchanged for these indices.

Observe that almost all the prefix XORs have remained unchanged.  
The only ones that changed are the ones corresponding to indices i-1 and i, and they simply swapped positions!

Thus, our operation really just allows us to do the following with the prefix XOR array X:

- Choose an index i (1 \lt i \lt N), and swap X\_i and X\_{i-1}.

* * *

Swapping any adjacent elements of an array is a powerful tool, and in fact allows us to rearrange the array however we like!  
Thus, using our operations, we can rearrange the elements [X\_1, X\_2, \ldots, X\_{N-1}] in whatever order we like.  
Note however that X\_0 = 0 is always fixed; and X\_N cannot be moved (because we cannot choose i = N in our operation.)

Let’s use this to solve the original problem of minimizing the value of A\_P.  
Note that by reversing the definition of prefix XORs, we see that A\_P = X\_P \oplus X\_{P-1}, so this is what we want to minimize.

To do this, we will require a bit of casework on the index P to account for the fixed values.

**Case 1:** P = 1  
We want to minimize X\_1 \oplus X\_0.  
However, X\_0 = 0 is fixed, so we really just want to minimize X\_1.  
Our operation allows us to rearrange [X\_1, \ldots, X\_{N-1}] however we like.  
So, the answer is simply \min(X\_1, \ldots, X\_{N-1}).

**Case 2: P = N**  
We want to minimize X\_N \oplus X\_{N-1}.  
However, X\_N is fixed, while the power of rearrangement allows us to freely bring any one of [X\_1, \ldots, X\_{N-1}] to index N-1.  
So, the answer here is the minimum value of X\_N \oplus X\_i across all 1 \le i \lt N.  
This can be easily found in linear time; just try all values of i.

**Case 3:** 1 \lt P \lt N.  
We want to minimize X\_P \oplus X\_{P-1}.  
Neither value is fixed, and recall that we can freely rearrange [X\_1, \ldots, X\_{N-1}].

So, in this case the answer is simply the **minimum XOR of some pair in [X\_1, \ldots, X\_{N-1}]**.

We now have a sub-problem: given a (multi)set of values, how to quickly compute the minimum XOR of some pair of them?

The solution to this is in fact quite simple: the optimal pair will always be some pair of adjacent elements in sorted order!  
That is, sort the values [X\_1, \ldots, X\_{N-1}] in ascending order, and then the answer will be the minimum of (X\_i \oplus X\_{i+1}) across all i.

> > **Proof**
> >
> > Clearly, sorting X in ascending order isn’t going to change the minimum pairwise XOR; so it doesn’t hurt to do this.  
> > We now have X\_1 \le\ldots\le X\_{N-1}.
> > 
> > Now consider three indices i \lt j \lt k.  
> > We’ll prove that \min(X\_i\oplus X\_j, X\_k\oplus X\_j) \le X\_i\oplus X\_k.
> > 
> > If X\_i = X\_k, then X\_j must also be equal to them, and all three pairwise XORs are just 0.  
> > So, our claim trivially holds.
> > 
> > Otherwise, X\_i \lt X\_k, so there exists a bit where they differ in binary representation.  
> > Let b be the _highest_ bit where they differ.  
> > Note that this means all bits higher than b are equal in X\_i and X\_k, i.e. this is their “longest common prefix”.  
> > Further, the highest set bit in X\_i \oplus X\_k is b.
> > 
> > Because X\_j lies between X\_i and X\_k, it will also share their prefix of bits above b.  
> > There are now two cases:
> > 
> > 1. X\_j has b set.  
> > In this case, X\_j \oplus X\_k won’t have b (or any higher bit) set, so that X\_j \oplus X\_k \lt 2^b \le X\_i \oplus X\_k.
> > 2. X\_j doesn’t have b set.  
> > In this case, X\_j \oplus X\_i won’t have b (or any higher bit) set, so that X\_j \oplus X\_i \lt 2^b \le X\_i \oplus X\_k.
> > 
> > So in either case, one of the “inner” XORs is strictly smaller than X\_i \oplus X\_k.  
> > This proves that the minimum XOR is attained by some adjacent pair, as claimed (since if not, we’d be able to find some inner pair with smaller XOR, contradicting minimality.)

This gives a solution in \mathcal{O}(N\log N) time.

Alternately, you can use a trie to achieve similar complexity; though it’s of course much more code.

# TIME COMPLEXITY:

\mathcal{O}(N\log N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n, p = map(int, input().split())
> a = list(map(int, input().split()))
> pref = [0]
> for x in a: pref.append(pref[-1] ^ x)
>     
> if p == 1:
> print(min(pref[1:n]))
> continue
> if p == n:
> print(min(pref[n] ^ pref[i] for i in range(1, n)))
> continue
>     
> s = sorted(pref[1:n])
> print(min(s[i] ^ s[i+1] for i in range(len(s)-1)))
> 
> ```

---

<div class="post-metadata">

**Author:** ![sathvik\_cc](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@sathvik\_cc](https://discuss.codechef.com/u/sathvik_cc)\
**Post date:** [April 23, 2026, 2:26am UTC](https://discuss.codechef.com/t/p6235-editorial/125266/3 "2026-04-23T02:26:32Z")

</div>

> [@sathvik\_cc](#):
>
> author

for j \>= i + 1

y[j] = y[j - 1] ^ a[j], so if we consider a case for j = i + 1

y[i + 1] = x[i - 1] ^ a[i + 1], I didn’t understood how we have y[j] = x[j] can you please explain ?

---

<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:** [April 23, 2026, 5:15am UTC](https://discuss.codechef.com/t/p6235-editorial/125266/4 "2026-04-23T05:15:49Z")

</div>

Don’t forget that index i+1 also gets changed by the operation, so the new value at that index is (A\_{i+1} \oplus A\_i).

This gives Y\_{i+1} = Y\_i \oplus (A\_{i+1} \oplus A\_i), and Y\_i = X\_{i-1} as shown earlier so

Y\_{i+1} = X\_{i-1} \oplus A\_i \oplus A\_{i+1} = X\_{i+1}

And once you have this equality at i+1, for j \gt i+1 it’s simply Y\_j = Y\_{j-1} \oplus A\_j = X\_{j-1} \oplus A\_j = X\_j.

---

<div class="post-metadata">

**Author:** ![sathvik\_cc](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@sathvik\_cc](https://discuss.codechef.com/u/sathvik_cc)\
**Post date:** [April 23, 2026, 9:33am UTC](https://discuss.codechef.com/t/p6235-editorial/125266/5 "2026-04-23T09:33:40Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![sathvik8008](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sathvik8008/32/340321_2.png) [@sathvik8008](https://discuss.codechef.com/u/sathvik8008)\
**Post date:** [April 23, 2026, 11:24am UTC](https://discuss.codechef.com/t/p6235-editorial/125266/6 "2026-04-23T11:24:53Z")

</div>

I really Loved the solution Idea and any problems that have similar idea please share ..

---

<div class="post-metadata">

**Author:** ![sathvik8008](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sathvik8008/32/340321_2.png) [@sathvik8008](https://discuss.codechef.com/u/sathvik8008)\
**Post date:** [April 23, 2026, 11:49am UTC](https://discuss.codechef.com/t/p6235-editorial/125266/7 "2026-04-23T11:49:46Z")

</div>

btw, is max(Xi​⊕Xj​,Xk​⊕Xj​)≤Xi​⊕Xk

also true i just want to verify something i am trying to proove ?

---

<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:** [April 29, 2026, 4:20am UTC](https://discuss.codechef.com/t/p6235-editorial/125266/8 "2026-04-29T04:20:01Z")

</div>

No, it’s not.

\max(1\oplus 2, 3\oplus 2) = \max(3, 1) = 3 whereas 1\oplus 3 = 2.
