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 the 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| are as minimal 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 up to. 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 possiblevoid 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 codeint main(){Â Â Â Â int area = 99;Â Â Â Â find_rectangle(area);Â Â Â Â return 0;} |
Java
// Java implementation of the approachclass 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. |
Python3
# Python3 implementation of the approachimport math as mtÂ
# Function to print the length (l)# and breadth (b) of the rectangle # having area = N and |l - b| as # minimum as possibledef 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 codearea = 99find_rectangle(area)Â
# This code is contributed by # Mohit kumar 29 |
C#
// C# implementation of the approachusing 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?> |
Javascript
<script>Â
// Javascript 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)    {        let l = 0, b = 0;        let M = Math.floor(Math.sqrt(area)), ans;Â
        for (let i = M; i >= 1; i--) {Â
            // i is a factor            if (area % i == 0) {Â
                // l >= sqrt(area) >= i                l = Math.floor(area / i);Â
                // so here l is +ve always                b = i;                break;            }        }Â
        // Here l and b are length and        // breadth of the rectangle        document.write("l = " + l + ", b = " + b);    }Â
// Driver codeÂ
    let area = 99;    find_rectangle(area);     </script> |
l = 11, b = 9
Â
Time Complexity: O()
Auxiliary Space: O(1)
Below is a simple implementation. Â
CPP
// 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 possiblevoid find_rectangle(int area){Â Â Â Â for (int i = ceil(sqrt(area)); i <= area; i++) {Â Â Â Â Â Â Â Â if (area / i * i == area) {Â Â Â Â Â Â Â Â Â Â Â Â printf("%d %d", i, area / i);Â Â Â Â Â Â Â Â Â Â Â Â return;Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
// Driver codeint main(){Â Â Â Â int area = 99;Â Â Â Â find_rectangle(area);Â Â Â Â return 0;} |
Java
// Java implementation of the approachimport java.io.*;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)    {        for(int i = (int)Math.ceil(Math.sqrt(area)); i <= area; i++)        {            if(area / i * i == area)            {                System.out.println(i + " " + (int)(area / i));                return;            }        }    }       // Driver code    public static void main (String[] args)    {        int area = 99;        find_rectangle(area);           }}Â
// This code is contributed by rag2127 |
Python3
# Python3 implementation of the approach import mathÂ
# Function to print the length (l) # and breadth (b) of the rectangle # having area = N and |l - b| as # minimum as possible def find_rectangle(area):Â Â Â Â for i in range(int(math.ceil(math.sqrt(area))) , area + 1):Â Â Â Â Â Â Â Â if((int(area / i) * i) == area):Â Â Â Â Â Â Â Â Â Â Â Â print(i, int(area / i))Â Â Â Â Â Â Â Â Â Â Â Â returnÂ
# Driver code area = 99find_rectangle(area)Â
# This code is contributed by avanitrachhadiya2155 |
C#
// C# implementation of the approachusing 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)  {    for(int i = (int)Math.Ceiling(Math.Sqrt(area)); i <= area; i++)    {      if(area / i * i == area)      {        Console.WriteLine(i + " " + (int)(area / i));        return;      }    }  }Â
  // Driver code  static void Main()  {    int area = 99;    find_rectangle(area);  }}Â
// This code is contributed by divyeshrabadiya07. |
Javascript
<script>// Javascript 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 possiblefunction find_rectangle(are){Â Â Â Â for(let i = Math.floor(Math.ceil(Math.sqrt(area))); i <= area; i++)Â Â Â Â Â Â Â Â {Â Â Â Â Â Â Â Â Â Â Â Â if(Math.floor(area / i) * i == area)Â Â Â Â Â Â Â Â Â Â Â Â {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â document.write(i + " " + Math.floor(area / i));Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return;Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }}Â
 // Driver codelet area = 99;find_rectangle(area);  Â
Â
Â
// This code is contributed by unknown2108</script> |
11 9
Â
Time Complexity: O(log(area)), due to inbuild function sqrt()
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



