Open In App

Find maximum in an array without using Relational Operators

Given an array A[] of non-negative integers, find the maximum in the array without using Relational Operator.

Examples: 

Input : A[] = {2, 3, 1, 4, 5}
Output : 5

Input : A[] = {23, 17, 93}
Output : 93

We use repeated subtraction to find out the maximum. To find maximum between two numbers, we take a variable counter initialized to zero. We keep decreasing both the value till both of them becomes equal to zero (Note : The first value to become zero is no further decreased), increasing the counter simultaneously. While both the values becomes zero, the counter has increased to be the maximum of both of them. We first find the maximum of first two numbers and then compare it with the rest elements of the array one by one to find the overall maximum.

Below is the implementation of the above idea.

#include <iostream>
using namespace std;

// Function to find maximum between two non-negative
// numbers without using relational operator.
int maximum(int x, int y)
{
    int c = 0;

    // Continues till both becomes zero.
    while(x || y)
    {
        // decrement if the value is not already zero
        if(x)
        x--;

        if(y)
        y--;
        c++;
    }
    return c;
}

// Function to find maximum in an array.
int arrayMaximum(int A[], int N)
{
    // calculating maximum of first two numbers
    int mx = A[0];
    
    // Iterating through each of the member of the array 
    // to calculate the maximum
    for (int i = N-1; i; i--)

        // Finding the maximum between current maximum 
       // and current value.
        mx = maximum(mx, A[i]);
    
    return mx;
}

// Driver code
int main() 
{
    // Array declaration 
    int A[] = {4, 8, 9, 18};
    int N = sizeof(A) / sizeof(A[0]);
    
    // Calling Function to find the maximum of the Array
    cout << arrayMaximum(A, N);
    return 0;
} 
import java.io.*;

class GFG {
    
    // Function to find maximum between two 
    // non-negative numbers without using 
    // relational operator.
    static int maximum(int x, int y)
    {
        int c = 0;

        // Continues till both becomes zero.
        while (x > 0 || y > 0) {
            
            // decrement if the value is not 
            // already zero
            if (x > 0)
                x--;

            if (y > 0)
                y--;
            c++;
        }
        
        return c;
    }

    // Function to find maximum in an array.
    static int arrayMaximum(int A[], int N)
    {
        
        // calculating maximum of first 
        // two numbers
        int mx = A[0];

        // Iterating through each of the 
        // member of the array to calculate
        // the maximum
        for (int i = N - 1; i > 0; i--)

            // Finding the maximum between 
            // current maximum and current 
            // value.
            mx = maximum(mx, A[i]);

        return mx;
    }

    // Driver code
    public static void main(String[] args)
    {
        
        // Array declaration
        int A[] = { 4, 8, 9, 18 };
        int N = A.length;

        // Calling Function to find the maximum
        // of the Array
        System.out.print(arrayMaximum(A, N));
    }
}

// This code is contributed by vt_m.
# Function to find maximum between two 
# non-negative numbers without using 
# relational operator.
def maximum(x, y):
    c = 0

    # Continues till both becomes zero.
    while(x or y):
        
        # decrement if the value is 
        # not already zero
        if(x):
            x -= 1

        if(y):
            y -= 1
        c += 1

    return c

# Function to find maximum in an array.
def arrayMaximum(A, N):
    
    # calculating maximum of 
    # first two numbers
    mx = A[0]
    
    # Iterating through each of 
    # the member of the array 
    # to calculate the maximum
    i = N - 1
    while(i):
        
        # Finding the maximum between 
        # current maximum and current value.
        mx = maximum(mx, A[i])
        i -= 1
    
    return mx

# Driver code
if __name__ == '__main__':
    
    # Array declaration 
    A = [4, 8, 9, 18]
    N = len(A)
    
    # Calling Function to find the 
    # maximum of the Array
    print(arrayMaximum(A, N))
    
# This code is contributed by
# Surendra_Gangwar
// C# program to Find maximum
// in an array without using 
// Relational Operators
using System;

class GFG 
{
    
    // Function to find maximum 
    // between two non-negative
    // numbers without using 
    // relational operator.
    static int maximum(int x, 
                       int y)
    {
        int c = 0;

        // Continues till 
        // both becomes zero.
        while (x > 0 || y > 0) 
        {
            
            // decrement if 
            // the value is not 
            // already zero
            if (x > 0)
                x--;

            if (y > 0)
                y--;
            c++;
        }
        
        return c;
    }

    // Function to find 
    // maximum in an array.
    static int arrayMaximum(int []A, 
                            int N)
    {
        
        // calculating 
        // maximum of first 
        // two numbers
        int mx = A[0];

        // Iterating through 
        // each of the member
        // of the array to 
        // calculate the maximum
        for (int i = N - 1;
                 i > 0; i--)

            // Finding the maximum 
            // between current 
            // maximum and current 
            // value.
            mx = maximum(mx, A[i]);

        return mx;
    }

