BMI - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Practice

Setter:
Tester: Istvan Nagy
Editorialist: Taranpreet Singh

DIFFICULTY

Cakewalk

PREREQUISITES

None

PROBLEM

Given height H of chef in meters and mass M in kilograms, Body Mass Index is computed as \displaystyle \frac{H}{M^2}.
Report the category into which Chef falls, based on his BMI:

  • Category 1: Underweight if BMI \leq 18
  • Category 2: Normal weight if BMI \in \{19, 20,\ldots, 24\}
  • Category 3: Overweight if BMI \in \{25, 26,\ldots, 29\}
  • Category 4: Obesity if BMI \geq 30

Given that M^2 divides H

QUICK EXPLANATION

Compute BMI using the formula given and then use if-else statement to determine category.

EXPLANATION

All we need to do in this problem is to first compute the BMI index, using formula given. Since M^2 divides H, \displaystyle \frac{H}{M^2} turns out to be an integer value.

Let’s say V = \displaystyle \frac{H}{M^2} is the BMI index.

Now, we need to categorize Chef into one of the category using if-else statement.

if V <= 18: category 1
else if V <= 24: category 2
else if V <= 29: category 3
else category 4

Hence, we can print the category that way.

Follow up

  • What is the minimum and maximum BMI possible with constraints given in problem statement?
  • What is the number of distinct combinations (H, M) which can appear in input for current problem, with constraints given in problem statement?

TIME COMPLEXITY

The time complexity is O(1) per test case, since all operations (computing BMI and then if-else statements) are performed in constant time.
The space complexity is also O(1) per test case.

SOLUTIONS

Setter's Solution
// #include <format>
#include <climits>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>

# define pb push_back 
#define pii pair<int, int>
#define mp make_pair
# define ll long long int

using namespace std;
  
FILE *fp;
ofstream outfile;

long long readInt(long long l,long long r,char endd){
    long long x=0;
    int cnt=0;
    int fi=-1;
    bool is_neg=false;
    while(true){
        char g=getchar();
        // char g = getc(fp);
        if(g=='-'){
            assert(fi==-1);
            is_neg=true;
            continue;
        }
        if('0'<=g && g<='9'){
            x*=10;
            x+=g-'0';
            if(cnt==0){
                fi=g-'0';
            }
            cnt++;
            assert(fi!=0 || cnt==1);
            assert(fi!=0 || is_neg==false);
            
            assert(!(cnt>19 || ( cnt==19 && fi>1) ));
        } else if(g==endd){
            if(is_neg){
                x= -x;
            }
            assert(l<=x && x<=r);
            return x;
        } else {
            assert(false);
        }
    }
}
string readString(int l,int r,char endd){
    string ret="";
    int cnt=0;
    while(true){
        char g=getchar();
        // char g=getc(fp);
        assert(g != -1);
        if(g==endd){
            break;
        }
        cnt++;
        ret+=g;
    }
    assert(l<=cnt && cnt<=r);
    return ret;
}
long long readIntSp(long long l,long long r){
    return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
    return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
    return readString(l,r,'\n');
}
string readStringSp(int l,int r){
    return readString(l,r,' ');
}

const int maxh = 1e2, maxw = 1e4, maxt = 1e6;

int main()
{   
    // for(int test = 0; test <= 0; test++){
    //     string in = "/home/daanish/CP/codechef/LearningTeam/Problems/BMI/in" + to_string(test) + ".txt";
    //     string out = "/home/daanish/CP/codechef/LearningTeam/Problems/BMI/out" + to_string(test) + ".txt";
    //     fp = fopen (in.c_str(), "r");
        // outfile.open(out.c_str());
        int t = readIntLn(1, maxt);
        // cout << t << endl;
        // int cnt[] = {0, 0, 0, 0, 0};
        while(t--){
            int w = readIntSp(1, maxw), h = readIntLn(1, maxh);
            assert(w % (h * h) == 0);
            int bmi = w / (h * h);
            int cate = 4;
            if(bmi <= 18)cate = 1;
            else if(bmi <= 24)cate = 2;
            else if(bmi <= 29)cate = 3;
            // cnt[cate]++;
            // outfile << cate << endl;
            cout << cate << endl;
        }
        // for(int i = 1; i <= 4; i++)cout << cnt[i] << " ";
        // outfile.close();
        assert(getchar()==-1);
        // assert(getc(fp)==-1);
    // }
} 
Testers Solution
#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <random>

#ifdef HOME
#include <windows.h>
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

template<class T> bool umin(T& a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T& a, T b) { return a < b ? (a = b, true) : false; }

using namespace std;

long long readInt(long long l, long long r, char endd) {
    long long x = 0;
    int cnt = 0;
    int fi = -1;
    bool is_neg = false;
    while (true) {
	    char g = getchar();
	    if (g == '-') {
		    assert(fi == -1);
		    is_neg = true;
		    continue;
	    }
	    if ('0' <= g && g <= '9') {
		    x *= 10;
		    x += g - '0';
		    if (cnt == 0) {
			    fi = g - '0';
		    }
		    cnt++;
		    assert(fi != 0 || cnt == 1);
		    assert(fi != 0 || is_neg == false);

		    assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
	    }
	    else if (g == endd) {
		    assert(cnt > 0);
		    if (is_neg) {
			    x = -x;
		    }
		    assert(l <= x && x <= r);
		    return x;
	    }
	    else {
		    //assert(false);
	    }
    }
}

