Check if given four integers (or sides) make rectangle
Given four positive integers, determine if there’s a rectangle such that the lengths of its sides are a, b, c and d (in any order).
Examples :
Input : 1 1 2 2
Output : Yes
Input : 1 2 3 4
Output : No
Approach 1:- We will check, if any of the two integers are equal and make sure rest of two are also equal using few if else conditions.
C++
#include <iostream>
using namespace std;
bool isRectangle( int a, int b, int c, int d)
{
if (a == b == c == d)
return true ;
else if (a == b && c == d)
return true ;
else if (a == d && c == b)
return true ;
else if (a == c && d == b)
return true ;
else
return false ;
}
int main()
{
int a, b, c, d;
a = 1, b = 2, c = 3, d = 4;
if (isRectangle(a, b, c, d))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG {
static boolean isRectangle( int a, int b, int c, int d)
{
if (a == b && a == c && a == d && c == d && b == c
&& b == d)
return true ;
else if (a == b && c == d)
return true ;
else if (a == d && c == b)
return true ;
else if (a == c && d == b)
return true ;
else
return false ;
}
public static void main(String[] args)
{
int a = 1 , b = 2 , c = 3 , d = 4 ;
if (isRectangle(a, b, c, d))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isRectangle(a, b, c, d):
if (a = = b and d = = c) or (a = = c and b = = d) or (a = = d and b = = c):
return True
else :
return False
a, b, c, d = 1 , 2 , 3 , 4
print ( "Yes" if isRectangle(a, b, c, d) else "No" )
|
C#
using System;
class GFG {
static bool isRectangle( int a, int b, int c, int d)
{
if (a == b && a == c && a == d && c == d && b == c
&& b == d)
return true ;
else if (a == b && c == d)
return true ;
else if (a == d && c == b)
return true ;
else if (a == c && d == b)
return true ;
else
return false ;
}
public static void Main()
{
int a = 1, b = 2, c = 3, d = 4;
if (isRectangle(a, b, c, d))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isRectangle(a, b, c, d)
{
if (a == b && a == c &&
a == d && c == d &&
b == c && b == d)
return true ;
else if (a == b && c == d)
return true ;
else if (a == d && c == b)
return true ;
else if (a == c && d == b)
return true ;
else
return false ;
}
let a = 1, b = 2, c = 3, d = 4;
if (isRectangle(a, b, c, d))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(1) as constant operations are being performed
Auxiliary space: O(1)
Approach 2:
- Here’s another approach to check if the given 4 values can represent 4 sides of a rectangle:
- Sort the four values in non-descending order.
- Check if the first two values and the last two values are equal to each other.
- If step 2 is true, then the four values can represent the sides of a rectangle.
Here’s the updated code:
C++
#include <iostream>
#include <algorithm>
using namespace std;
bool isRectangle( int a, int b, int c, int d)
{
int sides[4] = {a, b, c, d};
sort(sides, sides + 4);
if (sides[0] == sides[1] && sides[2] == sides[3] && sides[1] != sides[2])
return true ;
else
return false ;
}
int main()
{
int a, b, c, d;
a = 1, b = 2, c = 3, d = 4;
if (isRectangle(a, b, c, d))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static boolean isRectangle( int a, int b, int c, int d) {
int [] sides = {a, b, c, d};
Arrays.sort(sides);
if (sides[ 0 ] == sides[ 1 ] && sides[ 2 ] == sides[ 3 ] && sides[ 1 ] != sides[ 2 ]) {
return true ;
} else {
return false ;
}
}
public static void main(String[] args) {
int a = 1 , b = 2 , c = 3 , d = 4 ;
if (isRectangle(a, b, c, d)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
def is_rectangle(a, b, c, d):
sides = [a, b, c, d]
sides.sort()
if sides[ 0 ] = = sides[ 1 ] and sides[ 2 ] = = sides[ 3 ] and sides[ 1 ] ! = sides[ 2 ]:
return True
else :
return False
if __name__ = = "__main__" :
a, b, c, d = 1 , 2 , 3 , 4
if is_rectangle(a, b, c, d):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class Program
{
static bool IsRectangle( int a, int b, int c, int d)
{
int [] sides = { a, b, c, d };
Array.Sort(sides);
if (sides[0] == sides[1] && sides[2] == sides[3] && sides[1] != sides[2])
return true ;
else
return false ;
}
static void Main()
{
int a, b, c, d;
a = 1; b = 2; c = 3; d = 4;
if (IsRectangle(a, b, c, d))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isRectangle(a, b, c, d) {
const sides = [a, b, c, d];
sides.sort((a, b) => a - b);
return (
sides[0] === sides[1] && sides[2] === sides[3] && sides[1] !== sides[2]
);
}
const a = 1,
b = 2,
c = 3,
d = 4;
if (isRectangle(a, b, c, d)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Output
No
Time complexity: O(1) as constant operations are being performed
Auxiliary space: O(1)
Last Updated :
07 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...