Given the length and breadth of N rectangles and a range i.e. [a, b], the task is to count the number of rectangles whose sides(larger/smaller) ratio is in the range [a, b].

**Examples:**

Input:{{165, 100}, {180, 100}, {100, 170}}, a = 1.6, b = 1.7

Output:2

165/100 = 1.65

170/100 = 1.7

Input:{{10, 12}, {26, 19}}, a = 0.8, b = 1.2

Output:1

**Approach:** Iterate in the array of pairs, and increase the counter when *max(a[i].first, a[i].second)/min(a[i].first, a[i].second)* lies in the range a and b.

Below is the implementation of the above approach:

## C++

`// C++ program to print the length of the shortest ` `// subarray with all elements greater than X ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the number of ratios ` `int` `countRatios(pair<` `int` `, ` `int` `> arr[], ` `int` `n, ` ` ` `double` `a, ` `double` `b) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// count the number of ratios ` ` ` `// by iterating ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `double` `large = max(arr[i].first, arr[i].second); ` ` ` `double` `small = min(arr[i].first, arr[i].second); ` ` ` ` ` `// find ratio ` ` ` `double` `ratio = large / small; ` ` ` ` ` `// check if lies in range ` ` ` `if` `(ratio >= a && ratio <= b) ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `pair<` `int` `, ` `int` `> arr[] = { { 165, 100 }, ` ` ` `{ 180, 100 }, ` ` ` `{ 100, 170 } }; ` ` ` `double` `a = 1.6, b = 1.7; ` ` ` `int` `n = 3; ` ` ` ` ` `cout << countRatios(arr, n, a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print the length of the shortest ` `// subarray with all elements greater than X ` `class` `GFG ` `{ ` `static` `int` `n = ` `3` `; ` `static` `class` `pair ` `{ ` ` ` `int` `first, second; ` ` ` `public` `pair(` `int` `first, ` `int` `second) ` ` ` `{ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` `} ` ` ` `// Function to count the number of ratios ` `static` `int` `countRatios(pair []arr, ` `int` `n, ` ` ` `double` `a, ` `double` `b) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// count the number of ratios ` ` ` `// by iterating ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `double` `large = Math.max(arr[i].first, ` ` ` `arr[i].second); ` ` ` `double` `small = Math.min(arr[i].first, ` ` ` `arr[i].second); ` ` ` ` ` `// find ratio ` ` ` `double` `ratio = large / small; ` ` ` ` ` `// check if lies in range ` ` ` `if` `(ratio >= a && ratio <= b) ` ` ` `count += ` `1` `; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `pair []arr = {` `new` `pair(` `165` `, ` `100` `), ` ` ` `new` `pair(` `180` `, ` `100` `), ` ` ` `new` `pair(` `100` `, ` `170` `)}; ` ` ` `double` `a = ` `1.6` `, b = ` `1.7` `; ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println(countRatios(arr, n, a, b)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print the length of the shortest ` `// subarray with all elements greater than X ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `n = 3; ` `class` `pair ` `{ ` ` ` `public` `int` `first, second; ` ` ` `public` `pair(` `int` `first, ` `int` `second) ` ` ` `{ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` `} ` ` ` `// Function to count the number of ratios ` `static` `int` `countRatios(pair []arr, ` `int` `n, ` ` ` `double` `a, ` `double` `b) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// count the number of ratios ` ` ` `// by iterating ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `double` `large = Math.Max(arr[i].first, ` ` ` `arr[i].second); ` ` ` `double` `small = Math.Min(arr[i].first, ` ` ` `arr[i].second); ` ` ` ` ` `// find ratio ` ` ` `double` `ratio = large / small; ` ` ` ` ` `// check if lies in range ` ` ` `if` `(ratio >= a && ratio <= b) ` ` ` `count += 1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `pair []arr = {` `new` `pair(165, 100), ` ` ` `new` `pair(180, 100), ` ` ` `new` `pair(100, 170)}; ` ` ` `double` `a = 1.6, b = 1.7; ` ` ` `int` `n = 3; ` ` ` ` ` `Console.WriteLine(countRatios(arr, n, a, b)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Count of Rectangles with area K made up of only 1s from given Binary Arrays
- Number of unique rectangles formed using N unit squares
- Number of rectangles in N*M grid
- Number of rectangles in a circle of radius R
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Total number of unit cells covered by all given Rectangles
- Program to find the count of coins of each type from the given ratio
- Find if two rectangles overlap
- Create a matrix with alternating rectangles of O and X
- Find all rectangles filled with 0
- Total area of two overlapping rectangles
- Sum of Areas of Rectangles possible for an array
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Smallest square formed with given rectangles
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Minimum area of square holding two identical rectangles
- Find side of Square which makes minimal area to fit two identical rectangles inside it

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.