The Wayback Machine - https://web.archive.org/web/20241127002440/https://www.geeksforgeeks.org/number-of-parallelograms-when-n-horizontal-parallel-lines-intersect-m-vertical-parallellines/
Open In App

Number of parallelograms when n horizontal parallel lines intersect m vertical parallel lines

Last Updated : 12 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given two positive integers n and m. The task is to count number of parallelogram that can be formed of any size when n horizontal parallel lines intersect with m vertical parallel lines. 
 

Examples: 
 

Input : n = 3, m = 2
Output : 3
2 parallelograms of size 1x1 and 1 parallelogram
of size 2x1.
Input : n = 5, m = 5
Output : 100

 

The idea is to use Combination, which state, number of ways to choose k items from given n items is given by nCr. 
To form a parallelogram, we need two horizontal parallel lines and two vertical parallel lines. So, number of ways to choose two horizontal parallel lines are nC2 and number of ways to choose two vertical parallel lines are mC2. So, total number of possible parallelogram will be nC2 x mC2.
Below is C++ implementation of this approach: 
 

C++




// CPP Program to find number of parallelogram when
// n horizontal parallel lines intersect m vertical
// parallel lines.
#include<bits/stdc++.h>
#define MAX 10
using namespace std;
 
// Find value of Binomial Coefficient
int binomialCoeff(int C[][MAX], int n, int k)
{
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
  
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }
}
 
// Return number of parallelogram when n horizontal
// parallel lines intersect m vertical parallel lines.
int countParallelogram(int n, int m)
{
    int  C[MAX][MAX] = { 0 };   
    binomialCoeff(C, max(n, m), 2);   
    return C[n][2] * C[m][2];
}
 
// Driver Program
int main()
{
    int n = 5, m = 5;   
    cout << countParallelogram(n, m) << endl;
    return 0;
}


Java




// Java Program to find number of parallelogram when
// n horizontal parallel lines intersect m vertical
// parallel lines.
class GFG
{
    static final int MAX = 10;
     
    // Find value of Binomial Coefficient
    static void binomialCoeff(int C[][], int n, int k)
    {
        // Calculate value of Binomial Coefficient
        // in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= Math.min(i, k); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
     
                // Calculate value using previously
                // stored values
                else
                    C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
            }
        }
    }
     
    // Return number of parallelogram when n horizontal
    // parallel lines intersect m vertical parallel lines.
    static int countParallelogram(int n, int m)
    {
        int C[][]=new int[MAX][MAX];
         
        binomialCoeff(C, Math.max(n, m), 2);
         
        return C[n][2] * C[m][2];
    }
     
    // Driver code
    public static void main(String arg[])
    {
        int n = 5, m = 5;
        System.out.println(countParallelogram(n, m));
    }
}
 
// This code is contributed By Anant Agarwal.


Python3




# Python Program to find number of parallelogram when
# n horizontal parallel lines intersect m vertical
# parallel lines.
MAX = 10;
 
# Find value of Binomial Coefficient
def binomialCoeff(C, n, k):
     
    # Calculate value of Binomial Coefficient
    # in bottom up manner
    for i in range(n + 1):
        for j in range(0, min(i, k) + 1):
         
            # Base Cases
            if (j == 0 or j == i):
                C[i][j] = 1;
 
            # Calculate value using previously
            # stored values
            else:
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
 
# Return number of parallelogram when n horizontal
# parallel lines intersect m vertical parallel lines.
def countParallelogram(n, m):
    C = [[0 for i in range(MAX)] for j in range(MAX)]
 
    binomialCoeff(C, max(n, m), 2);
 
    return C[n][2] * C[m][2];
 
# Driver code
if __name__ == '__main__':
    n = 5;
    m = 5;
    print(countParallelogram(n, m));
 
# This code is contributed by 29AjayKumar


C#




// C# Program to find number of parallelogram when
// n horizontal parallel lines intersect m vertical
// parallel lines.
using System;
 
class GFG
{
    static int MAX = 10;
     
    // Find value of Binomial Coefficient
    static void binomialCoeff(int [,]C, int n, int k)
    {
        // Calculate value of Binomial Coefficient
        // in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= Math.Min(i, k); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i, j] = 1;
     
                // Calculate value using previously
                // stored values
                else
                    C[i, j] = C[i - 1, j - 1] + C[i - 1, j];
            }
        }
    }
     
    // Return number of parallelogram when n horizontal
    // parallel lines intersect m vertical parallel lines.
    static int countParallelogram(int n, int m)
    {
        int [,]C = new int[MAX, MAX];
         
        binomialCoeff(C, Math.Max(n, m), 2);
         
        return C[n, 2] * C[m, 2];
    }
     
    // Driver code
    public static void Main()
    {
        int n = 5, m = 5;
        Console.WriteLine(countParallelogram(n, m));
    }
}
 
// This code is contributed By vt_m.


Javascript




<script>
 
// Javascript Program to find number of parallelogram when
// n horizontal parallel lines intersect m vertical
// parallel lines.
var MAX = 10;
 
// Find value of Binomial Coefficient
function binomialCoeff(C, n, k)
{
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (var i = 0; i <= n; i++)
    {
        for (var j = 0; j <= Math.min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
  
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }
}
 
// Return number of parallelogram when n horizontal
// parallel lines intersect m vertical parallel lines.
function countParallelogram(n, m)
{
    var C = Array.from(Array(MAX), () => Array(MAX).fill(0));
    binomialCoeff(C, Math.max(n, m), 2);   
    return C[n][2] * C[m][2];
}
 
// Driver Program
var n = 5, m = 5;   
document.write( countParallelogram(n, m));
 
// This code is contributed by rdtank.
</script>


Output

100








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

Approach: Using Basic Maths

The same Question can be Solved By just using the basic maths
as we know nC2 = n*(n-1)/2 and same for mC2 so just using basic maths we can solve the question in O(1)

Below is the implementation of the above approach:

C++




#include <iostream>
 
class GFG {
public:
    static int findtheParallelogram(int n, int m)
    {
        // as nC2 = (n*(n-1))/2
        int result
            = ((n * (n - 1)) / 2) * ((m * (m - 1)) / 2);
 
        return result;
    }
};
 
int main()
{
    int n = 5;
    int m = 5;
    std::cout << GFG::findtheParallelogram(n, m)
              << std::endl;
    return 0;
}


Java




import java.io.*;
 
class GFG {
   public static int findtheParallelogram(int n, int m) {
     //as nC2 = (n*(n-1))/2
    int result = ((n * (n - 1)) / 2) * ((m * (m - 1)) / 2);
      
    return result;
  }
  //Driver code
  public static void main(String[] vars){
    int n = 5;
    int  m =5;
    System.out.println(findtheParallelogram(n,m));
  }
}


Python3




def find_the_parallelogram(n, m):
    # Calculate the number of parallelograms using the formula:
    # nC2 = (n * (n - 1)) / 2
    # mC2 = (m * (m - 1)) / 2
    result = ((n * (n - 1)) // 2) * ((m * (m - 1)) // 2)
    return result
 
# Driver code
if __name__ == "__main__":
    n = 5
    m = 5
    print(find_the_parallelogram(n, m))


C#




using System;
 
class GFG {
    // This method calculates the number of parallelograms
    // that can be formed given the dimensions n and m,
    // where n and m are the number of rows and columns.
    public static int FindTheParallelogram(int n, int m)
    {
        // Calculate nC2 = (n * (n - 1)) / 2
        // This formula represents the number of ways to
        // choose 2 items from n. For a parallelogram, we
        // need to choose 2 rows from n and 2 columns from
        // m.
        int result
            = ((n * (n - 1)) / 2) * ((m * (m - 1)) / 2);
 
        return result;
    }
}
 
class Program {
    static void Main(string[] args)
    {
        int n = 5;
        int m = 5;
 
        // Calculate and display the number of
        // parallelograms that can be formed with the given
        // dimensions n and m.
        Console.WriteLine("Number of Parallelograms: "
                          + GFG.FindTheParallelogram(n, m));
    }
}


Javascript




class GFG {
    static findtheParallelogram(n, m) {
        // Calculate the result using the formula
        // nC2 = (n * (n-1)) / 2
        const result = ((n * (n - 1)) / 2) * ((m * (m - 1)) / 2);
 
        return result;
    }
}
 
// Entry point
const n = 5;
const m = 5;
console.log(GFG.findtheParallelogram(n, m));


Output

100








Time Complexity :O(1)
Space Complexity : O(1)
This article is contributed by Aarti_Rathi and Dhruv Khoradiya.
 



Previous Article
Next Article

Similar Reads

Total number of triangles formed when there are H horizontal and V vertical lines
Given a triangle ABC. H horizontal lines from side AB to AC (as shown in fig.) and V vertical lines from vertex A to side BC are drawn, the task is to find the total no. of triangles formed.Examples: Input: H = 2, V = 2 Output: 18 As we see in the image above, total triangles formed are 18.Input: H = 3, V = 4 Output: 60 Approach: As we see in the i
5 min read
Check if N given lines can be intersected by K vertical lines
Given N horizontal lines represented by an array position[][] of size N, where position[i] represents the ith horizontal line which has x-coordinates from position[i][0] to position[i][1] and an integer K, which represents the maximum number of vertical lines that can be drawn, the task is to check if N given lines can be intersected by at most K v
6 min read
Number of horizontal or vertical line segments to connect 3 points
Given three points on the x-y coordinate plane. You need to find the no. of line segments formed by making a polyline passing through these points. (Line segment can be vertically or horizontally aligned to the coordinate axis) Examples : Input : A = {-1, -1}, B = {-1, 3}, C = {4, 3} Output : 2 Expantaion: There are two segments in this polyline. I
8 min read
Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps
Given a matrix mat[][] of dimensions N*M and a positive integer K, the task is to find the number of cells in a grid from which maximum cells can be reached by K jumps in the vertical or horizontal direction. Examples: Input: N = 3, M = 3, K = 2Output: 4Explanation:The cells represented as X are the cells from which maximum cells (= 2) can be reach
8 min read
Count number of triangles cut by the given horizontal and vertical line segments
Given an array triangles[][] consisting of N triangles of the form {x1, y1, x2, y2, x3, y3} and an array cuts[] consisting of M horizontal and vertical lines of the form "X=x" or "Y=y" which represents the equation of the line segments. The task is to print the number of triangles intersected by each cut in such a way that the left and the right pa
13 min read
Finding the number of triangles amongst horizontal and vertical line segments
Prerequisites: BIT Given 'n' line segments, each of them is either horizontal or vertical, find the maximum number of triangles(including triangles with zero area) that can be formed by joining the intersection points of the line segments. No two horizontal line segments overlap, nor do two vertical line segments. A line is represented using two po
6 min read
Maximum number of parallelograms that can be made using the given length of line segments
Given N line segments where the length of the ith line segment is [Tex]a_i [/Tex]. The task is to find the maximum number of parallelograms can be made with those line segments if each line segment is used at most in one parallelogram.Examples: Input: arr[] = {1, 2, 1, 2} Output: 1 Only one parallelogram can be made with sides 1, 2, 1, 2 Input: arr
6 min read
Vertical and Horizontal retrieval (MRT) on Tapes
Prerequisite: Optimal Storage on Tapes You are given tapes[] of different sizes and records[] of different volumes. The task is to fill the volumes in tapes horizontally or vertically such that Retrieval Time is minimised. Examples: Input: tape[] = { 25, 80, 160}, records[] = { 15, 2, 8, 23, 45, 50, 60, 120 } Output: Horizontal Filling: 1st tape :
15+ min read
Count ways to remove pairs from a matrix such that remaining elements can be grouped in vertical or horizontal pairs
Given an integer K and a square matrix mat[][] of size N * N with elements from the range[1, K], the task is to count ways to remove pairs of distinct matrix elements such that remaining elements can be arranged in pairs vertically or horizontally. Examples: Input: mat[][] = {{1, 2}, {3, 4}}, K = 4Output: 4Explanation:Remove the row {1, 2}. Therefo
10 min read
Maximum area of a Cake after Horizontal and Vertical cuts
Given two positive integers h and w representing the height h and width w which forms a rectangle. Also, there are two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangle to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangle to the
7 min read
Minimize cost to reach a cell in Matrix using horizontal, vertical and diagonal moves
Given two points P1(x1, y1) and P2(x2, y2) of a matrix, the task is to find the minimum cost to reach P2 from P1 when: A horizontal or vertical movement in any direction cost 1 unitA diagonal movement in any direction costs 0 unit. Examples: Input: P1 = {7, 4}, P2 = {4, 4}Output: 1Explanation: The movements are given below given below: Movements ar
5 min read
Largest area possible after removal of a series of horizontal & vertical bars
Given a grid consisting of horizontal & vertical bars of size (N + 2) x (M + 2) and two arrays H[] and V[] denoting the number of horizontal & vertical bars required to be removed, the task is to find the largest area when a series of vertical and horizontal bars are removed. Examples: Input: N = 3, M = 3, H[] = {2}, V[] = {2}Output: 4Expla
10 min read
Check horizontal and vertical symmetry in binary matrix
Given a 2D binary matrix of N rows and M columns. The task is to check whether the matrix is horizontal symmetric, vertical symmetric, or both. The matrix is said to be horizontal symmetric if the first row is the same as the last row, the second row is the same as the second last row, and so on. And the matrix is said to be vertical symmetric if t
10 min read
Javascript Program to Check horizontal and vertical symmetry in binary matrix
Given a 2D binary matrix of N rows and M columns. The task is to check whether the matrix is horizontal symmetric, vertical symmetric, or both. The matrix is said to be horizontal symmetric if the first row is the same as the last row, the second row is the same as the second last row, and so on. And the matrix is said to be vertical symmetric if t
3 min read
Class 9 RD Sharma Solutions - Chapter 15 Areas of Parallelograms and Triangles- Exercise 15.3 | Set 1
Question 1. In the figure, compute the area of quadrilateral ABCD. Solution: According to the question DC = 17 cm, AD = 9 cm and BC = 8 cm Find: the area of quadrilateral ABCD In ΔBCD, Using Pythagoras Theorem CD2 = BD2 + BC2 172 = BD2 + 82 BD2 = 289 − 64 BD = 15 So, the area of ΔBCD = 1/2(8 × 17) = 68 In ΔABD Using Pythagoras Theorem AB2 + AD2 = B
7 min read
Class 9 RD Sharma Solutions - Chapter 15 Areas of Parallelograms and Triangles- Exercise 15.3 | Set 2
Question11. If P is any point in the interior of a parallelogram ABCD, then prove that area of the triangle APB is less than half the area of parallelogram. Solution: According to the question ABCD is a parallelogram and P is any point in the interior of parallelogram Prove: ar(ΔAPB) < 1/2 ar(|| gm ABCD) Proof: Construction: Draw DN ⊥ AB and PM
10 min read
Class 9 RD Sharma Solutions - Chapter 15 Areas of Parallelograms and Triangles- Exercise 15.3 | Set 3
Question 21. In figure, PSDA is a parallelogram in which PQ = QR = RS and AP || BQ || CR. Prove that ar (ΔPQE) = ar(ΔCFD). Solution: According to the question PSDA is a parallelogram So, AP || BQ || CR || DS and AD || PS PQ = CD .....(i) Prove that (ΔPQE) = ar(ΔCFD) Proof: In ΔBED, C is the midpoint of BD and CF || BE So, F is the midpoint of ED He
10 min read
Count of parallelograms in a plane
Given some points on a plane, which are distinct and no three of them lie on the same line. We need to find number of Parallelograms with the vertices as the given points. Examples: Input : points[] = {(0, 0), (0, 2), (2, 2), (4, 2), (1, 4), (3, 4)} Output : 2 Two Parallelograms are possible by choosing above given point as vertices, which are show
7 min read
Number of lines from given N points not parallel to X or Y axis
Given N distinct integers points on 2D Plane. The task is to count the number of lines which are formed from given N points and not parallel to X or Y-axis. Examples: Input: points[][] = {{1, 2}, {1, 5}, {1, 15}, {2, 10}} Output: 3 Chosen pairs are {(1, 2), (2, 10)}, {(1, 5), (2, 10)}, {(1, 15), (2, 10)}. Input: points[][] = {{1, 2}, {2, 5}, {3, 15
9 min read
Parallel Dijkstra's Algorithm: SSSP in Parallel
The Parallel computing has become essential for solving computationally intensive problems efficiently. Dijkstra's algorithm is a well-known graph algorithm used to find the shortest paths from a single source vertex to all other vertices in a graph. When dealing with large graphs and it becomes necessary to parallelize the algorithm to achieve fas
12 min read
Maximum number of intervals that an interval can intersect
Given an array arr[] consisting of N intervals of the form of [L, R], where L, R denotes the start and end positions of the interval, the task is to count the maximum number of intervals that an interval can intersect with each other. Examples: Input: arr[] = {{1, 2}, {3, 4}, {2, 5}}Output: 3Explanation: The required set of intervals are {1, 2}, {2
15+ min read
Find whether only two parallel lines contain all coordinates points or not
Given an array that represents the y coordinates of a set of points on a coordinate plane, where (i, arr[i]) represent a single point. Find whether it is possible to draw a pair of parallel lines that includes all the coordinate points given and also both lines must contain a point. Print 1 for possible and 0 if not possible. Examples: Input: arr[]
7 min read
Distance between two parallel lines
Given are two parallel straight lines with slope m, and different y-intercepts b1 & b2.The task is to find the distance between these two parallel lines.Examples: Input: m = 2, b1 = 4, b2 = 3 Output: 0.333333 Input: m = -4, b1 = 11, b2 = 23 Output: 0.8 Approach: Let PQ and RS be the parallel lines, with equations y = mx + b1 y = mx + b2 The dis
4 min read
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 paral
5 min read
Count squares possible from M and N straight lines parallel to X and Y axis respectively
Given two arrays X[] and Y[] consisting of N and M integers such that there are N lines parallel to the y-axis and M lines parallel to the x-axis. The task is to find the total number of squares formed by these lines on a coordinate plane. Each integer(say a) in the array X[] denotes lines having equation x = a, parallel to y-axis. Each integer(say
9 min read
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
Given a 2D array rectangle[][] representing vertices of a rectangle {(0, 0), (L, 0), (0, B), (L, B)} of dimensions L * B, and another 2D-array, points[][] of size N in a Cartesian coordinate system. Draw a horizontal line parallel to the X-axis and a vertical line parallel to the Y-axis from each point of the given array. The task is to count all p
6 min read
Count squares of unique dimensions possible from given Straight Lines parallel to the axes
Given two arrays X[] and Y[] consisting of N and M integers such that there are N lines parallel to the y-axis and M lines parallel to the x-axis, the task is to find the total number of squares having unique dimensions that can be generated from the given straight lines. Each integer(say a) in the array X[] denotes lines having equation x = a, par
8 min read
Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points
Given an array arr[] consisting of N pair of integers representing coordinates of N points, the task is to find the area of the largest rectangle formed by straight lines drawn parallel to X and Y-axis from a given set of points. Examples: Input: arr[] = {{0, 0}, {1, 1}}Output: 1Explanation: The area of the largest rectangle is 1 formed by the coor
6 min read
Check whether two straight lines are parallel or not
Given equations of two lines (a1, b1, c1) and (a2, b2, c2) such that (ai, bi, ci) are the coefficients of X2, X and a constant term of the straight line respectively, in the general equation [Tex]a_{i}x^{2} + b_{i}x + c [/Tex], the task is to check if both the straight lines are parallel or not. If they are found to be parallel, then print "Yes". O
4 min read
Check if two circles intersect such that the third circle passes through their points of intersections and centers
Given centres and the radii of three circles A, B, and C in the form of {X, Y, R}, where (X, Y) is the centre of the circle and R is the radius of that circle. The task is to check if any two circles intersect such that the third circle passes through the intersecting points and the centres of the two circles. If found to be true, then print "Yes".
9 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg