VOOZH about

URL: https://www.geeksforgeeks.org/dsa/generate-all-integral-points-lying-inside-a-rectangle/

⇱ Generate all integral points lying inside a rectangle - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Generate all integral points lying inside a rectangle

Last Updated : 15 Jul, 2025

Given a rectangle of length L, and width, W, the task is to generate all integral coordinates (X, Y) that lie within a rectangle pf dimensions L * W having one of its vertexes in the origin (0, 0).

Examples:

Input: L = 3, W = 2
Output: (0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)
Explanation: Total number of integral coordinates existing within the rectangle are L × W = 6. Therefore, the output is (0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1).

Input: L = 5, W = 3
Output: (0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2) (2, 0) (2, 1) (2, 2) (3, 0) (3, 1) (3, 2) (4, 0) (4, 1) (4, 2)

Approach: The problem can be solved by generating all integral numbers from the range 0 to L for X-coordinates and from 0 to W for Y-coordinates using the rand() function. Follow the steps below to solve the problem:

  1. Create a set of pairs to store all the coordinates(X, Y) that lie within the rectangle.
  2. Use the equation rand() % L to generate all the integers lying between 0 to L and rand() % W to generate all the integers lying between 0 to W.
  3. Print all possible L × W coordinates (X, Y) that lie within the rectangle.

Below is the implementation of the above approach: 


Output: 
(0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)

 

Time Complexity: O(L * W) 
Auxiliary Space: O(L * W)
 

Comment