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 n, int b){int i = 1;int cnt = 1;int ans = 0;while (true){if (2 * n <= b){ans += cnt * i;i++;b = b - n;}elsebreak;}return ans;}int main(){int n = 2, b = 5;cout << squareInTrinagle(n, b) << "\n";return 0;}
No comments:
Post a Comment