Java Day 29: Bitwise AND



Given set S={1, 2, 3,..., N}. Find two integers, A and B



  1. import java.util.Scanner;
  2. public class Solution {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. int T = in.nextInt();
  6. for (int i = 0; i < T; i++) {
  7. int N = in.nextInt();
  8. int K = in.nextInt();
  9. int max = 0;
  10. for (int j = 1; j < N; j++) {
  11. for (int k = j + 1; k <= N; k++) {
  12. int h = j & k;
  13. if (h < K && max < h) max = h;
  14. }
  15. }
  16. System.out.println(max);
  17. }
  18. }
  19. }