- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we are given two integers k and n. Our task is to perform three operations; bitwise AND, bitwise OR, and bitwise XOR between all pairs of numbers up to range n. We return the maximum value of all three operations between any two pairs of numbers that is less than the given value k.

So, if the input is like n = 5, k = 5, then the output will be 4 3 4.

The greatest value of AND, OR, and XOR operations between all pairs of numbers that are less than 5 are 4, 3, and 4 respectively. We can see that the values of these operations are less than that of the given value k, which is 5.

To solve this, we will follow these steps −

- andMax := 0, orMax = 0, xorMax = 0
- value1 := 0, value2 = 0, value3 = 0
- for initialize i := 1, when i <= n, update (increase i by 1), do:
- value1 := i AND j
- value2 := i OR j
- value3 := i XOR j
- if value1 > andMax and value1 < k, then −
- andMax := value1

- if value2 > orMax and value2 < k, then −
- orMax := value2

- if value3 > xorMax and value3 < k, then −
- xorMax := value3

- print(andMax, orMax, xorMax)

Let us see the following implementation to get better understanding −

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> void solve(int n, int k) { int andMax = 0, orMax = 0, xorMax = 0; int value1 = 0, value2 = 0, value3 = 0; for (int i = 1; i <= n; i++) { for (int j = i+1; j <= n; j++) { value1 = i & j; value2 = i | j; value3 = i ^ j; if (value1 > andMax && value1 < k) andMax = value1; if (value2 > orMax && value2 < k) orMax = value2; if (value3 > xorMax && value3 < k) xorMax = value3; } } printf("%d %d %d ", andMax, orMax, xorMax); } int main() { solve(5, 5); return 0; }

5, 5

4 3 4

- Related Questions & Answers
- Program to find list of elements which are less than limit and XOR is maximum in Python
- Python Program to find out the number of sets greater than a given value
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Find a value whose XOR with given number is maximum in C++
- Program to find out the value of a given equation in Python
- Count of alphabets having ASCII value less than and greater than k in C++
- Python - Check if all the values in a list are less than a given value
- C# program to check if all the values in a list that are greater than a given value
- Count elements less than or equal to a given value in a sorted rotated array in C++
- How to check whether a column value is less than or greater than a certain value in R?
- Find Maximum XOR value of a sub-array of size k in C++
- Find three integers less than or equal to N such that their LCM is maximum - C++
- Find maximum sum array of length less than or equal to m in C++
- Check if any value in an R vector is greater than or less than a certain value.
- Find MongoDB records with Price less than a specific value

Advertisements