string readString(int l, int r, char endd) {
    string ret = "";
    int cnt = 0;
    while (true) {
	    char g = getchar();
	    assert(g != -1);
	    if (g == endd) {
		    break;
	    }
	    cnt++;
	    ret += g;
    }
    assert(l <= cnt && cnt <= r);
    return ret;
}
long long readIntSp(long long l, long long r) {
    return readInt(l, r, ' ');
}
long long readIntLn(long long l, long long r) {
    return readInt(l, r, '\n');
}
string readStringLn(int l, int r) {
    return readString(l, r, '\n');
}
string readStringSp(int l, int r) {
    return readString(l, r, ' ');
}



int main(int argc, char** argv)
{
#ifdef HOME
    if (IsDebuggerPresent())
    {
	    freopen("../in.txt", "rb", stdin);
	    freopen("../out.txt", "wb", stdout);
    }
#endif
    ios::sync_with_stdio(false);
    int T = readIntLn(1, 20'000);
    forn(tc, T)
    {
	    int M = readIntSp(1, 10000);
	    int H = readIntLn(1, 100);
	    assert(M % (H * H) == 0);
	    int R = M / (H * H);
	    if (R <= 18)
	    {
		    printf("1\n");
	    }
	    else if (R <= 24) {
		    printf("2\n");
	    }
	    else if (R <= 29) {
		    printf("3\n");
	    }
	    else {
		    printf("4\n");
	    }
    }

    return 0;
}
Editorialist's Solution
import java.util.*;
import java.io.*;
class BMI{
    //SOLUTION BEGIN
    void pre() throws Exception{}
    void solve(int TC) throws Exception{
        int M = ni(), H = ni();
        int BMI = M/(H*H);
        if(BMI <= 18)pn(1);
        else if(BMI <= 24)pn(2);
        else if(BMI <= 29)pn(3);
        else pn(4);
    }
    //SOLUTION END
    void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
    static boolean multipleTC = true;
    FastReader in;PrintWriter out;
    void run() throws Exception{
        in = new FastReader();
        out = new PrintWriter(System.out);
        //Solution Credits: Taranpreet Singh
        int T = (multipleTC)?ni():1;
        pre();for(int t = 1; t<= T; t++)solve(t);
        out.flush();
        out.close();
    }
    public static void main(String[] args) throws Exception{
        new BMI().run();
    }
    int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
    void p(Object o){out.print(o);}
    void pn(Object o){out.println(o);}
    void pni(Object o){out.println(o);out.flush();}
    String n()throws Exception{return in.next();}
    String nln()throws Exception{return in.nextLine();}
    int ni()throws Exception{return Integer.parseInt(in.next());}
    long nl()throws Exception{return Long.parseLong(in.next());}
    double nd()throws Exception{return Double.parseDouble(in.next());}

    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

        String next() throws Exception{
            while (st == null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (IOException  e){
                    throw new Exception(e.toString());
                }
            }
            return st.nextToken();
        }

        String nextLine() throws Exception{
            String str = "";
            try{   
                str = br.readLine();
            }catch (IOException e){
                throw new Exception(e.toString());
            }  
            return str;
        }
    }
}

Feel free to share your approach. Suggestions are welcomed as always. :slight_smile:

1 Like

Hey team,
The actual solution is in the main function but what’s the reason behind the below:
I am relatively new to competitive coding.

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

template<class T> bool umin(T& a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T& a, T b) { return a < b ? (a = b, true) : false; }

using namespace std;

long long readInt(long long l, long long r, char endd) {
    long long x = 0;
    int cnt = 0;
    int fi = -1;
    bool is_neg = false;
    while (true) {
	    char g = getchar();
	    if (g == '-') {
		    assert(fi == -1);
		    is_neg = true;
		    continue;
	    }
	    if ('0' <= g && g <= '9') {
		    x *= 10;
		    x += g - '0';
		    if (cnt == 0) {
			    fi = g - '0';
		    }
		    cnt++;
		    assert(fi != 0 || cnt == 1);
		    assert(fi != 0 || is_neg == false);

		    assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
	    }
	    else if (g == endd) {
		    assert(cnt > 0);
		    if (is_neg) {
			    x = -x;
		    }
		    assert(l <= x && x <= r);
		    return x;
	    }
	    else {
		    //assert(false);
	    }
    }
}

string readString(int l, int r, char endd) {
    string ret = "";
    int cnt = 0;
    while (true) {
	    char g = getchar();
	    assert(g != -1);
	    if (g == endd) {
		    break;
	    }
	    cnt++;
	    ret += g;
    }
    assert(l <= cnt && cnt <= r);
    return ret;
}
long long readIntSp(long long l, long long r) {
    return readInt(l, r, ' ');
}
long long readIntLn(long long l, long long r) {
    return readInt(l, r, '\n');
}
string readStringLn(int l, int r) {
    return readString(l, r, '\n');
}
string readStringSp(int l, int r) {
    return readString(l, r, ' ');
}
1 Like

Same question.


#include <math.h>

using namespace std;

int main()

{

    int T;

    cin >> T;

    while (T--)

    {

        int M, H;

        cin >> M >> H;

        int bmi = M / pow(H, 2);

        if (bmi <= 18)

        

            cout << "1\n";

        

        else if (bmi >= 18 && bmi <= 24)

        

            cout << "2\n";

        

        else if (bmi > 25 && bmi <= 29)

        

            cout << "3\n";

        

        else

        

            cout << "4\n";

        

    }

}```  


why my answer is a wrong answer?