Home > Umum > Maximizing XOR

Maximizing XOR

Problem:

Given two integers: L and R, find the maximal values of A xor B given, L ≤ A ≤ B ≤ R

Java code:


import java.util.Scanner;

public class MaximizingXOR {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int l = in.nextInt();
int r = in.nextInt();
int max = 0;
for(int b=l; b<= r; b++) {
for(int a=b ; a <=r; a++) {
max = max > (a ^ b) ? max : (a ^ b);
}
}
System.out.println(max);
in.close();
}
}

Result for input 1 and 10:

maximizingXOR1

Result for input 10 and 15:

maximizingXOR2

Source:

https://www.hackerrank.com/challenges/maximizing-xor

Categories: Umum Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: