VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-of-parallelograms-when-n-horizontal-parallel-lines-intersect-m-vertical-parallellines/

⇱ Number of parallelograms when n horizontal parallel lines intersect m vertical parallel lines - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 12 Nov, 2023

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. 

👁 Image


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: 


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:


Output
100

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

Comment
Article Tags:
Article Tags: