Open In App

Check if given four integers (or sides) make rectangle

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// A simple program to find if given 4
// values can represent 4 sides of rectangle
#include <iostream>
using namespace std;
 
// Function to check if the given
// integers value make a rectangle
bool isRectangle(int a, int b, int c, int d)
{
    // Square is also a rectangle
    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;
}
 
// Driver code
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




// A simple program to find if
// given 4 values can represent
// 4 sides of rectangle
class GFG {
 
    // Function to check if the given
    // integers value make a rectangle
    static boolean isRectangle(int a, int b, int c, int d)
    {
        // Square is also a rectangle
        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;
    }
 
    // Driver code
    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");
    }
}
 
// This code is contributed by prerna saini.


Python3




# A simple program to find if given 4
# values can represent 4 sides of rectangle
 
# Function to check if the given
# integers value make a rectangle
 
 
def isRectangle(a, b, c, d):
 
    # check all sides of rectangle combinations
    if (a == b and d == c) or (a == c and b == d) or (a == d and b == c):
        return True
    else:
        return False
 
 
# Driver code
a, b, c, d = 1, 2, 3, 4
print("Yes" if isRectangle(a, b, c, d) else "No")
 
 
# This code is contributed by Jatinder SIngh


C#




// A simple program to find if
// given 4 values can represent
// 4 sides of rectangle
using System;
 
class GFG {
 
    // Function to check if the given
    // integers value make a rectangle
    static bool isRectangle(int a, int b, int c, int d)
    {
        // Square is also a rectangle
        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;
    }
 
    // Driver code
    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");
    }
}
 
// This code is contributed by vt_m.


Javascript




<script>
 
// A simple program to find if
// given 4 values can represent
// 4 sides of rectangle
 
    // Function to check if the given
    // integers value make a rectangle
    function isRectangle(a, b, c, d)
    {
        // Square is also a rectangle
        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;
    }
 
// Driver code
 
        let a = 1, b = 2, c = 3, d = 4;
        if (isRectangle(a, b, c, d))
            document.write("Yes");
        else
            document.write("No");
            
</script>


Output

No





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); // sort in non-descending order
     
    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 {
    // Function to check if given sides form a rectangle
    public static boolean isRectangle(int a, int b, int c, int d) {
        // Create an array to store the sides and sort it in non-descending order
        int[] sides = {a, b, c, d};
        Arrays.sort(sides);
         
        // Check if the conditions for a rectangle are met
        if (sides[0] == sides[1] && sides[2] == sides[3] && sides[1] != sides[2]) {
            return true;
        } else {
            return false;
        }
    }
 
    public static void main(String[] args) {
        // Define the values for the sides of the quadrilateral
        int a = 1, b = 2, c = 3, d = 4;
         
        // Check if the sides form a rectangle and print the result
        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()  # Sort in non-descending order
     
    # Check if it forms a rectangle
    if sides[0] == sides[1] and sides[2] == sides[3] and sides[1] != sides[2]:
        return True
    else:
        return False
 
# Main function
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
{
    // Function to check if the given sides form a rectangle
    static bool IsRectangle(int a, int b, int c, int d)
    {
        int[] sides = { a, b, c, d };
        Array.Sort(sides); // Sort in non-decreasing order
 
        // Conditions to check if it's a rectangle
        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); // sort in non-descending order
 
  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
Previous
Next
Share your thoughts in the comments
Similar Reads