Find largest element from array without using conditional operator
Last Updated :
19 Nov, 2021
Given an array of n-elements, we have to find the largest element among them without using any conditional operator like greater than or less than.
Examples:
Input : arr[] = {5, 7, 2, 9}
Output : Largest element = 9
Input : arr[] = {15, 0, 2, 15}
Output : Largest element = 15
First Approach (Use of Hashing) : To find the largest element from the array we may use the concept the of hashing where we should maintain a hash table of all element and then after processing all array element we should find the largest element in hash by simply traversing the hash table from end.
But there are some drawbacks of this approach like in case of very large elements maintaining a hash table is either not possible or not feasible.
Better Approach (Use of Bitwise AND) : Recently we have learn how to find the largest AND value pair from a given array. Also, we know that if we take bitwise AND of any number with INT_MAX (whose all bits are set bits) then the result will be that number itself. Now, using this property we will try to find the largest element from the array without any use conditional operator like greater than or less than.
For finding the largest element we will first insert an extra element i.e. INT_MAX in array, and after that we will try to find the maximum AND value of any pair from the array. This obtained maximum value will contain AND value of INT_MAX and largest element of original array and is our required result.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int checkBit( int pattern, vector< int > arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
int largest( int arr[], int n)
{
vector< int > v(arr, arr + n);
v.push_back(INT_MAX);
n++;
int res = 0;
for ( int bit = 31; bit >= 0; bit--)
{
int count = checkBit(res | (1 << bit), v, n);
if ((count | 1) != 1)
res |= (1 << bit);
}
return res;
}
int main()
{
int arr[] = { 4, 8, 6, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Largest element = " << largest(arr, n);
return 0;
}
|
Java
import java.util.Vector;
import java.util.Arrays;
class GfG
{
static int checkBit( int pattern, Vector<Integer> arr, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
if ((pattern & arr.get(i)) == pattern)
count++;
return count;
}
static int largest( int arr[], int n)
{
Vector<Integer> v = new Vector<>();
for (Integer a:arr)
v.add(a);
v.add(Integer.MAX_VALUE);
n++;
int res = 0 ;
for ( int bit = 31 ; bit >= 0 ; bit--)
{
int count = checkBit(res | ( 1 << bit), v, n);
if ((count | 1 ) != 1 )
res |= ( 1 << bit);
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 4 , 8 , 6 , 2 };
int n = arr.length;
System.out.println( "Largest element = " +
largest(arr, n));
}
}
|
Python3
import math as mt
def checkBit(pattern, arr, n):
count = 0
for i in range (n):
if ((pattern & arr[i]) = = pattern):
count + = 1
return count
def largest(arr, n):
v = arr
v.append( 2 * * 31 - 1 )
n = n + 1
res = 0
for bit in range ( 31 , - 1 , - 1 ):
count = checkBit(res | ( 1 << bit), v, n)
if ((count | 1 ) ! = 1 ):
res | = ( 1 << bit)
return res
arr = [ 4 , 8 , 6 , 2 ]
n = len (arr)
print ( "Largest element =" , largest(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static int checkBit( int pattern, List< int > arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
static int largest( int []arr, int n)
{
List< int > v = new List< int >();
foreach ( int a in arr)
v.Add(a);
v.Add( int .MaxValue);
n++;
int res = 0;
for ( int bit = 31; bit >= 0; bit--)
{
int count = checkBit(res | (1 << bit), v, n);
if ((count | 1) != 1)
res |= (1 << bit);
}
return res;
}
public static void Main(String[] args)
{
int []arr = { 4, 8, 6, 2 };
int n = arr.Length;
Console.WriteLine( "Largest element = " +
largest(arr, n));
}
}
|
PHP
<?php
function checkBit( $pattern , $arr , $n )
{
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
if (( $pattern & $arr [ $i ]) == $pattern )
$count ++;
return $count ;
}
function largest( $arr , $n )
{
$res = 0;
for ( $bit = 31; $bit >= 0; $bit --)
{
$count = checkBit( $res | (1 << $bit ), $arr , $n );
if ( $count | 1 != 1)
$res |= (1 << $bit );
}
return $res ;
}
$arr = array ( 4, 8, 6, 2 );
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo "Largest element = " . largest( $arr , $n );
?>
|
Javascript
<script>
function checkBit( pattern, arr, n){
let count = 0;
for (let i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
function largest( arr, n){
let v = [];
for (let i = 0;i<n;i++){
v.push(arr[i])
}
v.push(Math.pow(2,31)-1);
n++;
let res = 0;
for (let bit = 31; bit >= 0; bit--)
{
let count = checkBit(res | (1 << bit), v, n);
if ((count | 1) != 1)
res |= (1 << bit);
}
return res;
}
let a = [ 4, 8, 6, 2 ];
n = a.length;
document.write( "Largest element = " );
document.write(largest(a, n));
</script>
|
Output:
Largest element = 8
Time Complexity: O(32)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...