Find minimum in an array without using Relational Operators
Last Updated :
25 Jul, 2022
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++
// 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;
}
Java
// 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.
Python3
# 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.
C#
// 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.
PHP
<?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.
?>
JavaScript
<script>
// Javascript 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)
{
let 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.
function arrayMinimum(A, N)
{
// calculating minimum of
// first two numbers
let mn = A[0];
// Iterating through each of the
// member of the array to
// calculate the minimum
for (let i = N - 1; i > 0; i--)
// Finding the minimum between current
// minimum and current value.
mn = minimum(mn, A[i]);
return mn;
}
let A = [2, 3, 1, 4];
let N = A.length;
document.write(arrayMinimum(A, N));
// This code is contributed by divyesh072019.
</script>
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.
Similar Reads
Find the Target number in an Array Finding a number within an array is an operation, in the field of computer science and data analysis. In this article, we will discuss the steps involved and analyze their time and space complexities. Examples: Input: Array: {10, 20, 30, 40, 50} , Target: 30Output: "Target found at index 2" Input: A
13 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
Search, Insert, and Delete in an Unsorted Array | Array Operations In this post, a program to search, insert, and delete operations in an unsorted array is discussed.Search Operation:In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element. Coding implementation of the search operation:C++// C++ prog
15+ min read
Find pairs in array whose sums already exist in array Given an array of n distinct and positive elements, the task is to find pair whose sum already exists in the given array. Examples : Input : arr[] = {2, 8, 7, 1, 5};Output : 2 5 7 1 Input : arr[] = {7, 8, 5, 9, 11};Output : Not ExistA Naive Approach is to run three loops to find pair whose sum exist
9 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
Find all pairs (a,b) and (c,d) in array which satisfy ab = cd Given an array of distinct integers, the task is to find two pairs (a, b) and (c, d) such that ab = cd, where a, b, c and d are distinct elements. Examples: Input : arr[] = {3, 4, 7, 1, 2, 9, 8} Output : 4 2 and 1 8 Product of 4 and 2 is 8 and also product of 1 and 8 is 8 . Input : arr[] = {1, 6, 3,
7 min read
Find the only positive or only negative number in the given Array Given an array arr[] of either entirely positive integers or entirely negative integers except for one number. The task is to find that number. Examples: Input: arr[] = {3, 5, 2, 8, -7, 6, 9}Output: -7Explanation: Except -7 all the numbers in arr[] are positive integers. Input: arr[] = {-3, 5, -9}Ou
9 min read
Count pairs whose products exist in array Given an array, count those pair whose product value is present in array. Examples: Input : arr[] = {6, 2, 4, 12, 5, 3}Output : 3 All pairs whose product exist in array (6 , 2) (2, 3) (4, 3) Input : arr[] = {3, 5, 2, 4, 15, 8}Output : 2 A Simple solution is to generate all pairs of given array and c
15+ min read
Queries for bitwise AND in the index range [L, R] of the given array Given an array arr[] of N and Q queries consisting of a range [L, R]. the task is to find the bit-wise AND of all the elements of in that index range.Examples: Input: arr[] = {1, 3, 1, 2, 3, 4}, q[] = {{0, 1}, {3, 5}} Output: 1 0 1 AND 3 = 1 2 AND 3 AND 4 = 0Input: arr[] = {1, 2, 3, 4, 5}, q[] = {{0
8 min read
In what situation can we use binary search? Binary search is a powerful algorithm that can be used to find a target value within a sorted array. It works by repeatedly dividing the array in half until the target value is found or the array is empty. This makes binary search much faster than linear search, which must check every element in the
3 min read