-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBitLogic.java
More file actions
113 lines (109 loc) · 2.23 KB
/
BitLogic.java
File metadata and controls
113 lines (109 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package assessmentDay;
//[10:19 AM, 3/24/2023] NfoTECH: Bit Logic
//
// For two positive integers, lo and hi, and a limit k, find two integers, a and b, satisfying the following criteria. Return the value of ae b. The @symbol denotes the bitwise XOR operator.
//
// Io <= a < b <= hi
//
// The value of a e bis maximal for a oplus b <= k .
//
// Example
//
// / O = 3
//
// hi = 5
//
// k = 6i
//
// a
//
// b
//
// 3
//
// 4
//
// aeb
//
// 7
//
// 3
//
// 5
//
// 6
//
// 4
//
// 5
//
// The maximal useable XORed value is 6 because it is the maximal value that is less than or equal to the limit k = 6
// [10:19 AM, 3/24/2023] NfoTECH: Function Description
//
// Complete the function maxxor in the editor below. The function must return an integer denoting the maximum possible value of a ob for all a @bsk
//
//ALL
//
// 2
//
// 3
//
// 4
//
// maxxor has the following parameter(s): int lo: an integer int hi: an integer
//
// k: an integer
//
// Constraints
//
// 1slo<his 10
//
// 1sks 104
//
// Input Format for Custom Testing
//
// ▾ Sample Case 0
//
// Sample Input 0
//
// STDIN
//
// 13 3
//
// Function
//
// lo 1
//
// k3
//
// Sample Output 0
public class BitLogic {
public static int maxXor (int lo, int hi, int k) {
int max = 0;
for (int i = lo; i <= hi; i++) {
for (int j = i; j <= hi; j++) {
int xor = i ^ j;
if (xor > max && xor <= k) {
max = xor;
}
}
}
return max;
}
public static int maxXor2(int lo, int hi, int k) {
int max = 0;
for (int a = lo; a <= hi; a++) {
for (int b = a + 1; b <= hi; b++) {
int xor = a ^ b;
if (xor <= k && xor > max) {
max = xor;
}
}
}
return max;
}
public static void main(String[] args) {
System.out.println(maxXor(3, 5, 6));
System.out.println(maxXor2(3, 5, 6));
}
}