# Rectangle with minimum possible difference between the length and the width

Given an integer **area**, the task is to find the length and breadth of a rectangle with the given area such that the difference between the length and the breadth is minimum possible.

**Examples:**

Input:area = 99

Output:l = 11, b = 9

All possible rectangles (l, b) are (99, 1), (33, 3) and (11, 9)

And the one with the minimum |l – b| is (11, 9)

Input:area = 25

Output:l = 5, b = 5

**Approach:** The task is to find two integers **l** and **b** such that **l * b = area** and **|l – b|** is as minimum as possible. Factorization can be used to solve the problem but doing just simple factorization from **1** to **N** will take a long time to get the required output for larger values of **N**.

To overcome this, just iterate upto . Considering ** < l ≤ N**, then for all values of **l**, **b** will always be **< **.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the length (l) ` `// and breadth (b) of the rectangle ` `// having area = N and |l - b| as ` `// minimum as possible ` `void` `find_rectangle(` `int` `area) ` `{ ` ` ` `int` `l, b; ` ` ` `int` `M = ` `sqrt` `(area), ans; ` ` ` ` ` `for` `(` `int` `i = M; i >= 1; i--) { ` ` ` ` ` `// i is a factor ` ` ` `if` `(area % i == 0) { ` ` ` ` ` `// l >= sqrt(area) >= i ` ` ` `l = (area / i); ` ` ` ` ` `// so here l is +ve always ` ` ` `b = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Here l and b are length and ` ` ` `// breadth of the rectangle ` ` ` `cout << ` `"l = "` `<< l << ` `", b = "` ` ` `<< b << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `area = 99; ` ` ` `find_rectangle(area); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `//Function to print the length (l) ` ` ` `//and breadth (b) of the rectangle ` ` ` `//having area = N and |l - b| as ` ` ` `//minimum as possible ` ` ` `static` `void` `find_rectangle(` `int` `area) ` ` ` `{ ` ` ` `int` `l=` `0` `, b=` `0` `; ` ` ` `int` `M = (` `int` `)Math.sqrt(area), ans; ` ` ` ` ` `for` `(` `int` `i = M; i >= ` `1` `; i--) ` ` ` `{ ` ` ` ` ` `// i is a factor ` ` ` `if` `(area % i == ` `0` `) ` ` ` `{ ` ` ` ` ` `// l >= sqrt(area) >= i ` ` ` `l = (area / i); ` ` ` ` ` `// so here l is +ve always ` ` ` `b = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Here l and b are length and ` ` ` `// breadth of the rectangle ` ` ` `System.out.println(` `"l = "` `+ l + ` `", b = "` `+ b); ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `area = ` `99` `; ` ` ` `find_rectangle(area); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ita_c. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `import` `math as mt ` ` ` `# Function to prthe length (l) ` `# and breadth (b) of the rectangle ` `# having area = N and |l - b| as ` `# minimum as possible ` `def` `find_rectangle(area): ` ` ` ` ` `l, b ` `=` `0` `, ` `0` ` ` `M ` `=` `mt.ceil(mt.sqrt(area)) ` ` ` `ans ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(M, ` `0` `, ` `-` `1` `): ` ` ` ` ` `# i is a factor ` ` ` `if` `(area ` `%` `i ` `=` `=` `0` `): ` ` ` ` ` `# l >= sqrt(area) >= i ` ` ` `l ` `=` `(area ` `/` `/` `i) ` ` ` ` ` `# so here l is +ve always ` ` ` `b ` `=` `i ` ` ` `break` ` ` ` ` `# Here l and b are length and ` ` ` `# breadth of the rectangle ` ` ` `print` `(` `"l ="` `, l, ` `", b ="` `, b) ` ` ` `# Driver code ` `area ` `=` `99` `find_rectangle(area) ` ` ` `# This code is contributed by ` `# Mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

// C# implementation of the approach

using System;

class GFG

{

// Function to print the length (l)

// and breadth (b) of the rectangle

// having area = N and |l – b| as

// minimum as possible

static void find_rectangle(int area)

{

int l = 0, b = 0;

int M = (int)Math.Sqrt(area);

for (int i = M; i >= 1; i–)

{

// i is a factor

if (area % i == 0)

{

// l >= sqrt(area) >= i

l = (area / i);

// so here l is +ve always

b = i;

break;

}

}

// Here l and b are length and

// breadth of the rectangle

Console.WriteLine(“l = ” + l + “, b = ” + b);

}

// Driver code

public static void Main()

{

int area = 99;

find_rectangle(area);

}

}

// This code is contributed by Mukul Singh.

## PHP

`<?php ` `// Php implementation of the approach ` ` ` `// Function to print the length (l) ` `// and breadth (b) of the rectangle ` `// having area = N and |l - b| as ` `// minimum as possible ` `function` `find_rectangle(` `$area` `) ` `{ ` ` ` `$M` `= ` `floor` `(sqrt(` `$area` `)); ` ` ` ` ` `for` `(` `$i` `= ` `$M` `; ` `$i` `>= 1; ` `$i` `--) ` ` ` `{ ` ` ` ` ` `// i is a factor ` ` ` `if` `(` `$area` `% ` `$i` `== 0) ` ` ` `{ ` ` ` ` ` `// l >= sqrt(area) >= i ` ` ` `$l` `= ` `floor` `(` `$area` `/ ` `$i` `); ` ` ` ` ` `// so here l is +ve always ` ` ` `$b` `= ` `$i` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Here l and b are length and ` ` ` `// breadth of the rectangle ` ` ` `echo` `"l = "` `, ` `$l` `, ` `", b = "` `, ` `$b` `, ` `"\n"` `; ` `} ` ` ` `// Driver code ` `$area` `= 99; ` `find_rectangle(` `$area` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

l = 11, b = 9

**Time Complexity:** O()

## Recommended Posts:

- Number of squares of side length required to cover an N*M rectangle
- Length and Breadth of rectangle such that ratio of Area to diagonal^2 is maximum
- Minimum squares to evenly cut a rectangle
- Minimum squares to cover a rectangle
- Find minimum area of rectangle with given set of coordinates
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Minimum length of the shortest path of a triangle
- Minimum length of square to contain at least half of the given Coordinates
- Minimum absolute difference between N and a power of 2
- Minimum difference between groups of size two
- Divide 1 to n into two groups with minimum sum difference
- Sum of minimum difference between consecutive elements of an array
- Minimum sum of absolute difference of pairs of two arrays
- Minimize the difference between minimum and maximum elements
- Queries to add, remove and return the difference of maximum and minimum.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.