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++ // C++ Program to find largest element from array #include <bits/stdc++.h> using namespace std; // Utility function to check number of // elements having set msb as of pattern 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; } // Function for finding maximum and value pair int largest(int arr[], int n) { // Create a vector of given array vector<int> v(arr, arr + n); // Insert INT_MAX and update n v.push_back(INT_MAX); n++; int res = 0; // Iterate over total of 30bits from // msb to lsb for (int bit = 31; bit >= 0; bit--) { // Find the count of element having set msb int count = checkBit(res | (1 << bit), v, n); // if count | 1 != 1 set particular // bit in result if ((count | 1) != 1) res |= (1 << bit); } return res; } // Driver Code int main() { int arr[] = { 4, 8, 6, 2 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Largest element = " << largest(arr, n); return 0; }
Java // Java Program to find largest element from array import java.util.Vector; import java.util.Arrays; class GfG { // Utility function to check number of // elements having set msb as of pattern 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; } // Function for finding maximum and value pair static int largest(int arr[], int n) { // Create a vector of given array Vector<Integer> v = new Vector<>(); for(Integer a:arr) v.add(a); // Insert INT_MAX and update n v.add(Integer.MAX_VALUE); n++; int res = 0; // Iterate over total of 30bits from // msb to lsb for (int bit = 31; bit >= 0; bit--) { // Find the count of element having set msb int count = checkBit(res | (1 << bit), v, n); // if count | 1 != 1 set particular // bit in result if ((count | 1) != 1) res |= (1 << bit); } return res; } // Driver Code public static void main(String[] args) { int arr[] = { 4, 8, 6, 2 }; int n = arr.length; System.out.println("Largest element = " + largest(arr, n)); } } /* This code contributed by PrinciRaj1992 */
Python3 # Python3 Program to find largest # element from array import math as mt # Utility function to check number of # elements having set msb as of pattern def checkBit(pattern, arr, n): count = 0 for i in range(n): if ((pattern & arr[i]) == pattern): count += 1 return count # Function for finding maximum # and value pair def largest(arr, n): # Create a vector of given array v = arr # Insert max value of Int and update n v.append(2**31 - 1) n = n + 1 res = 0 # Iterate over total of 30bits # from msb to lsb for bit in range(31, -1, -1): # Find the count of element # having set msb count = checkBit(res | (1 << bit), v, n) # if count | 1 != 1 set particular # bit in result if ((count | 1) != 1): res |= (1 << bit) return res # Driver Code arr = [4, 8, 6, 2] n = len(arr) print("Largest element =", largest(arr, n)) # This code is contributed by # Mohit kumar 29
C# // C# Program to find largest element from array using System; using System.Collections.Generic; class GfG { // Utility function to check number of // elements having set msb as of pattern 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; } // Function for finding maximum and value pair static int largest(int []arr, int n) { // Create a vector of given array List<int> v = new List<int>(); foreach(int a in arr) v.Add(a); // Insert INT_MAX and update n v.Add(int.MaxValue); n++; int res = 0; // Iterate over total of 30bits from // msb to lsb for (int bit = 31; bit >= 0; bit--) { // Find the count of element having set msb int count = checkBit(res | (1 << bit), v, n); // if count | 1 != 1 set particular // bit in result if ((count | 1) != 1) res |= (1 << bit); } return res; } // Driver Code public static void Main(String[] args) { int []arr = { 4, 8, 6, 2 }; int n = arr.Length; Console.WriteLine("Largest element = " + largest(arr, n)); } } // This code contributed by Rajput-Ji
PHP <?php // php Program to find largest // element from array // Utility function to check // number of elements having // set msb as of pattern function checkBit($pattern,$arr,$n) { $count = 0; for ($i = 0; $i < $n; $i++) if (($pattern & $arr[$i]) == $pattern) $count++; return $count; } // Function for finding // maximum and value pair function largest($arr, $n) { $res = 0; // Iterate over total of // 30bits from msb to lsb for ($bit = 31; $bit >= 0; $bit--) { // Find the count of element // having set msb $count = checkBit($res | (1 << $bit),$arr, $n); // if count | 1 != 1 set // particular bit in result if ($count | 1 != 1) $res |= (1 << $bit); } return $res; } // Driver code $arr = array( 4, 8, 6, 2 ); $n = sizeof($arr) / sizeof($arr[0]); echo "Largest element = ". largest($arr, $n); // This code is contributed by mits ?>
JavaScript <script> // javascript Program to find largest element from array // Utility function to check number of // elements having set msb as of pattern function checkBit( pattern, arr, n){ let count = 0; for (let i = 0; i < n; i++) if ((pattern & arr[i]) == pattern) count++; return count; } // Function for finding maximum and value pair function largest( arr, n){ // Create a vector of given array let v = []; for(let i = 0;i<n;i++){ v.push(arr[i]) } // Insert INT_MAX and update n v.push(Math.pow(2,31)-1); n++; let res = 0; // Iterate over total of 30bits from // msb to lsb for (let bit = 31; bit >= 0; bit--) { // Find the count of element having set msb let count = checkBit(res | (1 << bit), v, n); // if count | 1 != 1 set particular // bit in result 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)); // This code is contributed by rohitsingh07052. </script>
Output:
Largest element = 8
Time Complexity: O(32)
Auxiliary Space: O(N)
Similar Reads
Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element. Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[
15+ min read
Javascript Program for Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[]
4 min read
Searching Elements in an Array | Array Operations In this post, we will look into search operation in an Array, i.e., how to search an element in an Array, such as: Searching in an Unsorted Array using Linear SearchSearching in a Sorted Array using Linear SearchSearching in a Sorted Array using Binary SearchSearching in an Sorted Array using Fibona
15+ min read
Find elements which are present in first array and not in second Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array. Examples : Input : a[] = {1, 2, 3, 4, 5, 10}; b[] = {2, 3, 1, 0, 5};Output : 4 10 4 and 10 are present in first array, butnot in second array.Input : a[] = {4, 3, 5, 9, 11}; b[]
14 min read
Queries to search for an element in an array and modify the array based on given conditions Given an array arr[] consisting of N integers and an integer X, the task is to print the array after performing X queries denoted by an array operations[]. The task for each query is as follows: If the array contains the integer operations[i], reverse the subarray starting from the index at which op
9 min read