# XOR of numbers | hackerearth solution

Problem

You are given an array $�$ of $�$ numbers and $�$ queries. In each query, $�$$�$ find $���$ of all $�$ numbers except the range $\left[�,�\right]$.

Input format

• The first line contains two integers $�$ denoting the number of elements and $�$ denoting the number of test cases.
• The second line of each test case contains $�$ space-separated integers.
• The next $�$ lines contain two integers $�$ and $�$.

Output format

For each test case, print the answer as described in the problem statement.

Example

Let the array $�$ be $\left[1,2,3,4\right]$ and you are given a query $\left(2,3\right)$, then print the $���$ of all the array $�$ without the range $\left[2,3\right]$.

Hence, the answer will be $1${XOR}$4$

Constraints

$1\le �\le {10}^{5}$

$1\le �\le {10}^{5}$

$1\le {�}_{�}\le {10}^{9}$

$1\le �,�\le �$

Sample Input
5 2
1 2 3 4 5
2 3
3 4

Sample Output
0
6
Time Limit: 1
Memory Limit: 256

Source Limit:

Code(C++):-

#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q; cin >> n >> q;
vector<int> a(n);
for(int i = 0; i < n; i ++) cin >> a[i];
vector<int> presum(n + 1, 0);
for(int i = 0; i < n; i ++) presum[i + 1] = presum[i] ^ a[i];
int l, r;
while(q --){
cin >> l >> r;
l --, r --;
cout << (presum.back() ^ presum[r + 1] ^ presum[l]) << '\n';
}
return 0;
}

Code(JAVA):-

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
class TestClass {
public static PrintWriter out = new PrintWriter(System.out);
// copied from codeforces
public static class MyScanner {
StringTokenizer st;
public MyScanner() {
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static final int MAX = 100_010;
static int[] A = new int[MAX];
static int[][] prefixSum = new int[MAX][32];
public static void main(String args[] ) throws Exception {
final var scanner = new MyScanner();
final int N = scanner.nextInt();
final int Q = scanner.nextInt();
for(int i = 1; i <= N; i++){
A[i] = scanner.nextInt();
for(int b = 0; b < 32; b++){
prefixSum[i][b] = prefixSum[i-1][b];
if((A[i] & (1 << b)) != 0){
prefixSum[i][b]++;
}
}
}
for(int q = 1; q <= Q; q++){
final int l = scanner.nextInt();
final int r = scanner.nextInt();
int xor = 0;
for(int b = 0; b < 32; b++){
if((prefixSum[N][b] - (prefixSum[r][b] - prefixSum[l-1][b])) % 2 != 0){
xor |= (1 << b);
}
}
out.println(xor);
}
out.flush();
}
}

### Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-