Header Ads Widget

Golden rectangles! Hackerearth solution

  Problem:-

 You have N rectangles. A rectangle is golden if the ratio of its sides is in between [1.6,1.7], both inclusive. Your task is to find the number of golden rectangles.

Input format

  • First line: Integer N denoting the number of rectangles
  • Each of the N following lines: Two integers W,H denoting the width and height of a rectangle

Output format

  • Print the answer in a single line.

Constraints

1N105

1W,H109

Sample Input
5
10 1
165 100
180 100
170 100
160 100

Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are three golden rectangles: (165, 100), (170, 100), (160, 100).

10

Code:-

Solution 1 ( C language):-


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool isInRatio(double a, double b){
return (a/b>=1.6 && a/b<=1.7);
}
int main () {
char first[10] = {0};
fgets(first, 10, stdin);
int N = strtoll(first, NULL, 10);
int sum = 0;
for(int i = 0; i<N; i++){
char line[100]={0};
fgets(line, 100, stdin);
char *end;
int W = strtoll(line, &end, 10);
int H = strtoll(end, NULL, 10);
if(isInRatio(W, H)||isInRatio(H,W)){
sum++;
}
}
printf("%d\n", sum);
return 0;
}

Solution 2 ( C++ language):-

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define rep(i, k, n) for (long long int i = k; i < n; i++)
#define rept(i, k, n) for (auto i = k; i != n; ++i)
#define drep(i, k, n) for (long long int i = k; i >= n; i--)
#define endl '\n'
const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
int main() {
    ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
    
    ll t;
    cin>>t;
    int count=0;
    while(t-->0)
    {
        int w,h;
        cin>>w>>h;
        
if(w>=1.6*h && w<=1.7*h)
         {
count++;
            
         }
         else if(h>=1.6*w && h<=1.7*w)
         {
             count++;
         }
        // cout<<rat<<endl;
    }
    cout<<count<<endl;
}
 

Solution 3 (java language):-

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class GoldenRectangle {
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;
        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(
                    new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') {
                    if (cnt != 0) {
                        break;
                    }
                    else {
                        continue;
                    }
                }
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }
        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }
        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }
        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)
                return -ret;
            return ret;
        }
        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0,
                    BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

keywords:-

 ,golden rectangle hackerearth solutions,golden rectangle hackerearth solutions java,you have n rectangles a rectangle is golden,golden rectangle in java,golden string hackerearth solution,golden rectangle problem,popular shop problem hackerearth,special shop hackerearth solutions,


Recommended Post:-