HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Finding the largest of a series of numbers being XORed

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thenumbersbeinglargestxoredfindingseries

Problem

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 

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  max) { 
                    max = res[i];
                }
            }
            printf ("%d\n", max);
        }
    }
    return 0;
}


I apologize for all the arbitrarily named variables. All of these problems

Solution

Putting all variables of the same type onto a single line can be bad for readability and maintenance:

unsigned int T,N,Q,i,a,p,q,count,max,n[100000],res[100000];


Since the single-character variables are similar, they can still be together. The rest of them, however, should be on separate lines. Also, since i is a loop counter, it's best to declare it as close as possible to its for loop instead.

unsigned int T, N, Q, a, p, q;
unsigned int count;
unsigned int max;
unsigned int n[100000]
unsigned int res[100000];

Code Snippets

unsigned int T,N,Q,i,a,p,q,count,max,n[100000],res[100000];
unsigned int T, N, Q, a, p, q;
unsigned int count;
unsigned int max;
unsigned int n[100000]
unsigned int res[100000];

Context

StackExchange Code Review Q#57954, answer score: 9

Revisions (0)

No revisions yet.