Find minimum in an array without using Relational Operators

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

Examples:

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

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



We use repeated subtraction to find out the minimum. To find minimum between two numbers, we take a variable counter initialized to zero. We keep decreasing the both the value till any one of them becomes equal to zero, increasing the counter simultaneously. The minimum value reaches zero first and the counter has increased to be the minimum of both of them. We first find the minimum of first two numbers and then compare it with the rest elements of the array one by one to find the overall minimum.

Below is the implementation of the above idea.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find minimum in an 
// array without using Relational Operators
#include <bits/stdc++.h>
using namespace std;
  
// Function to find minimum between two non-negative
// numbers without using relational operator.
int minimum(int x, int y)
{
    int c = 0;
  
    // Continues till any element becomes zero.
    while (x && y) 
    {
        x--;
        y--;
        c++;
    }
    return c;
}
  
// Function to find minimum in an array.
int arrayMinimum(int A[], int N)
{
    // calculating minimum of first two numbers
    int mn = A[0];
  
    // Iterating through each of the member of
    // the array to calculate the minimum
    for (int i = N-1; i; i--) 
  
        // Finding the minimum between current 
        // minimum and current value.
        mn = minimum(mn, A[i]);    
  
    return mn;
}
  
// Driver code
int main()
{
    int A[] = { 2, 3, 1, 4 };
    int N = sizeof(A) / sizeof(A[0]);
    cout << arrayMinimum(A, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find minimum in an 
// array without using Relational Operators
  
class GFG {
      
// Function to find minimum between two
// non-negative numbers without 
// using relational operator.
static int minimum(int x, int y)
{
    int c = 0;
  
    // Continues till any element becomes zero.
    while (x > 0 && y > 0) {
    x--;
    y--;
    c++;
    }
    return c;
}
  
// Function to find minimum in an array.
static int arrayMinimum(int A[], int N) {
      
    // calculating minimum of first two numbers
    int mn = A[0];
  
    // Iterating through each of the member of
    // the array to calculate the minimum
    for (int i = N - 1; i > 0; i--)
  
    // Finding the minimum between current
    // minimum and current value.
    mn = minimum(mn, A[i]);
  
    return mn;
}
  
// Driver code
public static void main(String arg[]) 
{
    int A[] = {2, 3, 1, 4};
    int N = A.length;
    System.out.print(arrayMinimum(A, N));
}
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to find minimum
# between two non-negative
# numbers without using
# relational operator.
  
def minimum(x,y):
    c = 0
   
    # Continues till any
    # element becomes zero.
    while (x>0 and y>0): 
      
        x=x-1
        y=y-1
        c=c+1
      
    return c
   
# Function to find
# minimum in an array.
def arrayMinimum(A,N):
  
    # calculating minimum
    # of first two numbers
    mn = A[0]
   
    # Iterating through each
    # of the member of
    # the array to calculate
    # the minimum
    for i in range(N-1,0,-1): 
   
        # Finding the minimum
        # between current 
        # minimum and current value.
        mn = minimum(mn, A[i])    
   
    return mn
      
# Driver code
  
A = [ 2, 3, 1, 4]
N =len(A)
  
print(arrayMinimum(A, N))
  
# This code is contributed
# by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find minimum in an 
// array without using Relational Operators
using System;
  
class GFG 
{
      
// Function to find minimum between two
// non-negative numbers without 
// using relational operator.
static int minimum(int x, int y)
{
    int c = 0;
  
    // Continues till any 
    // element becomes zero 
    while (x > 0 && y > 0)
    {
        x--;
        y--;
        c++;
    }
    return c;
}
  
// Function to find minimum in an array.
static int arrayMinimum(int []A, int N) 
{
      
    // calculating minimum of 
    // first two numbers
    int mn = A[0];
  
    // Iterating through each of the
    // member of the array to 
    // calculate the minimum
    for (int i = N - 1; i > 0; i--)
  
        // Finding the minimum between current
        // minimum and current value.
        mn = minimum(mn, A[i]);
  
    return mn;
}
  
// Driver code
public static void Main() 
{
    int []A = {2, 3, 1, 4};
    int N = A.Length;
    Console.WriteLine(arrayMinimum(A, N));
}
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum 
// in an array without using
// Relational Operators
  
// Function to find minimum 
// between two non-negative
// numbers without using
// relational operator.
function minimum($x, $y)
{
    $c = 0;
  
    // Continues till any
    // element becomes zero.
    while ($x and $y
    {
        $x--;
        $y--;
        $c++;
    }
    return $c;
}
  
// Function to find 
// minimum in an array.
function arrayMinimum( $A, $N)
{
    // calculating minimum of
    // first two numbers
    $mn = $A[0];
  
    // Iterating through each 
    // of the member of the 
    // array to calculate 
    // the minimum
    for ($i = $N - 1; $i; $i--) 
  
        // Finding the minimum 
        // between current minimum 
        // and current value.
        $mn = minimum($mn, $A[$i]); 
  
    return $mn;
}
  
// Driver code
$A = array(2, 3, 1, 4);
$N = count($A);
echo arrayMinimum($A, $N);
  
// This code is contributed 
// by anuj_67.
?>

chevron_right



Output:

1

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.