0% found this document useful (0 votes)
42 views6 pages

2 Programm

The document contains code to find the maximum XOR of any subarray in an array. It uses a trie data structure to store prefixes and their XOR values. It inserts all prefixes into the trie and finds the maximum XOR by querying the trie with each prefix XOR. The output shows the maximum XOR of the sample input array is 15.

Uploaded by

pathanlucifer227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views6 pages

2 Programm

The document contains code to find the maximum XOR of any subarray in an array. It uses a trie data structure to store prefixes and their XOR values. It inserts all prefixes into the trie and finds the maximum XOR by querying the trie with each prefix XOR. The output shows the maximum XOR of the sample input array is 15.

Uploaded by

pathanlucifer227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROBLEM NO:

AIM-:

Program: Power of 2 bit-difference


Algorithm:

Code:
public class PowerOf2BitDifference {

public static void main(String[] args) {

int limit = 10;

int[] powersOfTwo = generatePowersOfTwo(limit);

System.out.printf("%-15s %-20s %-20s %-20s %-15s%n", "Power of 2", "Binary", "Next Power of
2", "Binary", "Bit Differences");

System.out.println("-----------------------------------------------------------------------------------------------------
------");

for (int i = 0; i < powersOfTwo.length - 1; i++) {

int current = powersOfTwo[i];

int nextPower = powersOfTwo[i + 1];


int diff = bitDifference(current, nextPower)

System.out.printf("%-15d %-20s %-20d %-20s %-15d%n", current, toBinary(current), nextPower,


toBinary(nextPower), diff);

// Generate powers of 2 up to 2^limit

public static int[] generatePowersOfTwo(int limit) {

int[] powersOfTwo = new int[limit + 1];

for (int i = 0; i <= limit; i++) {

powersOfTwo[i] = (int) Math.pow(2, i);

return powersOfTwo;

// Convert an integer to its binary representation

public static String toBinary(int n) {

return Integer.toBinaryString(n);

public static int bitDifference(int a, int b) {

return Integer.bitCount(a ^ b);

Output:
PROBLEM NO:
AIM-:

Program: Finding Unique element in Array

Algorithm:

Code:
public class UniqueElement {

public static void main(String[] args) {

int[] arr = {2, 3, 5, 4, 5, 3, 4};

int unique = findUniqueElement(arr);

System.out.println("The unique element is: " + unique);

public static int findUniqueElement(int[] arr) {

int unique = 0;

for (int num : arr) {

unique ^= num;

}
return unique;

Output:
The unique element is: 2

Program: Finding the Maximum XOR Subarray


Algorithm:

Code:
import java.util.HashMap;

import java.util.Map;

class TrieNode {

Map<Integer, TrieNode> children = new HashMap<>();

public class MaximumXORSubarray {

public static void main(String[] args) {

int[] arr = {8, 1, 2, 12, 7, 6};

int maxXor = findMaximumXORSubarray(arr);

System.out.println("The maximum XOR of any subarray is: " + maxXor);

}
public static int findMaximumXORSubarray(int[] arr) {

TrieNode root = new TrieNode();

insert(root, 0);

int maxXor = 0, prefixXor = 0;

for (int num : arr) {

prefixXor ^= num;

insert(root, prefixXor);

maxXor = Math.max(maxXor, query(root, prefixXor));

return maxXor;

private static void insert(TrieNode root, int num) {

TrieNode node = root;

for (int i = 31; i >= 0; i--) {

int bit = (num >> i) & 1;

node.children.putIfAbsent(bit, new TrieNode());

node = node.children.get(bit);

private static int query(TrieNode root, int num) {

TrieNode node = root;

int maxXor = 0;

for (int i = 31; i >= 0; i--) {

int bit = (num >> i) & 1;

int toggleBit = 1 - bit; // Get the opposite bit to maximize XOR


if (node.children.containsKey(toggleBit)) {

maxXor = (maxXor << 1) | 1;

node = node.children.get(toggleBit);

} else {

maxXor = maxXor << 1;

node = node.children.get(bit);

return maxXor;

Output:
The maximum XOR of any subarray is: 15

You might also like