Skip to content

Commit be19eee

Browse files
committed
up12
1 parent 499007f commit be19eee

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

  • JavaPractice/MaximumXORofTwoNumbersinanArray
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Maximum XOR of two numbers in an array
2+
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. [🔗Goto](https://practice.geeksforgeeks.org/problems/maximum-xor-of-two-numbers-in-an-array/1/?page=2&difficulty[]=1&status[]=unsolved&sortBy=accuracy#)
3+
4+
**Example 1:**
5+
6+
><p>Input:<br>
7+
>Arr = {25, 10, 2, 8, 5, 3}<br>
8+
>Output: 28<br>
9+
>Explanation:<br>
10+
>The maximum result is 5 ^ 25 = 28.
11+
</p>
12+
13+
<details>
14+
<summary>Full Code</summary>
15+
16+
```java
17+
import java.util.*;
18+
import java.lang.*;
19+
import java.io.*;
20+
class GFG
21+
{
22+
public static void main(String[] args) throws IOException
23+
{
24+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
25+
int T = Integer.parseInt(br.readLine().trim());
26+
while(T-->0)
27+
{
28+
int n = Integer.parseInt(br.readLine().trim());
29+
String s = br.readLine();
30+
String[] S = s.split(" ");
31+
int[] v = new int[n];
32+
for(int i = 0; i < n; i++)
33+
{
34+
v[i] = Integer.parseInt(S[i]);
35+
}
36+
Solution ob = new Solution();
37+
System.out.println(ob.max_xor(v, n));
38+
39+
}
40+
}
41+
}
42+
43+
```
44+
</details>
45+
46+
```java
47+
class Solution
48+
{
49+
public static int max_xor(int arr[], int n)
50+
{
51+
int maxXor = 0;
52+
for(int i=0; i<n; i++){
53+
for(int j=i; j<n; j++){
54+
int xor = arr[i]^arr[j];
55+
maxXor = Math.max(maxXor,xor);
56+
}
57+
}
58+
return maxXor;
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)