
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum in an Array Without Using Relational Operators in C++
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
#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
Advertisements