# Problem: Generating Rectangles

The next sample exam problem is about using nested loops and program logic to generate all possible rectangles, which have integer coordinates in given range and given minimum area.

## Video: Generating Rectangles

Watch the video lesson about solving the "Generating Rectangles" problem: https://youtu.be/gk1IOpMXpCc.

## Problem Description

By a given number n and a minimum area m, generate all possible rectangles with integer coordinates in the range [-n…n] with an area of at least m. The generated rectangles must be printed in the following format:

• (left, top) (right, bottom) -> area

Rectangles are defined using the top left and bottom right corner. The following inequalities are in effect:

• -n ≤ left < right ≤ n
• -n ≤ top < bottom ≤ n

## Input

Two numbers, one per line, are entered from the console:

• An integer n in the range [1 … 100] – sets the minimum and maximum coordinates of a peak.
• An integer m in the range [0 … 50 000] – sets the minimum area of the generated rectangles.

## Output

• The described rectangles should be printed on the console in a format such as in the examples below.
• If there are no rectangles for the specified n and m, then print "No".
• The order of rectangles in the output is not important, so use and order of your choice.

## Sample Input and Output

Input Output Input Output
1
2
(-1, -1) (0, 1) -> 2
(-1, -1) (1, 0) -> 2
(-1, -1) (1, 1) -> 4
(-1, 0) (1, 1) -> 2
(0, -1) (1, 1) -> 2
2
17
No
Input Output
3
36
(-3, -3) (3, 3) -> 36

Read the input data from the console. We will also create a counter, which will store the number of rectangles found.

## Sample Idea for the Solution

It is very important to be able to imagine the problem before we begin to solve it. In our case it is required to search for rectangles in a coordinate system. The thing we know is that the left point will always have the coordinate x, smaller than the right one. Accordingly, the upper one will always have a smaller y coordinate than the lower one. To find all the rectangles, we'll have to create a loop similar to the previous problem, but this time, not every next loop will start from the next number because some of the coordinates can be equal (for example left and top).

With the variables left and right we will follow the coordinates horizontally and with top and bottomvertically.

## Calculating the Rectangle Area and Printing the Output

The important thing here is knowing the corresponding coordinates so we can correctly calculate the sides of the rectangle. Now we have to find the area of the rectangle and check if it is greater than or equal to m. One side will be the difference between left and right and the other one – between top and bottom. Since the coordinates may be eventually interchanged, we will use absolute values. Again, we add the counter in the loop, counting only the rectangles we write. It is important to note that the writing order is left, top, right, bottom, as it is set in the problem's description.

Finally, we print "No" if there are no such rectangles.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.6148353219032288, "perplexity": 704.9787612144349}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-30/segments/1563195526536.46/warc/CC-MAIN-20190720153215-20190720175215-00163.warc.gz"}