forked from int28h/JavaTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_Bitwise_AND.java
More file actions
33 lines (31 loc) · 961 Bytes
/
29_Bitwise_AND.java
File metadata and controls
33 lines (31 loc) · 961 Bytes
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
/**
* Given set S = {1, 2, 3, ..., N}. Find two integers, A and B (where A<B), from set S such that the value of A&B
* is the maximum possible and also less than a given integer, K. In this case, & represents the bitwise AND operator.
*/
import java.io.*;
import java.util.*;
public class Solution {
public static int findMaximum(int n, int k){
int max = 0;
int a = n - 1;
while(a-- > 0) {
for(int b = a + 1; b <= n; b++){
int test = a & b;
if(test < k && test > max){
max = test;
}
}
}
return max;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++){
int n = in.nextInt();
int k = in.nextInt();
System.out.println( findMaximum(n, k) );
}
in.close();
}
}