Given a circular sheet of radius, r. Find the total number of rectangles with integral length and width that can be cut from the sheet that can fit on the circle, one at a time.
Examples :
Input: r=1 Output: 1 Explanation: Only 1 rectangle of dimensions 1x1.
Input: r=2 Output: 8 Explanation: The 8 possible rectangles are (1x1)(1x2)(1x3)(2x1)(2x2)(2x3)(3x1)(3x2).
Expected Time Complexity: O(r2)
Expected Auxillary Space: O(1)
Constraints:
1<=r<=1000
Approach 01:
-
C++
-
Python
#include <iostream> #include <cmath> #include <vector> class Solution { public: int rectanglesInCircle(int r) { int count = 0; for (int w = 1; w <= 2 * r; ++w) { for (int h = 1; h <= 2 * r; ++h) { if (std::sqrt(w * w + h * h) <= 2 * r) { count += 1; } } } return count; } };
import math class Solution: def rectanglesInCircle(self, r: int) -> int: count = 0 r_squared = (2 * r) ** 2 for w in range(1, 2 * r + 1): for h in range(1, 2 * r + 1): if w * w + h * h <= r_squared: count += 1 return count