Count the number of rectangles such that ratio of sides lies in the range [a,b]
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++
#include <bits/stdc++.h>
using namespace std;
int countRatios(pair< int , int > arr[], int n,
double a, double b)
{
int count = 0;
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);
double ratio = large / small;
if (ratio >= a && ratio <= b)
count += 1;
}
return count;
}
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;
}
|
Java
class GFG
{
static int n = 3 ;
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int countRatios(pair []arr, int n,
double a, double b)
{
int count = 0 ;
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);
double ratio = large / small;
if (ratio >= a && ratio <= b)
count += 1 ;
}
return count;
}
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));
}
}
|
Python3
def countRatios(arr, n, a, b):
count = 0
for i in range (n):
large = max (arr[i][ 0 ],
arr[i][ 1 ])
small = min (arr[i][ 0 ],
arr[i][ 1 ])
ratio = large / small
if (ratio > = a and
ratio < = b):
count + = 1
return count
if __name__ = = "__main__" :
arr = [[ 165 , 100 ],
[ 180 , 100 ],
[ 100 , 170 ]]
a = 1.6
b = 1.7
n = 3
print (countRatios(arr, n, a, b))
|
C#
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;
}
}
static int countRatios(pair []arr, int n,
double a, double b)
{
int count = 0;
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);
double ratio = large / small;
if (ratio >= a && ratio <= b)
count += 1;
}
return count;
}
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));
}
}
|
Javascript
<script>
function countRatios(arr,n,a,b)
{
let count = 0;
for (let i = 0; i < n; i++)
{
let large = Math.max(arr[i][0],
arr[i][1]);
let small = Math.min(arr[i][0],
arr[i][1]);
let ratio = large / small;
if (ratio >= a && ratio <= b)
count += 1;
}
return count;
}
let arr = [[165, 100],
[180, 100],
[100, 170]];
let a = 1.6, b = 1.7;
let n = 3;
document.write(countRatios(arr, n, a, b));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
06 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...