
- 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
Print pair with maximum AND value in an array in C Program.
According to the problem we are given an array of n positive integers, we have to find a pair with maximum AND value from the array.
Example
Input: arr[] = { 4, 8, 12, 16 } Output: pair = 8 12 The maximum and value= 8 Input:arr[] = { 4, 8, 16, 2 } Output: pair = No possible AND The maximum and value = 0
For finding Maximum AND value is similar to find out Maximum AND value in an array. Program must find out the pair of elements resulting in obtained AND value. For finding the elements, simply traverse the whole array and find the AND value of each element with the obtained maximum AND value (result) and if arr[i] & result == result, that means arr[i] is the element which will generate maximum AND value. Also, in the case if maximum AND value (result) is zero then we should print “Not possible” in that case.
Algorithm
int checkBit(int pattern, int arr[], int n) START STEP 1: DECLARE AND INITIALIZE count AS 0 STEP 2: LOOP FOR i = 0 AND i < n AND i++ IF (pattern & arr[i]) == pattern THEN, INCREMENT count BY 1 STEP 3: RETURN count STOP int maxAND(int arr[], int n) START STEP 1: DECLARE AND INITIALIZE res = 0 AND count STEP 2: LOOP FOR bit = 31 AND bit >= 0 AND bit-- count = GOTO FUNCTION checkBit(res | (1 << bit), arr,n) IF count >= 2 THEN, res |= (1 << bit); END IF IF res == 0 PRINT "no possible AND” ELSE PRINT "Pair with maximum AND= " count = 0; LOOP FOR int i = 0 AND i < n && count < 2 AND i++ IF (arr[i] & res) == res THEN, INCREMENT count BY 1 PRINT arr[i] END IF END FOR END FOR RETURN res STOP
Example
#include <stdio.h> int checkBit(int pattern, int arr[], int n){ int count = 0; for (int i = 0; i < n; i++) if ((pattern & arr[i]) == pattern) count++; return count; } // Function for finding maximum AND value pair int maxAND(int arr[], int n){ int res = 0, count; for (int bit = 31; bit >= 0; bit--) { count = checkBit(res | (1 << bit), arr, n); if (count >= 2) res |= (1 << bit); } if (res == 0) //if there is no pair available printf("no possible and
"); else { //Printing the pair available printf("Pair with maximum AND= "); count = 0; for (int i = 0; i < n && count < 2; i++) { // incremnent count value after // printing element if ((arr[i] & res) == res) { count++; printf("%d ", arr[i]); } } } return res; } int main(int argc, char const *argv[]){ int arr[] = {5, 6, 2, 8, 9, 12}; int n = sizeof(arr)/sizeof(arr[0]); int ma = maxAND(arr, n); printf("
The maximum AND value= %d ", ma); return 0; }
Output
If we run the above program then it will generate the following output −
pair = 8 9 The maximum and value= 8
- Related Articles
- Maximum bitwise AND value of a pair in an array in C++
- Find pair with maximum GCD in an array in C++
- Find a pair from the given array with maximum nCr value in C++
- Find a pair from the given array with maximum nCr value in Python
- Find a pair with maximum product in array of Integers in C++
- C Program to Minimum and Maximum prime numbers in an array
- Maximum sum of pairwise product in an array with negative allowed in C++ program
- Maximum product subset of an array in C++ program
- Swift Program to Find Maximum Key-Value Pair in the Dictionary
- Write a program in Go language to find the element with the maximum value in an array
- Maximum sum of smallest and second smallest in an array in C++ Program
- C# program to find maximum and minimum element in an array\n
- C++ Program to Find Closest Pair of Points in an Array
- Maximum value in an array after m range increment operations in C++
- C++ program to rearrange an array in maximum minimum form

Advertisements