    // Driver code
    public static void Main()
    {
        
        // Array declaration
        int []A = { 4, 8, 9, 18 };
        int N = A.Length;

        // Calling Function to
        // find the maximum
        // of the Array
        Console.WriteLine(arrayMaximum(A, N));
    }
}

// This code is contributed
// by anuj_67.
    // Javascript program to Find maximum
    // in an array without using 
    // Relational Operators
    
    // Function to find maximum 
    // between two non-negative
    // numbers without using 
    // relational operator.
    function maximum(x, y)
    {
        let c = 0;
  
        // Continues till 
        // both becomes zero.
        while (x > 0 || y > 0) 
        {
              
            // decrement if 
            // the value is not 
            // already zero
            if (x > 0)
                x--;
  
            if (y > 0)
                y--;
            c++;
        }
          
        return c;
    }
  
    // Function to find 
    // maximum in an array.
    function arrayMaximum(A, N)
    {
          
        // calculating 
        // maximum of first 
        // two numbers
        let mx = A[0];
  
        // Iterating through 
        // each of the member
        // of the array to 
        // calculate the maximum
        for (let i = N - 1; i > 0; i--)
  
            // Finding the maximum 
            // between current 
            // maximum and current 
            // value.
            mx = maximum(mx, A[i]);
  
        return mx;
    }
    
    // Array declaration
    let A = [ 4, 8, 9, 18 ];
    let N = A.length;

    // Calling Function to
    // find the maximum
    // of the Array
    console.log(arrayMaximum(A, N));
    
    // This code is contributed by divyesh072019.
<?php
// Function to find maximum 
// between two non-negative
// numbers without using 
// relational operator.
function maximum($x, $y)
{
    $c = 0;

    // Continues till 
    // both becomes zero.
    while($x or $y)
    {
        // decrement if the value
        // is not already zero
        if($x)
        $x--;

        if($y)
        $y--;
        $c++;
    }
    return $c;
}

// Function to find 
// maximum in an array.
function arrayMaximum($A, $N)
{
    // calculating maximum of
    // first two numbers
    $mx = $A[0];
    
    // Iterating through each of 
    // the member of the array 
    // to calculate the maximum
    for ( $i = $N - 1; $i; $i--)

        // Finding the maximum
        // between current maximum 
        // and current value.
        $mx = maximum($mx, $A[$i]);
    
    return $mx;
}

// Driver code

// Array declaration 
$A = array(4, 8, 9, 18);
$N = count($A);

// Calling Function to find 
// the maximum of the Array
echo arrayMaximum($A, $N);

// This code is contributed
// by anuj_67.
?>

Output: 

18

Time complexity of the code will be O(N*max) where max is the maximum of the array elements.

Limitations : This will only work if the array contains all non negative integers.

Recursion Approach:

Follow the below steps:

Below is the implementation of the above approach: 

#include <iostream>
#include <vector>

using namespace std;

// Function to find the maximum element in an array
// recursively
int findMax(vector<int> arr, int n)
{
    // Base case
    if (n == 1) {
        return arr[0];
    }
    // Recursive case
    return max(arr[n - 1], findMax(arr, n - 1));
}

int main()
{
    // Example usage
    vector<int> arr = { 2, 3, 1, 4, 5 };

    // Call the findMax function with the array and its size
    cout << "Maximum element in the array: "
         << findMax(arr, arr.size()) << endl;

    return 0;
}
public class MaxElementRecursive {

    // Recursive function to find the maximum element in the
    // array
    public static int findMax(int[] arr, int n)
    {
        // Base case: if there is only one element in the
        // array
        if (n == 1) {
            return arr[0];
        }

        // Recursive case: find the maximum of the current
        // element and the maximum of the rest of the array
        return Math.max(arr[n - 1], findMax(arr, n - 1));
    }

    // Example usage
    public static void main(String[] args)
    {
        int[] arr = { 2, 3, 1, 4, 5 };

        // Call the findMax function with the array and its
        // size
        int maxElement = findMax(arr, arr.length);
        System.out.println("Maximum element in the array: "
                           + maxElement);
    }
}
# Define a recursive function to find the
# maximum element in the array


def find_max(arr, n):
    # Base case
    if n == 1:
        return arr[0]
    # Recursive case
    return max(arr[n-1], find_max(arr, n-1))


# Example usage
arr = [2, 3, 1, 4, 5]

# Call the find_max function with the array and its size
print(find_max(arr, len(arr)))
function findMax(arr, n) {
    // Base case
    if (n === 1) {
        return arr[0];
    }
    // Recursive case
    return Math.max(arr[n - 1], findMax(arr, n - 1));
}

// Example usage
const arr = [2, 3, 1, 4, 5];

// Call the findMax function with the array and its size
console.log(findMax(arr, arr.length));

Output
5

Time Complexity: O(n), where n is the size of the input array

Auxiliary Space: O(n)


Article Tags :