Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given two points **p** (x1, y1) and ** q **(x2, y2), Calculate the number of integral points lying on the line joining them.

**Note: **You are given the 4 points x1, y1, x2, y2 as Input.

**Example 1:**

Input:x1 =2,y1 =2,x2 =5,y2 =5Output:2Explanation:There are only 2 integral points on the line joining (2,2) and (5,5). The points are (3,3) and (4,4).

**Example 2:**

Input:x1 =1,y1 =9,x2 =8,y2 =16Output:6Explanation:There are 6 integral points on the line joining (1,9) and (8,16).

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **countIntegralPoints()** which takes 4 Integers x1,y1,x2 and y2 as input and returns the answer.

**Expected Time Complexity:** O(log(max(x1,x2,y1,y2)))

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= x1,x2,y1,y2 <= 10^{8}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Count Integral Points

...