18(June) Number of Rectangles in a Circle
18. Number of Rectangles in a Circle
The problem can be found at the following link: Question Link
Problem Description
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 and fit within the circle, one at a time.
Examples:
Input:
r = 1Output:
1Explanation: Only 1 rectangle of dimensions 1x1.
Input:
r = 2Output:
8Explanation: The 8 possible rectangles are: (1x1), (1x2), (1x3), (2x1), (2x2), (2x3), (3x1), (3x2).
My Approach
Initialization:
Initialize a variable
ansto store the count of rectangles that can fit within the circle.Define a
limitvariable as4 * R * Rwhich represents the square of the diameter of the circle.
Rectangle Calculation:
Use nested loops to iterate over possible rectangle dimensions
(i, j)whereiandjrange from 1 to2 * R.For each pair
(i, j), check if the sum of their squared dimensions is less than or equal tolimit.If the condition is satisfied, increment
ans.
Return:
Return the value of
answhich contains the total count of rectangles that can fit within the circle.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(R^2), as we iterate through all possible rectangle dimensions within the given range.
Expected Auxiliary Space Complexity: O(1), as we use a constant amount of additional space.
Code (C++)
Code (Java)
Code (Python)
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated