Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively

Given two integers N and M, where N straight lines are parallel to the X-axis and M straight lines are parallel to Y-axis, the task is to calculate the number of rectangles that can be formed by these lines.
Â
Examples:Â
Input: N = 3, M = 6Â
Output: 45Â
Explanation:Â
There are total 45 rectangles possible with 3 lines parallel to x axis and 6 lines parallel to y axis.
Input: N = 2, M = 4Â
Output: 6Â
Explanation:Â
There are total 6 rectangles possible with 2 lines parallel to x axis and 4 lines parallel to y axis.Â
Â
Â
Approach:Â
To solve the problem mentioned above we need to observe that a rectangle is formed by 4 straight lines in which opposite sides are parallel and the angle between any two sides is 90. Hence, for every rectangle, two sides need to be parallel to X-axis and the other two sides need to be parallel to Y-axis.Â
Â
- Number of ways to select two lines parallel to X axis = NC2 and the Number of ways to select two lines parallel to Y axis = MC2 .
- So the total number of rectangles  = NC2 * MC2 = [ N * (N – 1) / 2 ] * [ M * (M – 1) / 2 ]
Below is implementation of above approach:Â
Â
C++
// C++ Program to count number of// rectangles formed by N lines// parallel to X axis M lines// parallel to Y axis#include <bits/stdc++.h>using namespace std;Â
// Function to calculate// number of rectanglesint count_rectangles(int N, int M){    // Total number of ways to    // select two lines    // parallel to X axis    int p_x = (N * (N - 1)) / 2;Â
    // Total number of ways    // to select two lines    // parallel to Y axis    int p_y = (M * (M - 1)) / 2;Â
    // Total number of rectangles    return p_x * p_y;}Â
// Driver Programint main(){Â
    int N = 3;Â
    int M = 6;Â
    cout << count_rectangles(N, M);} |
Java
// Java Program to count number of// rectangles formed by N lines// parallel to X axis M lines// parallel to Y axisclass GFG{Â
// Function to calculate// number of rectanglesstatic int count_rectangles(int N, int M){    // Total number of ways to    // select two lines    // parallel to X axis    int p_x = (N * (N - 1)) / 2;Â
    // Total number of ways    // to select two lines    // parallel to Y axis    int p_y = (M * (M - 1)) / 2;Â
    // Total number of rectangles    return p_x * p_y;}Â
// Driver Programpublic static void main(String[] args){Â Â Â Â int N = 3;Â Â Â Â int M = 6;Â
    System.out.print(count_rectangles(N, M));}}Â
// This code is contributed by sapnasingh4991 |
Python3
# Python3 program to count number of rectangles# formed by N lines parallel to X axis# and M lines parallel to Y axisdef count_rectangles(N, M):Â
    # Total number of ways to select    # two lines parallel to X axis    p_x = (N * (N - 1)) // 2Â
    # Total number of ways to select    # two lines parallel to Y axis    p_y = (M * (M - 1)) // 2Â
    # Total number of rectangles    return p_x * p_yÂ
# Driver codeN = 3M = 6Â
print(count_rectangles(N, M))Â
# This code is contributed by himanshu77 |
C#
// C# Program to count number of// rectangles formed by N lines// parallel to X axis M lines// parallel to Y axisusing System;class GFG{Â
// Function to calculate// number of rectanglesstatic int count_rectangles(int N, int M){    // Total number of ways to    // select two lines    // parallel to X axis    int p_x = (N * (N - 1)) / 2;Â
    // Total number of ways    // to select two lines    // parallel to Y axis    int p_y = (M * (M - 1)) / 2;Â
    // Total number of rectangles    return p_x * p_y;}Â
// Driver Programpublic static void Main(){Â Â Â Â int N = 3;Â Â Â Â int M = 6;Â
    Console.Write(count_rectangles(N, M));}}Â
// This code is contributed by Code_mech |
Javascript
<script>Â
// JavaScript Program to count number of// rectangles formed by N lines// parallel to X axis M lines// parallel to Y axisÂ
// Function to calculate// number of rectanglesfunction count_rectangles(N, M){    // Total number of ways to    // select two lines    // parallel to X axis    let p_x = (N * (N - 1)) / 2;       // Total number of ways    // to select two lines    // parallel to Y axis    let p_y = (M * (M - 1)) / 2;       // Total number of rectangles    return p_x * p_y;}Â
// Driver CodeÂ
    let N = 3;    let M = 6;       document.write(count_rectangles(N, M));          </script> |
45
Â
Time Complexity: O(1), as constant operations are being performed.
Auxiliary Space: O(1), as constant space is being used.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



