- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion

# C program to find out the maximum value of AND, OR, and XOR operations that are less than a given value

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)

## Example

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; }

## Input

5, 5

## Output

4 3 4

- Related Articles
- Program to find list of elements which are less than limit and XOR is maximum in Python
- C++ program to find out the maximum value of i
- Find a value whose XOR with given number is maximum in C++
- 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 Maximum XOR value of a sub-array of size k in C++
- Program to find out the value of a given equation in Python
- Maximum XOR value in matrix in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Mask an array where less than or equal to a given value in Numpy
- C# program to check if all the values in a list that are greater than a given value
- Maximum XOR value of a pair from a range in C++
- Program to find minimum possible maximum value after k operations in python
- C# Program to Find out the value of Sin(x)
- Program to find out the maximum value of a 'valid' array in Python

Advertisements