Given a 2D array **A[][2] **of size** N *** (1 ≤ N ≤ 10^{3})*, where

**A[i][0]**and

**A[i][1]**denotes the length and breadth of rectangle

**i**respectively.

Two rectangle

iandjwhere (i < j) are similar if the ratio of their length and breadth is equalA[i][0] / A[i][1] = A[j][0] / A[j][1]

The task is to count the pair of rectangles that are nearly similar.

**Examples:**

Input: A[][2] = {{4, 8}, {15, 30}, {3, 6}, {10, 20}}Output:6Explanation:Pairs of similar rectangles are (0, 1), {0, 2), (0, 3), (1, 2), (1, 3), (2, 3). For every rectangle, ratio of length : breadth is 1 : 2

Input: A[][2] = {{2, 3}, {4, 5}, {7, 8}}Output:0Explanation:No pair of similar rectangles exists.

**Approach: **Follow the steps to solve the problem

- Traverse the array.
- For every pair such that (
**i < j**), check whether the rectangles are similar or not by checking if the condition**A[i][0] / A[i][1] = A[j][0] / A[j][1]**is satisfied or not. - If found to be true, increment the count.
- Finally, print the count obtained.

Below is the implementation of the above approach:

## C++

`// C++ Program for the above approach` `#include <iostream>` `using` `namespace` `std;` `// Function to calculate the count` `// of similar rectangles` `int` `getCount(` `int` `rows,` ` ` `int` `columns, ` `int` `A[][2])` `{` ` ` `int` `res = 0;` ` ` `for` `(` `int` `i = 0; i < rows; i++) {` ` ` `for` `(` `int` `j = i + 1; j < rows; j++) {` ` ` `if` `(A[i][0] * 1LL * A[j][1]` ` ` `== A[i][1] * 1LL * A[j][0]) {` ` ` `res++;` ` ` `}` ` ` `}` ` ` `}` ` ` `return` `res;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Input` ` ` `int` `A[][2]` ` ` `= { { 4, 8 }, { 10, 20 }, { 15, 30 }, { 3, 6 } };` ` ` `int` `columns = 2;` ` ` `int` `rows = ` `sizeof` `(A) / ` `sizeof` `(A[0]);` ` ` `cout << getCount(rows, columns, A);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to calculate the count` `// of similar rectangles` `static` `int` `getCount(` `int` `rows, ` `int` `columns,` ` ` `int` `[][] A)` `{` ` ` `int` `res = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < rows; i++)` ` ` `{` ` ` `for` `(` `int` `j = i + ` `1` `; j < rows; j++)` ` ` `{` ` ` `if` `(A[i][` `0` `] * A[j][` `1` `] ==` ` ` `A[i][` `1` `] * A[j][` `0` `])` ` ` `{` ` ` `res++;` ` ` `}` ` ` `}` ` ` `}` ` ` `return` `res;` `}` ` ` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Input` ` ` `int` `[][] A = { { ` `4` `, ` `8` `}, { ` `10` `, ` `20` `},` ` ` `{ ` `15` `, ` `30` `}, { ` `3` `, ` `6` `} };` ` ` `int` `columns = ` `2` `;` ` ` `int` `rows = ` `4` `;` ` ` ` ` `System.out.print(getCount(rows, columns, A));` `}` `}` `// This code is contributed by code_hunt.` |

## Python3

`# Python3 program for the above approach` `# Function to calculate the count` `# of similar rectangles` `def` `getCount(rows, columns, A):` ` ` ` ` `res ` `=` `0` ` ` `for` `i ` `in` `range` `(rows):` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, rows, ` `1` `):` ` ` `if` `(A[i][` `0` `] ` `*` `A[j][` `1` `] ` `=` `=` ` ` `A[i][` `1` `] ` `*` `A[j][` `0` `]):` ` ` `res ` `+` `=` `1` ` ` ` ` `return` `res` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Input` ` ` `A ` `=` `[ [ ` `4` `, ` `8` `], [ ` `10` `, ` `20` `],` ` ` `[ ` `15` `, ` `30` `], [ ` `3` `, ` `6` `] ]` ` ` `columns ` `=` `2` ` ` `rows ` `=` `len` `(A)` ` ` `print` `(getCount(rows, columns, A))` `# This code is contributed by SURENDRA_GANGWAR` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to calculate the count` `// of similar rectangles` `static` `int` `getCount(` `int` `rows, ` `int` `columns,` ` ` `int` `[,] A)` `{` ` ` `int` `res = 0;` ` ` ` ` `for` `(` `int` `i = 0; i < rows; i++)` ` ` `{` ` ` `for` `(` `int` `j = i + 1; j < rows; j++)` ` ` `{` ` ` `if` `(A[i, 0] * A[j, 1] ==` ` ` `A[i, 1] * A[j, 0])` ` ` `{` ` ` `res++;` ` ` `}` ` ` `}` ` ` `}` ` ` `return` `res;` `}` `// Driver code` `static` `void` `Main()` `{` ` ` ` ` `// Input` ` ` `int` `[,] A = { { 4, 8 }, { 10, 20 },` ` ` `{ 15, 30 }, { 3, 6 } };` ` ` `int` `columns = 2;` ` ` `int` `rows = 4;` ` ` ` ` `Console.Write(getCount(rows, columns, A));` `}` `}` `// This code is contributed by divyesh072019` |

**Output:**

6

**Time Complexity**: O(N^{2})**Auxiliary Space**: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.