-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitwiseAND.cpp
More file actions
42 lines (32 loc) · 1.14 KB
/
BitwiseAND.cpp
File metadata and controls
42 lines (32 loc) · 1.14 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
#include <iostream>
using namespace std;
// Function to find the maximum A & B value
int findMaxAND(int n, int k) {
int max_and = 0; // Initialize max_and to 0
// Iterate through all pairs of integers (A, B) from 1 to n
for (int A = 1; A <= n; A++) {
for (int B = A + 1; B <= n; B++) {
// Calculate the bitwise AND of A and B
int current_and = A & B;
// Check if the current_and value is less than K and greater than the current max_and
if (current_and < k && current_and > max_and) {
max_and = current_and; // Update max_and with the new maximum
}
}
}
// Return the maximum A & B value
return max_and;
}
int main() {
int t;
cin >> t; // Read the number of test cases
while (t--) { // Loop through each test case
int n, k;
cin >> n >> k; // Read the values of N and K for the current test case
// Call the findMaxAND function to get the maximum A & B value
int result = findMaxAND(n, k);
// Print the result for the current test case
cout << result << endl;
}
return 0;
}