VOOZH about

URL: https://www.geeksforgeeks.org/dsa/compare-two-integers-without-using-comparison-operator/

⇱ Compare two integers without using any Comparison operator - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Compare two integers without using any Comparison operator

Last Updated : 30 Jan, 2023

Given two integers A & B. Task is to check if A and B are same or not without using comparison operators.
Examples: 

Input : A = 5 , B = 6
Output : 0

Input : A = 5 , B = 5 
Output : 1

Note : 1 = "YES" and 0 = "NO"


 


The idea is pretty simple we do Xor of both elements ( A, B ). if Xor is zero then two numbers are equal else not. 

Below is the implementation of the above idea : 


Output
0

Time Complexity: O(1)
Auxiliary Space: O(1)

2nd Method:

Another Approach is using "-" (Minus/Subtraction Operator). If the Result of Subtraction between the two Numbers is "Zero", then they are equal (we return '1'), else they are not equal (we return '0').


Output
0

Time Complexity: O(1)
Auxiliary Space: O(1)


 

Comment
Article Tags: