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.
C++
#include <iostream>
using namespace std;
int maximum( int x, int y)
{
int c = 0;
while (x || y)
{
if (x)
x--;
if (y)
y--;
c++;
}
return c;
}
int arrayMaximum( int A[], int N)
{
int mx = A[0];
for ( int i = N-1; i; i--)
mx = maximum(mx, A[i]);
return mx;
}
int main()
{
int A[] = {4, 8, 9, 18};
int N = sizeof (A) / sizeof (A[0]);
cout << arrayMaximum(A, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maximum( int x, int y)
{
int c = 0 ;
while (x > 0 || y > 0 ) {
if (x > 0 )
x--;
if (y > 0 )
y--;
c++;
}
return c;
}
static int arrayMaximum( int A[], int N)
{
int mx = A[ 0 ];
for ( int i = N - 1 ; i > 0 ; i--)
mx = maximum(mx, A[i]);
return mx;
}
public static void main(String[] args)
{
int A[] = { 4 , 8 , 9 , 18 };
int N = A.length;
System.out.print(arrayMaximum(A, N));
}
}
|
Python3
def maximum(x, y):
c = 0
while (x or y):
if (x):
x - = 1
if (y):
y - = 1
c + = 1
return c
def arrayMaximum(A, N):
mx = A[ 0 ]
i = N - 1
while (i):
mx = maximum(mx, A[i])
i - = 1
return mx
if __name__ = = '__main__' :
A = [ 4 , 8 , 9 , 18 ]
N = len (A)
print (arrayMaximum(A, N))
|
C#
using System;
class GFG
{
static int maximum( int x,
int y)
{
int c = 0;
while (x > 0 || y > 0)
{
if (x > 0)
x--;
if (y > 0)
y--;
c++;
}
return c;
}
static int arrayMaximum( int []A,
int N)
{
int mx = A[0];
for ( int i = N - 1;
i > 0; i--)
mx = maximum(mx, A[i]);
return mx;
}
public static void Main()
{
int []A = { 4, 8, 9, 18 };
int N = A.Length;
Console.WriteLine(arrayMaximum(A, N));
}
}
|
PHP
<?php
function maximum( $x , $y )
{
$c = 0;
while ( $x or $y )
{
if ( $x )
$x --;
if ( $y )
$y --;
$c ++;
}
return $c ;
}
function arrayMaximum( $A , $N )
{
$mx = $A [0];
for ( $i = $N - 1; $i ; $i --)
$mx = maximum( $mx , $A [ $i ]);
return $mx ;
}
$A = array (4, 8, 9, 18);
$N = count ( $A );
echo arrayMaximum( $A , $N );
?>
|
Javascript
<script>
function maximum(x, y)
{
let c = 0;
while (x > 0 || y > 0)
{
if (x > 0)
x--;
if (y > 0)
y--;
c++;
}
return c;
}
function arrayMaximum(A, N)
{
let mx = A[0];
for (let i = N - 1; i > 0; i--)
mx = maximum(mx, A[i]);
return mx;
}
let A = [ 4, 8, 9, 18 ];
let N = A.length;
document.write(arrayMaximum(A, N));
</script>
|
Output:
18
The 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.