File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Given an integer array A of N integers, find the pair of integers
3
+ in the array which have minimum XOR value. Report the minimum XOR value.
4
+ Explanation: According to the problem, we are given the size of the array
5
+ and the numbers present in it. So we have to find two such numbers that are
6
+ the elements of the array and their xor is the minimum in value in comparison
7
+ to all the possible pairs of the array.
8
+ */
9
+
10
+ #include < bits/stdc++.h>
11
+ using namespace std ;
12
+
13
+ void Min_xor (int *A,int n)
14
+ {
15
+ // ele1 and ele2 will store the value of the elements that gives minimum xor value
16
+ int ele1,ele2;
17
+ // ans will store the minimum xor value
18
+ int ans=INT_MAX;
19
+ for (int i=0 ;i<n-1 ;i++)
20
+ {
21
+ // x will store xor value for each possible pair of elements of array
22
+ int x=A[i]^A[i+1 ];
23
+ if (x<ans)
24
+ {
25
+ ans=x;
26
+ ele1=A[i];
27
+ ele2=A[i+1 ];
28
+ }
29
+ }
30
+ // printing the output
31
+ cout<<" The Minimum xor value is : " <<ans<<endl;
32
+ cout<<" The corresponding elements are : " <<ele1<<" and " <<ele2;
33
+ }
34
+
35
+ int main () {
36
+ // n will store the size of the array
37
+ int n;
38
+ cout<<" Enter the size of array : " ;
39
+ cin>>n;
40
+ cout<<" Enter the array : " ;
41
+ int A[n];
42
+ for (int i=0 ;i<n;i++)
43
+ cin>>A[i];
44
+ // sort() will sort the array in increasing order
45
+ sort (A,A+n);
46
+ // calling the function
47
+ Min_xor (A,n);
48
+ return 0 ;
49
+ }
50
+
51
+ /*
52
+ INPUT:
53
+ Enter the size of array : 4
54
+ Enter the array : 0 2 5 7
55
+ OUTPUT:
56
+ The Minimum xor value is : 2
57
+ The corresponding elements are : 0 and 2
58
+
59
+ Time Complexity : O(nlogn)
60
+ Space Complexity : O(1)
61
+ */
You can’t perform that action at this time.
0 commit comments