Maximum OR value of a pair in an Array without using OR operator
Last Updated :
09 Nov, 2021
Given an array arr[] containing N positive integers, the task is to find the maximum bitwise OR value of a pair in the given array without using the Bitwise OR operator.
Examples:
Input: arr[] = {3, 6, 8, 16}
Output: 24
Explanation:
The pair giving maximum OR value is (8, 16) => 8|16 = 24
Input: arr[] = {8, 7, 3, 12}
Output: 15
Explanation:
There are more than one pair giving us the maximum OR value. One among them => 8|7 = 15
Approach: The idea is to find the two numbers which have the most count of set bits at distinct indices. In this way, the resultant number will have all those indices as a set bit, and this can be done without using the OR operator.
- Find out the maximum element in the array and then find the particular element in the remaining array that will have the set bit at the indexes where the maximum element has an unset bit.
- To maximize our output we have to find such an element that will have a set bit in such a manner that will maximize our output.
- Calculate the complement of the maximum element in the array and find the maximum AND value with the other numbers.
- The maximum AND value of this complement with other array elements will give us the maximum unset bits that could be set in our answer due to other array elements.
- Adding maximum elements with this maximum AND value will give us our desired maximum OR value pair without using OR operation.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum bitwise OR
// for any pair of the given array
// without using bitwise OR operation
int maxOR(int arr[], int n)
{
// find maximum element in the array
int max_value
= *max_element(arr, arr + n);
int number_of_bits
= floor(log2(max_value)) + 1;
// finding complement will set
// all unset bits in a number
int complement
= ((1 << number_of_bits) - 1)
^ max_value;
int c = 0;
// iterate through all other
// array elements to find
// maximum AND value
for (int i = 0; i < n; i++) {
if (arr[i] != max_value) {
c = max(c, (complement & arr[i]));
}
}
// c will give the maximum value
// that could be added to max_value
// to produce maximum OR value
return (max_value + c);
}
// Driver code
int main()
{
int arr[] = { 3, 6, 8, 16 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxOR(arr, n);
return 0;
}
Java
// Java implementation
// of the approach
import java.util.*;
class GFG{
// Function to return the maximum
// bitwise OR for any pair of the
// given array without using bitwise
// OR operation
static int maxOR(int arr[], int n)
{
// find maximum element in the array
int max_value =
Arrays.stream(arr).max().getAsInt();
int number_of_bits =
(int)((Math.log(max_value))) + 2;
// finding complement will set
// all unset bits in a number
int complement = ((1 << number_of_bits) - 1) ^
max_value;
int c = 0;
// iterate through all other
// array elements to find
// maximum AND value
for (int i = 0; i < n; i++)
{
if (arr[i] != max_value)
{
c = Math.max(c,
(complement & arr[i]));
}
}
// c will give the maximum value
// that could be added to max_value
// to produce maximum OR value
return (max_value + c);
}
// Driver code
public static void main(String[] args)
{
int arr[] = {3, 6, 8, 16};
int n = arr.length;
System.out.print(maxOR(arr, n));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 implementation of the approach
from math import log2, floor
# Function to return the maximum bitwise OR
# for any pair of the given array
# without using bitwise OR operation
def maxOR(arr, n):
# Find maximum element in the array
max_value = max(arr)
number_of_bits = floor(log2(max_value)) + 1
# Finding complement will set
# all unset bits in a number
complement = (((1 << number_of_bits) - 1) ^
max_value)
c = 0
# Iterate through all other
# array elements to find
# maximum AND value
for i in range(n):
if (arr[i] != max_value):
c = max(c, (complement & arr[i]))
# c will give the maximum value
# that could be added to max_value
# to produce maximum OR value
return (max_value + c)
# Driver code
if __name__ == '__main__':
arr = [3, 6, 8, 16]
n = len(arr)
print(maxOR(arr, n))
# This code is contributed by Bhupendra_Singh
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG{
// Function to return the maximum
// bitwise OR for any pair of the
// given array without using bitwise
// OR operation
static int maxOR(int[] arr, int n)
{
// Find maximum element in the array
int max_value = arr.Max();
int number_of_bits = (int)(Math.Log(max_value)) + 2;
// Finding complement will set
// all unset bits in a number
int complement = ((1 << number_of_bits) - 1) ^
max_value;
int c = 0;
// Iterate through all other
// array elements to find
// maximum AND value
for(int i = 0; i < n; i++)
{
if (arr[i] != max_value)
{
c = Math.Max(c,
(complement & arr[i]));
}
}
// c will give the maximum value
// that could be added to max_value
// to produce maximum OR value
return (max_value + c);
}
// Driver code
public static void Main()
{
int[] arr = { 3, 6, 8, 16 };
int n = arr.Length;
Console.Write(maxOR(arr, n));
}
}
// This code is contributed by code_hunt
JavaScript
<script>
// javascript implementation
// of the approach
// Function to return the maximum
// bitwise OR for any pair of the
// given array without using bitwise
// OR operation
function maxOR(arr , n) {
// find maximum element in the array
var max_value = Math.max.apply(Math,arr);
var number_of_bits = parseInt( ((Math.log(max_value)))) + 2;
// finding complement will set
// all unset bits in a number
var complement = ((1 << number_of_bits) - 1) ^ max_value;
var c = 0;
// iterate through all other
// array elements to find
// maximum AND value
for (i = 0; i < n; i++) {
if (arr[i] != max_value) {
c = Math.max(c, (complement & arr[i]));
}
}
// c will give the maximum value
// that could be added to max_value
// to produce maximum OR value
return (max_value + c);
}
// Driver code
var arr = [ 3, 6, 8, 16 ];
var n = arr.length;
document.write(maxOR(arr, n));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximum OR value of a pair in an Array | Set 2 Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.Examples: Input: arr[] = {3, 6, 8, 16} Output: 24 Explanation: The pair giving maximum OR value is (8, 16) 8|16 = 24Input: arr[] = {8, 7, 3, 12} Output: 15 Explanation: There
5 min read
Largest of two distinct numbers without using any conditional statements or operators Given two positive and distinct numbers, the task is to find the greatest of two given numbers without using any conditional statements(if...) and operators(?: in C/C++/Java).Examples: Input: a = 14, b = 15 Output: 15 Input: a = 14, b = 14 Output: 14 Input: a = 1233133, b = 124 Output: 1233133The Ap
3 min read
C++ Program for Number of pairs with maximum sum Given an array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i Example : Input : arr[] = {1, 1, 1, 2, 2, 2} Output : 3 Explanation: The maximum possible pair sum where i Method 1 (Naive)Â Traverse a loop i from 0 to n, i.e length of the array and another loop j
3 min read
Maximum of XOR of first and second maximum of all subarrays Given an array arr[] of distinct elements, the task is to find the maximum of XOR value of the first and second maximum elements of every possible subarray.Note: Length of the Array is greater than 1. Examples: Input: arr[] = {5, 4, 3} Output: 7 Explanation: All Possible subarrays with length greate
11 min read
C++ Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix
3 min read