Number of Integral Points between Two Points
Last Updated :
04 Jul, 2024
Given two points p (x1, y1) and q (x2, y2), calculate the number of integral points lying on the line joining them.
Input: x1 = 2, y1 = 2, x2 = 5, y2 = 5
Output: 2
Explanation: There are only 2 integral points on the line joining (2,2) and (5,5). The points are (3,3) and (4,4).
Input: x1 = 1, y1 = 9, x2 = 8, y2 = 16
Output: 6
Explanation: There are 6 integral points on the line joining (1,9) and (8,16).
More Example: If points are (0, 2) and (4, 0), then the number of integral points lying on it is only one and that is (2, 1).
Similarly, if points are (1, 9) and (8, 16), the integral points lying on it are 6 and they are (2, 10), (3, 11), (4, 12), (5, 13), (6, 14) and (7, 15).
Simple Approach
Start from any of the given points, reach the other end point by using loops. For every point inside the loop, check if it lies on the line that joins given two points. If yes, then increment the count by 1. Time Complexity for this approach will be O(min(x2-x1, y2-y1)).
Optimal Approach
1. If the edge formed by joining p and q is parallel
to the Y-axis, then the number of integral points
between the vertices is :
abs(p.y - q.y)-1
2. Similarly if edge is parallel to the X-axis, then
the number of integral points in between is :
abs(p.x - q.x)-1
3. Else, we can find the integral points between the
vertices using below formula:
GCD(abs(p.x - q.x), abs(p.y - q.y)) - 1
How does the GCD formula work?
The idea is to find the equation of the line in simplest form, i.e., in equation ax + by +c, coefficients a, b and c become co-prime. We can do this by calculating the GCD (greatest common divisor) of a, b and c and convert a, b and c in the simplest form.
Then, the answer will be (difference of y coordinates) divided by (a) – 1. This is because after calculating ax + by + c = 0, for different y values, x will be number of y values which are exactly divisible by a.
Below is the implementation of above idea.
C++
// C++ code to find the number of integral points
// lying on the line joining the two given points
#include <iostream>
#include <cmath>
using namespace std;
// Class to represent an Integral point on XY plane.
class Point
{
public:
int x, y;
Point(int a=0, int b=0):x(a),y(b) {}
};
// Utility function to find GCD of two numbers
// GCD of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
// Finds the no. of Integral points between
// two given points.
int getCount(Point p, Point q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x==q.x)
return abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
if (p.y == q.y)
return abs(p.x-q.x) - 1;
return gcd(abs(p.x-q.x), abs(p.y-q.y))-1;
}
// Driver program to test above
int main()
{
Point p(1, 9);
Point q(8, 16);
cout << "The number of integral points between "
<< "(" << p.x << ", " << p.y << ") and ("
<< q.x << ", " << q.y << ") is "
<< getCount(p, q);
return 0;
}
Java
// Java code to find the number of integral points
// lying on the line joining the two given points
class GFG
{
// Class to represent an Integral point on XY plane.
static class Point
{
int x, y;
Point(int a, int b)
{
this.x = a;
this.y = b;
}
};
// Utility function to find GCD of two numbers
// GCD of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Finds the no. of Integral points between
// two given points.
static int getCount(Point p, Point q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x == q.x)
return Math.abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
if (p.y == q.y)
return Math.abs(p.x - q.x) - 1;
return gcd(Math.abs(p.x - q.x), Math.abs(p.y - q.y)) - 1;
}
// Driver program to test above
public static void main(String[] args)
{
Point p = new Point(1, 9);
Point q = new Point(8, 16);
System.out.println("The number of integral points between "
+ "(" + p.x + ", " + p.y + ") and ("
+ q.x + ", " + q.y + ") is "
+ getCount(p, q));
}
}
// This code contributed by Rajput-Ji
Python
# Python3 code to find the number of
# integral points lying on the line
# joining the two given points
# Class to represent an Integral point
# on XY plane.
class Point:
def __init__(self, a, b):
self.x = a
self.y = b
# Utility function to find GCD
# of two numbers GCD of a and b
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
# Finds the no. of Integral points
# between two given points.
def getCount(p, q):
# If line joining p and q is parallel
# to x axis, then count is difference
# of y values
if p.x == q.x:
return abs(p.y - q.y) - 1
# If line joining p and q is parallel
# to y axis, then count is difference
# of x values
if p.y == q.y:
return abs(p.x - q.x) - 1
return gcd(abs(p.x - q.x),
abs(p.y - q.y)) - 1
# Driver Code
if __name__ == "__main__":
p = Point(1, 9)
q = Point(8, 16)
print("The number of integral points",
"between ({}, {}) and ({}, {}) is {}" .
format(p.x, p.y, q.x, q.y, getCount(p, q)))
# This code is contributed by Rituraj Jain
C#
// C# code to find the number of integral points
// lying on the line joining the two given points
using System;
class GFG
{
// Class to represent an Integral point on XY plane.
public class Point
{
public int x, y;
public Point(int a, int b)
{
this.x = a;
this.y = b;
}
};
// Utility function to find GCD of two numbers
// GCD of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Finds the no. of Integral points between
// two given points.
static int getCount(Point p, Point q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x == q.x)
return Math.Abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
if (p.y == q.y)
return Math.Abs(p.x - q.x) - 1;
return gcd(Math.Abs(p.x - q.x), Math.Abs(p.y - q.y)) - 1;
}
// Driver code
public static void Main(String[] args)
{
Point p = new Point(1, 9);
Point q = new Point(8, 16);
Console.WriteLine("The number of integral points between "
+ "(" + p.x + ", " + p.y + ") and ("
+ q.x + ", " + q.y + ") is "
+ getCount(p, q));
}
}
/* This code contributed by PrinciRaj1992 */
JavaScript
<script>
// javascript code to find the number of integral points
// lying on the line joining the two given points
// Class to represent an Integral point on XY plane.
class Point {
constructor(a , b) {
this.x = a;
this.y = b;
}
}
// Utility function to find GCD of two numbers
// GCD of a and b
function gcd(a , b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// Finds the no. of Integral points between
// two given points.
function getCount( p, q)
{
// If line joining p and q is parallel to
// x axis, then count is difference of y
// values
if (p.x == q.x)
return Math.abs(p.y - q.y) - 1;
// If line joining p and q is parallel to
// y axis, then count is difference of x
// values
if (p.y == q.y)
return Math.abs(p.x - q.x) - 1;
return gcd(Math.abs(p.x - q.x), Math.abs(p.y - q.y)) - 1;
}
// Driver program to test above
p = new Point(1, 9);
q = new Point(8, 16);
document.write("The number of integral points between " + "(" + p.x + ", " + p.y + ") and (" + q.x + ", "
+ q.y + ") is " + getCount(p, q));
// This code is contributed by gauravrajput1
</script>
Output:
The number of integral points between (1, 9) and (8, 16) is 6
Time Complexity: O(log(min(a,b))), as we are using recursion to find the GCD.
Auxiliary Space: O(log(min(a,b))), for recursive stack space.
Reference :
https://www.geeksforgeeks.org/count-integral-points-inside-a-triangle/
This article has been contributed by Paridhi Johari.
Similar Reads
Number of perfect squares between two given numbers Given two numbers a and b where 1<=a<=b, find the number of perfect squares between a and b (a and b inclusive).ExampleInput: a = 3, b = 8Output: 1Explanation: The only perfect square in given range is 4.Input: a = 9, b = 25Output: 3Explanation: The three perfect squares in given range are 9,
4 min read
Number of perfect cubes between two given numbers Given two given numbers a and b where 1<=a<=b, find the number of perfect cubes between a and b (a and b inclusive).Examples: Input : a = 3, b = 16 Output : 1 The only perfect cube in given range is 8. Input : a = 7, b = 30 Output : 2 The two cubes in given range are 8, and 27 Method 1 : One n
6 min read
Number of pairs of lines having integer intersection points Given two integer arrays P[] and Q[], where pi and qj for each 0 <= i < size(P) and 0 <= j < size(Q) represents the line equations x - y = -pi and x + y = qj respectively. The task is to find the number of pairs from P[] and Q[] having integer intersection points. Examples: Input: P[] =
5 min read
Prime points (Points that split a number into two primes) Given a n-digit number. Prime point is the index of the digit whose left and right side numbers are prime. Print all the prime points of the number. If no prime point exists print -1. Examples: Input : 2317 Output : 1 2 Explanation : Left and right side numbers of index point 1 are 2 and 17 respecti
10 min read
Number of triangles that can be formed with given N points Given X and Y coordinates of N points on a Cartesian plane. The task is to find the number of possible triangles with the non-zero area that can be formed by joining each point to every other point. Examples: Input: P[] = {{0, 0}, {2, 0}, {1, 1}, {2, 2}} Output: 3 Possible triangles can be [(0, 0},
9 min read