
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find maximum in an array without using Relational Operators in C++
In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to find maximum in an array without using Relational Operators.
Let’s take an example to understand the problem,
Input: arr[] = {5, 1, 6, 7 , 8, 2}
Output: 8
Solution Approach
Since we need to compare values without using logical operators. For this we need to perform repeated subtraction, the number which will last longer will be the larger one.
We will decrement all values by one till they become zero. We will start with the first two values of the array and find the greatest of both. Then we need to compare the rest of array values with the largest element of the array. Using this we will find the maximum of all elements.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int returnMax(int x, int y) { int c = 0; while(x || y) { if(x) x--; if(y) y--; c++; } return c; } int findMaxEle(int A[], int N) { int maxVal = A[0]; for (int i = N-1; i; i--) maxVal = returnMax(maxVal, A[i]); return maxVal; } int main() { int A[] = {5, 1, 6, 7 , 8, 2}; int N = sizeof(A) / sizeof(A[0]); cout<<"The maximum element of the array is "<<findMaxEle(A, N); return 0; }
Output
The maximum element of the array is 8>
- Related Articles
- Find minimum in an array without using Relational Operators in C++
- Relational Operators on STL Array in C++
- Relational Operators in C++
- Comparing String objects using Relational Operators in C++
- Relational and comparison operators in C++
- What are relational operators in C#?
- Relational and Logical Operators in C
- Extended Operators in Relational Algebra in C++
- What are relational operators in C language?
- C++ Relational and Equality Operators
- Relational Set Operators in DBMS
- Basic Operators in Relational Algebra
- Relational Operators in Dart Programming
- Java Relational Operators
- C++ Program to Find Maximum Element in an Array using Binary Search

Advertisements