Given a circular sheet of radius, r. Find the number of different dimension rectangles with integral length and width that can be cut from the sheet that can fit on the circle.
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).
Table of Content
[Naive Approach] Using Brute Force - O(r^2) Time and O(1) Space
The idea is to generate all possible pairs of integer lengths and widths for rectangles and check whether each rectangle can fit inside the circle. We iterate over all values from 1 to 2r−1 for both dimensions using nested loops. For each pair (i , j), we compute the square of the diagonal as i2 + j2 and compare it with (2r)2, which represents the square of the diameter of the circle. If the diagonal is less than or equal to the diameter, the rectangle can fit inside the circle, and we increment the count. This way, all valid rectangles are counted by explicitly checking every possibility.
#include <bits/stdc++.h>
using namespace std;
// Function to return the total possible
// rectangles that can be cut from the circle
int rectanglesInCircle(int r)
{
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over all possible lengths
for (int i = 1; i < 2 * r; i++)
{
// Iterate over all possible widths
for (int j = 1; j < 2 * r; j++)
{
// Check if rectangle diagonal fits inside circle
if (i * i + j * j <= rr)
ans++;
}
}
return ans;
}
// Driver Code
int main()
{
int r = 2;
int totalRectangles = rectanglesInCircle(r);
cout << "Number of rectangles: " << totalRectangles;
return 0;
}
#include <stdio.h>
// Function to return the total possible
// rectangles that can be cut from the circle
int rectanglesInCircle(int r)
{
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over all possible lengths
for (int i = 1; i < 2 * r; i++)
{
// Iterate over all possible widths
for (int j = 1; j < 2 * r; j++)
{
// Check if rectangle diagonal fits inside circle
if (i * i + j * j <= rr)
ans++;
}
}
return ans;
}
// Driver Code
int main()
{
int r = 2;
int totalRectangles = rectanglesInCircle(r);
printf("Number of rectangles: %d\n", totalRectangles);
return 0;
}
public class GfG {
// Function to return the total possible
// rectangles that can be cut from the circle
static int rectanglesInCircle(int r) {
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over all possible lengths
for (int i = 1; i < 2 * r; i++) {
// Iterate over all possible widths
for (int j = 1; j < 2 * r; j++) {
// Check if rectangle diagonal fits inside circle
if (i * i + j * j <= rr)
ans++;
}
}
return ans;
}
// Driver Code
public static void main(String[] args) {
int r = 2;
int totalRectangles = rectanglesInCircle(r);
System.out.println("Number of rectangles: " + totalRectangles);
}
}
def rectanglesInCircle(r):
ans = 0
# rr stores (2r)^2 = 4 * r^2
rr = 4 * r * r
# Iterate over all possible lengths
for i in range(1, 2 * r):
# Iterate over all possible widths
for j in range(1, 2 * r):
# Check if rectangle diagonal fits inside circle
if i * i + j * j <= rr:
ans += 1
return ans
# Driver Code
if __name__ == "__main__":
r = 2
totalRectangles = rectanglesInCircle(r)
print("Number of rectangles:", totalRectangles)
using System;
public class GfG {
// Function to return the total possible
// rectangles that can be cut from the circle
static int rectanglesInCircle(int r) {
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over all possible lengths
for (int i = 1; i < 2 * r; i++) {
// Iterate over all possible widths
for (int j = 1; j < 2 * r; j++) {
// Check if rectangle diagonal fits inside circle
if (i * i + j * j <= rr)
ans++;
}
}
return ans;
}
// Driver Code
public static void Main() {
int r = 2;
int totalRectangles = rectanglesInCircle(r);
Console.WriteLine("Number of rectangles: " + totalRectangles);
}
}
function rectanglesInCircle(r) {
let ans = 0;
// rr stores (2r)^2 = 4 * r^2
let rr = 4 * r * r;
// Iterate over all possible lengths
for (let i = 1; i < 2 * r; i++) {
// Iterate over all possible widths
for (let j = 1; j < 2 * r; j++) {
// Check if rectangle diagonal fits inside circle
if (i * i + j * j <= rr)
ans++;
}
}
return ans;
}
// Driver Code
let r = 2;
let totalRectangles = rectanglesInCircle(r);
console.log("Number of rectangles:", totalRectangles);
Output
Number of rectangles: 8
Time Complexity : O(r^2)
Auxiliary Space: O(1)
[Expected Approach] Using Mathematical Enumeration - O(r) Time O(1) Space
The idea is to use the mathematical equation. For each possible value of x from 1 to 2r, we compute the maximum possible value of y such that the point (x, y) lies within the circle using the relation x2 + y2 ≤ (2r)2. This gives
y = \sqrt{(2r)^2 - x^2} . By summing up all such valid y-values for every x, we effectively count all the rectangles that can be formed within the circle.

