Find minimum in an array without using Relational Operators in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] consisting of n positive elements. Our task is to find minimum in an array without using Relational Operators.

Relational operators in programming are those operators which are used to check the relationship between two values. Like == (equals), greater than (>), less than (<), etc.

Let’s take an example to understand the problem,

Input

arr[] = {4, 2, 5, 1, 7}

Output

1

Explanation

The smallest element is 1.

Solution Approach

A simple way to solve the problem is using a loop and check for the minimum element out of all the elements of the array. For finding the minimum element between the two given elements, we can compare which element becomes 0 first when we decrease both by 1.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int findMin(int a, int b) {
   int minVal = 0;
   while (a && b) {
      minVal++;
      a--;
      b--;
   }
   return minVal;
}
int findMinimumElement(int arr[], int n) {
   int minVal = arr[0];
   int i = (n - 1) ;
   while(i){
      minVal = findMin(minVal, arr[i]);
      i--;
   }
   return minVal;
}
int main() {
   int arr[] = {4, 2, 5, 1, 7};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout<<"The minimum element is "<<findMinimumElement(arr, n);
   return 0;
}

Output

The minimum element is 1
raja
Updated on 12-Mar-2021 06:52:40

Advertisements