I'm working on solving a problem involving finding the largest of a series of numbers being XORed.
This is the actual problem:
Xorq has invented an encryption algorithm which uses bitwise XOR operations extensively. This encryption algorithm uses a sequence of non-negative integers \$x_1, x_2, ... x_n\$ as key. To implement this algorithm efficiently, Xorq needs to find maximum value for (\$a\$ xor \$x_j\$) for given integers \$a\$, \$p\$ and \$q\$ such that \$p<=j<=q\$. Help Xorq to implement this function.
Input
First line of input contains a single integer \$T\$ (\$1 <= T <= 6\$). T test cases follow.
First line of each test case contains two integers \$N\$ and \$Q\$ separated by a single space (\$1 <= N <= 100,000\$; \$1 <= Q <= 50,000\$). Next line contains \$N\$ integers \$x_1, x_2, ... x_n\$ separated by a single space (\$0 <= x_i < 2^{15}\$). Each of next Q lines describe a query which consists of three integers \$a_i\$, \$p_i\$ and \$q_i\$ (\$0 <= a_i < 2^{15}\$, \$1 <= p_i <= q_i <= N\$).
Output
For each query, print the maximum value for (\$a_i\$ xor \$x_j\$) such that \$p_i <= j <= q_i\$ in a single line.
I keep failing because of a timeout. What are some ways in which I could optimize my code to reduce time?
Is there a better way to do the bit manipulation? Should I be using a different data structure than an array? I've read a few articles on optimization and changed a few things, but nothing that made a real difference. Maybe I need to attempt the problem in a different fashion?
#include <stdio.h>
int main (int argc, char * argv[]) {
unsigned int T,N,Q,i,a,p,q,count,max,n[100000],res[100000];
scanf ("%d\n%d %d", &T, &N, &Q);
while (T--) {
for (i = 0; i < N; i++) {
scanf ("%d", &n[i]);
}
while (Q--) {
count = 0;
scanf ("%d %d %d", &a, &p, &q);
p--;
q--;
for (i = p; i <= q; i++) {
res[count++] = a ^ n[i];
}
max = res[0];
for (i = count; i--;) {
if (res[i] > max) {
max = res[i];
}
}
printf ("%d\n", max);
}
}
return 0;
}
I apologize for all the arbitrarily named variables. All of these problems just use single character names as their variables.
p<sub>i</sub><=j<=q<sub>i</sub>
means that you basically loop through the numbers from the second and third number on that line in you array of x elements and xor it with the first number on that line, then return the maximum result you get? \$\endgroup\$0 <= x <= 32,768
and there can be up toN=100000
of them. \$\endgroup\$