Header Ads Widget

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 [,].

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 [1,2,3,4] and you are given a query (2,3), then print the  of all the array  without the range [2,3].

Hence, the answer will be 1{XOR}4

Constraints

1105

1105

1109

1,

 

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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
class TestClass {
public static PrintWriter out = new PrintWriter(System.out);
// copied from codeforces
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
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 {
str = br.readLine();
} 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:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-


Post a Comment

0 Comments