#include <stdio.h>
void calculate_the_maximum(int n, int k) {
int max_and = 0, max_or = 0, max_xor = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
int and_result = i & j;
int or_result = i | j;
int xor_result = i ^ j;
if (and_result > max_and && and_result < k)
max_and = and_result;
if (or_result > max_or && or_result < k)
max_or = or_result;
if (xor_result > max_xor && xor_result < k)
max_xor = xor_result;
}
}
printf("%d\n%d\n%d\n", max_and, max_or, max_xor);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}
Let's go through the solution step by step:
We include the
We define a function called calculate_the_maximum that takes two integer parameters n and k. This function calculates the maximum values of bitwise AND, OR, and XOR operations.
Inside the calculate_the_maximum function, we declare three integer variables max_and, max_or, and max_xor to store the maximum values.
We use two nested for loops to iterate through all possible combinations of integers from 1 to n.
Within the nested loops, we perform the bitwise AND, OR, and XOR operations on each pair of integers i and j.
We compare the result of each operation with the current maximum value for the respective operation (max_and, max_or, max_xor).
If the result is greater than the current maximum value and less than k, we update the corresponding maximum value.
After iterating through all possible combinations, we use the printf() function to print the maximum values of bitwise AND, OR, and XOR operations.
In the main() function, we declare two integers n and k to store the input values.
We use the scanf() function to read input from the user and store it in the variables n and k.
We call the calculate_the_maximum function with the input values n and k.
By providing the expected input and executing the code, you should see the desired output according to the problem statement on HackerRank.