- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given sides of rectangles in and range variables first and last. The goal is to find the count of rectangles that have a ratio of their side’s length/breadth lying in the range [ first, last].

**For Example**

rec[] = { { 200, 210 }, { 100, 50 }, { 300, 190}, {180, 200}, {300, 200}} and first = 1.0, last = 1.6

Count of number of rectangles such that ratio of sides lies in the range [a,b] are: 4

The sides that have ratio in the range [ 1.0,1.6 ] are : {200,210}, {300,190}, {180,200}, {300,200}

rec[] = { { 10,20 }, { 30, 10 }, { 100, 500}, {900, 300}, {450, 90}} and first = 3.0, last = 4.0

Count of number of rectangles such that ratio of sides lies in the range [a,b] are: 2

The sides that have ratio in the range [ 3.0,4.0 ] are : {30,10}, {900,300}

**Approach used in the below program is as follows** −

In this approach we will take sides in the form of an array of pair<int,int>. For each pair check if the larger value/smaller value has a result which lies in the range [ first,last ]. If true then increment count of such pairs.

Take an array rec[] of type pair<int,int>.

Take two variables first and last for defining range.

Function ratio_sides(pair<int, int> rec[], int total, double first, double last) takes the sides of rectangles and returns the count of number of rectangles such that ratio of sides lies in the range [a,b].

Take the initial count as 0.

Using a for loop traverse from i=0 to i<total.

Extract larger value in pair rec[i] as maxi = max(rec[i].first, rec[i].second).

Extract smaller value in pair rec[i] as mini = min(rec[i].first, rec[i].second).

Calculate average=maxi/mini.

If average has value in range [ first,last ], then increment count.

At the end of the for loop return count as result..

#include <bits/stdc++.h> using namespace std; int ratio_sides(pair<int, int> rec[], int total, double first, double last){ int count = 0; for (int i = 0; i < total; i++){ double maxi = max(rec[i].first, rec[i].second); double mini = min(rec[i].first, rec[i].second); double average = maxi/mini; if (average >= first){ if(average <= last){ count++; } } } return count; } int main(){ pair<int, int> rec[] = { { 200, 210 }, { 100, 50 }, { 300, 190}, {180, 200}, {300, 200}}; int total = 5; double first = 1.0, last = 1.6; cout<<"Count of number of rectangles such that ratio of sides lies in the range [a,b] are: "<<ratio_sides(rec, total, first, last); return 0; }

If we run the above code it will generate the following output −

Count the number of rectangles such that ratio of sides lies in the range [a,b] are: 4

- Related Questions & Answers
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Count of all possible values of X such that A % X = B in C++
- Count number of triplets in an array having sum in the range [a,b] in C++
- Count the number of intervals in which a given value lies in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Count minimum bits to flip such that XOR of A and B equal to C in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Count total divisors of A or B in a given range in C++
- Find count of digits in a number that divide the number in C++
- Count the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Program to find number of rectangles that can form the largest square in Python
- Count of Numbers such that difference between the number and sum of its digits not less than L in C++

Advertisements