Square in Triangle

Your task is to find the maximum no. of the square of length X  that can fit into an isosceles right angled triangle of base length B.

One side of the square must be parallel to the base of the isosceles triangle.

Example:

Input:  n = 2, b = 5
Output: 1

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int squareInTrinagle(int nint b)
{
    int i = 1;
    int cnt = 1;
    int ans = 0;
    while (true)
    {
        if (2 * n <= b)
        {
            ans += cnt * i;
            i++;
            b = b - n;
        }
        else
            break;
    }
    return ans;
}
int main()
{

    int n = 2b = 5;

    cout << squareInTrinagle(nb<< "\n";

    return 0;
}


No comments:

Post a Comment