# Determine the number of squares of unit area that a given line will pass through.

Given 2 endpoints **(x1, y1)** and **(x2, y2)** of a line, the task is to determine the number of squares of the unit area that line will pass through.

**Examples:**

Input:(x1 = 1, y1 = 1), (x2 = 4, y2 = 3)

Output:4

In the diagram above the line is passing through 4 squares

Input:(x1 = 0, y1 = 0), (x2 = 2, y2 = 2)

Output:2

**Approach**:

Let,

Dx = (x2 - x1) Dy = (y2 - y1)

Therefore,

x = x1 + Dx * t y = y1 + Dy * t

We have to find (x, y) for t in (0, 1].

For, x and y to be integral Dx and Dy must be divisible by t. Also, t cannot be irrational since Dx and Dy are integers.

Therefore let **t = p / q**.

Dx and Dy must be divisible by q. So GCD of Dx and Dy must be q.

Or, q = GCD(Dx, Dy).

There are only GCD(Dx, Dy) smallest subproblems.

Below is the implementation of the above approach:

## C++

`#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required position ` `int` `noOfSquares(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `int` `dx = ` `abs` `(x2 - x1); ` ` ` `int` `dy = ` `abs` `(y2 - y1); ` ` ` ` ` `int` `ans = dx + dy - __gcd(dx, dy); ` ` ` ` ` `cout<<ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `x1 = 1, y1 = 1, x2 = 4, y2 = 3; ` ` ` ` ` `noOfSquares(x1, y1, x2, y2); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to determine the numer ` `// of squares that line will pass through ` `class` `GFG ` `{ ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` `// Function to return the required position ` `static` `void` `noOfSquares(` `int` `x1, ` `int` `y1, ` ` ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `int` `dx = Math.abs(x2 - x1); ` ` ` `int` `dy = Math.abs(y2 - y1); ` ` ` ` ` `int` `ans = dx + dy - __gcd(dx, dy); ` ` ` ` ` `System.out.println(ans); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `x1 = ` `1` `, y1 = ` `1` `, x2 = ` `4` `, y2 = ` `3` `; ` ` ` ` ` `noOfSquares(x1, y1, x2, y2); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to determine the number ` `# of squares that line will pass through ` `from` `math ` `import` `gcd ` ` ` `# Function to return the required position ` `def` `noOfSquares(x1, y1, x2, y2) : ` ` ` ` ` `dx ` `=` `abs` `(x2 ` `-` `x1); ` ` ` `dy ` `=` `abs` `(y2 ` `-` `y1); ` ` ` ` ` `ans ` `=` `dx ` `+` `dy ` `-` `gcd(dx, dy); ` ` ` ` ` `print` `(ans); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `x1 ` `=` `1` `; y1 ` `=` `1` `; x2 ` `=` `4` `; y2 ` `=` `3` `; ` ` ` ` ` `noOfSquares(x1, y1, x2, y2); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` `// Function to return the required position ` `static` `void` `noOfSquares(` `int` `x1, ` `int` `y1, ` ` ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `int` `dx = Math.Abs(x2 - x1); ` ` ` `int` `dy = Math.Abs(y2 - y1); ` ` ` ` ` `int` `ans = dx + dy - __gcd(dx, dy); ` ` ` ` ` `Console.WriteLine(ans); ` `} ` ` ` `// Driver Code ` `static` `void` `Main() ` `{ ` ` ` `int` `x1 = 1, y1 = 1, x2 = 4, y2 = 3; ` ` ` ` ` `noOfSquares(x1, y1, x2, y2); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to determine the numer ` `// of squares that line will pass through ` ` ` `// Function to return the required position ` `function` `noOfSquares(` `$x1` `, ` `$y1` `, ` `$x2` `, ` `$y2` `) ` `{ ` ` ` ` ` `$dx` `= ` `abs` `(` `$x2` `- ` `$x1` `); ` ` ` `$dy` `= ` `abs` `(` `$y2` `- ` `$y1` `); ` ` ` ` ` `$ans` `= ` `$dx` `+ ` `$dy` `- gcd(` `$dx` `, ` `$dy` `); ` ` ` ` ` `echo` `(` `$ans` `); ` `} ` ` ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` `return` `gcd(` `$b` `, ` `$a` `% ` `$b` `); ` ` ` `} ` ` ` `// Driver Code ` `$x1` `= 1; ` `$y1` `= 1; ` `$x2` `= 4; ` `$y2` `= 3; ` `noOfSquares(` `$x1` `, ` `$y1` `, ` `$x2` `, ` `$y2` `); ` ` ` `// This code has been contributed ` `// by 29AjayKumar ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(1)

## Recommended Posts:

- Number of unique rectangles formed using N unit squares
- Number of squares of maximum area in a rectangle
- Area of triangle formed by the axes of co-ordinates and a given straight line
- Determine whether a given number is a Hyperperfect Number
- Minimum number of squares whose sum equals to given number n
- Find the number of jumps to reach X in the number line from zero
- Count number of squares in a rectangle
- Number of perfect squares between two given numbers
- Check whether a number can be represented by sum of two squares
- Square pyramidal number (Sum of Squares)
- Paper Cut into Minimum Number of Squares
- Number of ways of writing N as a sum of 4 squares
- Sum of the count of number of adjacent squares in an M X N grid
- Count number less than N which are product of perfect squares
- Program to find number of squares in a chessboard

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.