from typing import *
def compareIfElse(a: int, b: int):
if a>b:
return 'greater'
elif a==b:
return 'equal'
else:
return 'smaller'
Problem of the day
Programming languages have some conditional / decision-making statements that execute when some specific condition is fulfilled.
If-else is one of the ways to implement them.
You are given two numbers 'a' and 'b'.
Compare the numbers and print the relation.
Print “smaller”, “greater” or “equal” when ‘a’ is smaller than ‘b’, greater than ‘b’, or equal to ‘b’ respectively.
Input: ‘a’ = 5 and ‘b’ = 3
Output: greater
Explanation: Since ‘a’ (= 5) is greater than ‘b’ (= 3), we are printing “greater”.
The first and only line of the input contains two space-separated integers, ‘a’ and ‘b’, the numbers you have to compare.
The first and only line of the output consists of a string representing the relation between the two integers.
You do not need to print anything; it has already been taken care of. Just implement the given function.
5 3
greater
Since ‘a’ (= 5) is greater than ‘b’ (= 3), we are printing “greater”.
2 2
equal
Since ‘a’ (= 2) is equal to ‘b’ (= 2), we are printing “equal”.
The expected time complexity is O(1).
-10 ^ 5 <= ‘a’ <= 10 ^ 5
-10 ^ 5 <= ‘b’ <= 10 ^ 5
Time limit: 1 second
The if-else conditional statements let us choose the statements we want to execute depending on some conditions.
We can check if ‘a’ is greater than, smaller than or equal to ‘b’ and print the valid relation.
The steps are as follows:
string compareIfElse(int ‘a’, int ‘b’)
O(1), that is constant time.
The comparison of two numbers is a constant time operation, and we are doing it 2 - 3 times.
Hence the time complexity is O(1).
O(1), that is constant space.
We are not using any auxiliary space.
Hence the space complexity is O(1).
Interview problems
using python
from typing import *
def compareIfElse(a: int, b: int):
if a>b:
return 'greater'
elif a==b:
return 'equal'
else:
return 'smaller'
Interview problems
code in C++ using ternary operators
//using ternary opertor
string compareIfElse(int a, int b) {
// Write your code here
return (a>b)?"greater":(a<b)?"smaller": "equal";
}
Interview problems
simple python solution
def compareIfElse(a: int, b: int)->str:
# Write your code here
if a>b:
return "greater"
elif a<b:
return "smaller"
return "equal"
Interview problems
code in C++ using ternary operators
string compareIfElse(int a, int b) {
// Write your code here
return (a>b)?"greater":(a<b)?"smaller":"equal";
}
Interview problems
if-else decision making
string compareIfElse(int a, int b) {
// Write your code here
if(a>b){
return "greater";
}
else if (a<b){
return "smaller";
}
else {
return "equal";
}
}
Interview problems
One Line answer
string compareIfElse(int a, int b) {
return a==b?"equal":(a>b?"greater":"smaller");
}
Interview problems
Using ternary operator
public class Solution {
public static String compareIfElse(int a, int b) {
// Write your code here
// if(a > b)
// return "greater";
// else if (a < b)
// return "smaller";
// else if ( a== b)
// return "equal";
// return "invalid";
String data = a > b ? "greater" : (a<b)? "smaller" : "equal";
return data;
}
}
Interview problems
easy question
string compareIfElse(int a, int b)
{
if (a < b) {
return "smaller";
}
else if (a > b) {
return "greater";
}
else{
return "equal";
}
}
Interview problems
easy solution
string compareIfElse(int a, int b) {
// Write your code here
if(a==b) return "equal";
else {
if (a < b )
return "smaller";
else
return "greater";
}
}
Interview problems
no input
I don't know why the code doesn't need input but just writing the code without input will make the code passfrom typing import *
def compareIfElse(a: int, b: int):
# Write your code here
if a > b:
return "greater"
if a < b:
return "smaller"
if a == b:
return "equal"
else:
return None