
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Largest of two distinct numbers without using any conditional statements or operators
In this problem set, we will be given any two distinct positive numbers, let’s say a and b, we need to return the largest of two distinct numbers without using any conditional statements (if-else) or any operators(<,>,==,!=,etc.) in c++.
The main difficulty of the problem includes that we need to determine the largest of any two distinct positive numbers without using any operators or conditional statements.
For example,
INPUT: x=12, y=20
OUTPUT: 20
INPUT: x=3, y=2
OUTPUT: 3
Below is the algorithm that we will be using to solve this problem.
Algorithm
We will use type casting to solve this problem. Type casting includes the conversion of one data type into another data type. For example, we can convert int data type into long long data type using type casting or we can convert int data type into boolean data type using it.
Here in this problem we will use type casting of int into boolean to determine the largest of any two distinct positive integers. It can be better explained with the below expression −
$$\mathrm{x *\:(bool)\:(\frac{x}{y})\:+\:y\:*\:(bool)\:(\frac{y}{x})}$$
(data type) expression is the syntax for type casting where we can pass any data type we want to convert into.
The above equation will work as follow to give us the desired output −
The result of the value ${\frac{x}{y}}$ after type casting it to boolean is 1 if x>y and it is 0 if y>x.
The same goes for ${\frac{y}{x}}$ It will return 1 if y>x and 0 if x>y after type casting it to boolean.
In simple words, type casting any value from int data type into boolean data type will return 1 if int value is greater than or equal to1 and 0 if it is less than 1.
Hence the above expression will return x+0 or 0+y depending upon which one is greater x or y. Let’s understand this better with an example.
Let’s say we are given x=5 and y=3.
The above expression will be $\mathrm{5 *\:(bool)\:(\frac{5}{3})\:+\:3\:*\:(bool)\:(\frac{3}{5})}$
Since 5/3 is greater than 1 so type casting it into a boolean data type will give 1 and 3/5 is less than 1 so it will give 0 after type casting. There, the expression will become −
$$\mathrm{5 * 1 + 0, which\:is\:equal\:to\:5.}$$
5 is a greater number between 5 and 3. Hence, 5 is the desired output.
Approach
We will make a function to determine the larger number between the two.
Initialize a variable named num to store the value given by the expression, $\mathrm{x *\:(bool)\:(\frac{x}{y})\:+\:y\:*\:(bool)\:(\frac{y}{x})}$.
It will give either x or y whichever will be greater.
Return num which is our required output.
Example
Below is the C++ implementation of the above approach −
#include <stdio.h> #include<bits/stdc++.h> using namespace std; //function to get the larger number among two distinct positive numbers int larger(int x, int y){ //type casting to boolean data type int num= x * (bool) (x/y) + y * (bool) (y/x); //to store larger number return num; } //Driver Code int main(){ int x=28,y=20; cout<<larger(x,y)<<endl; x=8, y=13; cout<<larger(x,y)<<endl; return 0; }
Output
28 13
Time Complexity: O(1), since constant time is taken to perform the operation.
Space Complexity: O(1), since it uses no extra space.
Conclusion
In this article, we tried to solve the problem to determine the largest number among two distinct positive numbers without using any conditional statements or operators. We used the concept of type casting to boolean to get the required output using a particular expression.
I hope you find this article helpful and it cleared all your concepts regarding this problem.
- Related Articles
- Maximum of four numbers without using conditional or bitwise operator in C++
- Find largest element from array without using conditional operator in C++
- How do I add two numbers without using ++ or + or any other arithmetic operator in C/C++?
- C Program to find sum of two numbers without using any operator
- Java program to find largest of the three numbers using ternary operators
- C++ Program to Print “Even” or “Odd” without using conditional statement
- C Program to print “Even” or “Odd” without using Conditional statement
- Find HCF of two numbers without using recursion or Euclidean algorithm in C++
- Finding LCM of more than two (or array) numbers without using GCD in C++
- How to sum two integers without using arithmetic operators in C/C++?
- How to swap two numbers without using the third or a temporary variable using C Programming?
- Implement MySQL query using multiple OR statements. Any optimal alternative?
- Subtracting two numbers without using the (-) sign JavaScript
- Conditional statements in JavaScript
- What are Conditional Operators in JavaScript?
