Tuesday, 23 September 2014

Hackerrank Maximizing XOR


The Solution for hackerrank problem, Maximizing XOR using C Program.
Passed Test cases: 11 out of 11

SOURCE CODE:



 #include <stdio.h>  
 #include <string.h>  
 #include <math.h>  
 #include <stdlib.h>  
 #include <assert.h>  

 int maxXor(int l, int r) {  
   int answer = 0 ,i,j,temp=0;  
   for(i=l;i<=r;i++){  
     for(j=i;j<=r;j++){  
       temp = (i | j) & ~(i & j);  
     if(answer<temp)  
       answer=temp;  
     }  
   }  
   return answer;  
 }  
 int main() {  
   int res;  
   int _l;  
   scanf("%d", &_l);  
   int _r;  
   scanf("%d", &_r);  
   res = maxXor(_l, _r);  
   printf("%d\n", res);  
   return 0;  
 }  


2 comments:

  1. Brute force .. :o... could you pass more than 3 tests???

    ReplyDelete
    Replies
    1. yes ... ;)
      it passes all the test cases :)

      please comment an efficient approach for this program..

      Delete