We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
* Complete the 'bitwiseAnd' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER N
* 2. INTEGER K
*/
public static int bitwiseAnd(int n, int k) {
// Write your code here
int max = 0;
for (int i = 1; i <= n; i++) {
if (i == k-1)
continue;
int val = i&(k-1);
if (val > max && val < k)
max = val;
}
return max;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(bufferedReader.readLine().trim());
for (int tItr = 0; tItr < t; tItr++) {
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int count = Integer.parseInt(firstMultipleInput[0]);
int lim = Integer.parseInt(firstMultipleInput[1]);
int res = Result.bitwiseAnd(count, lim);
bufferedWriter.write(String.valueOf(res));
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bitwise AND
You are viewing a single comment's thread. Return to all comments →
class Result {
int max = 0; for (int i = 1; i <= n; i++) { if (i == k-1) continue; int val = i&(k-1); if (val > max && val < k) max = val; } return max; }
}
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
}