#include <bits/stdc++.h>
using namespace std;
// Function to return the total possible
// rectangles that can be cut from the circle
int rectanglesInCircle(int r)
{
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over possible x-coordinates
for (int i = 1; i <= 2 * r; i++)
{
// Add number of valid y-coordinates
// sqrt(rr - i*i) gives maximum y for given x
ans += sqrt(rr - i * i);
}
return ans;
}
// Driver Code
int main()
{
int r = 2;
int totalRectangles = rectanglesInCircle(r);
cout << "Number of rectangles: " << totalRectangles;
return 0;
}
#include <stdio.h>
#include <math.h>
// Function to return the total possible
// rectangles that can be cut from the circle
int rectanglesInCircle(int r)
{
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over possible x-coordinates
for (int i = 1; i <= 2 * r; i++)
{
// Add number of valid y-coordinates
// sqrt(rr - i*i) gives maximum y for given x
ans += (int)sqrt(rr - i * i);
}
return ans;
}
// Driver Code
int main()
{
int r = 2;
int totalRectangles = rectanglesInCircle(r);
printf("Number of rectangles: %d", totalRectangles);
return 0;
}
import java.lang.Math;
// Function to return the total possible
// rectangles that can be cut from the circle
public class GfG {
public static int rectanglesInCircle(int r) {
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over possible x-coordinates
for (int i = 1; i <= 2 * r; i++) {
// Add number of valid y-coordinates
// sqrt(rr - i*i) gives maximum y for given x
ans += (int)Math.sqrt(rr - i * i);
}
return ans;
}
// Driver Code
public static void main(String[] args) {
int r = 2;
int totalRectangles = rectanglesInCircle(r);
System.out.println("Number of rectangles: " + totalRectangles);
}
}
import math
# Function to return the total possible
# rectangles that can be cut from the circle
def rectanglesInCircle(r):
ans = 0
# rr stores (2r)^2 = 4 * r^2
rr = 4 * r * r
# Iterate over possible x-coordinates
for i in range(1, 2 * r + 1):
# Add number of valid y-coordinates
# sqrt(rr - i*i) gives maximum y for given x
ans += int(math.sqrt(rr - i * i))
return ans
# Driver Code
if __name__ == "__main__":
r = 2
totalRectangles = rectanglesInCircle(r)
print("Number of rectangles:", totalRectangles)
using System;
// Function to return the total possible
// rectangles that can be cut from the circle
public class GfG {
public static int rectanglesInCircle(int r) {
int ans = 0;
// rr stores (2r)^2 = 4 * r^2
int rr = 4 * r * r;
// Iterate over possible x-coordinates
for (int i = 1; i <= 2 * r; i++) {
// Add number of valid y-coordinates
// sqrt(rr - i*i) gives maximum y for given x
ans += (int)Math.Sqrt(rr - i * i);
}
return ans;
}
// Driver Code
public static void Main() {
int r = 2;
int totalRectangles = rectanglesInCircle(r);
Console.WriteLine("Number of rectangles: " + totalRectangles);
}
}
// Function to return the total possible
// rectangles that can be cut from the circle
function rectanglesInCircle(r) {
let ans = 0;
// rr stores (2r)^2 = 4 * r^2
let rr = 4 * r * r;
// Iterate over possible x-coordinates
for (let i = 1; i <= 2 * r; i++) {
// Add number of valid y-coordinates
// sqrt(rr - i*i) gives maximum y for given x
ans += Math.floor(Math.sqrt(rr - i * i));
}
return ans;
}
// Driver Code
let r = 2;
let totalRectangles = rectanglesInCircle(r);
console.log("Number of rectangles:", totalRectangles);
Output
Number of rectangles: 8
Time Complexity : O(r)
Auxiliary Space: O(1)