Given two integer arrays **P[]** and **Q[]**, where ** p_{i}** and

**for each**

*q*_{j}*0 <= i < size(P)*and

*0 <= j < size(Q)*represents the line equations

**and**

*x – y = -p*_{i}**respectively. The task is to find the number of pairs from**

*x + y = q*_{j}**P[]**and

**Q[]**having integer intersection points.

**Examples:**

Input:P[] = {1, 3, 2}, Q[] = {3, 0}

Output:3

The pairs of lines (p, q) having integer intersection points are (1, 3), (2, 0) and (3, 3). Here p is the line parameter of P[] and q is the that of Q[].

Input:P[] = {1, 4, 3, 2}, Q[] = {3, 6, 10, 11}

Output:8

**Approach:**

- The problem can be solved easily by solving the two equations and analyzing the condition for integer intersection points.
- The two equations are
and*x – y = -p*.*x + y = q* - Solving for
**x**and**y**we get,**x = (q-p)/2**and**y = (p+q)/2**. - It is clear that integer intersection point is possible if and only if
**p**and**q**have same parity. - Let
**p**and_{0}**p**be the number of even and odd_{1}**p**respectively._{i} - Similarly,
**q**and_{0}**q**for the number of even and odd_{1}**q**respectively._{i} - Therefore the required answers is
**p***_{0}**q**+_{0}**p***_{1}**q**._{1}

Below is the implementation of the above approach:

## CPP

`// C++ program to Number of pairs of lines ` `// having integer intersection points ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Count number of pairs of lines ` `// having integer intersection point ` `int` `countPairs(` `int` `* P, ` `int` `* Q, ` `int` `N, ` `int` `M) ` `{ ` ` ` `// Initialize arrays to store counts ` ` ` `int` `A[2] = { 0 }, B[2] = { 0 }; ` ` ` ` ` `// Count number of odd and even Pi ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `A[P[i] % 2]++; ` ` ` ` ` `// Count number of odd and even Qi ` ` ` `for` `(` `int` `i = 0; i < M; i++) ` ` ` `B[Q[i] % 2]++; ` ` ` ` ` `// Return the count of pairs ` ` ` `return` `(A[0] * B[0] + A[1] * B[1]); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `P[] = { 1, 3, 2 }, Q[] = { 3, 0 }; ` ` ` `int` `N = ` `sizeof` `(P) / ` `sizeof` `(P[0]); ` ` ` `int` `M = ` `sizeof` `(Q) / ` `sizeof` `(Q[0]); ` ` ` ` ` `cout << countPairs(P, Q, N, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Number of pairs of lines ` `// having integer intersection points ` `class` `GFG ` `{ ` ` ` `// Count number of pairs of lines ` `// having integer intersection point ` `static` `int` `countPairs(` `int` `[]P, ` `int` `[]Q, ` ` ` `int` `N, ` `int` `M) ` `{ ` ` ` `// Initialize arrays to store counts ` ` ` `int` `[]A = ` `new` `int` `[` `2` `], B = ` `new` `int` `[` `2` `]; ` ` ` ` ` `// Count number of odd and even Pi ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `A[P[i] % ` `2` `]++; ` ` ` ` ` `// Count number of odd and even Qi ` ` ` `for` `(` `int` `i = ` `0` `; i < M; i++) ` ` ` `B[Q[i] % ` `2` `]++; ` ` ` ` ` `// Return the count of pairs ` ` ` `return` `(A[` `0` `] * B[` `0` `] + A[` `1` `] * B[` `1` `]); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[]P = { ` `1` `, ` `3` `, ` `2` `}; ` ` ` `int` `[]Q = { ` `3` `, ` `0` `}; ` ` ` `int` `N = P.length; ` ` ` `int` `M = Q.length; ` ` ` ` ` `System.out.print(countPairs(P, Q, N, M)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python

`# Python3 program to Number of pairs of lines ` `# having eger ersection pos ` ` ` `# Count number of pairs of lines ` `# having eger ersection po ` `def` `countPairs(P, Q, N, M): ` ` ` ` ` `# Initialize arrays to store counts ` ` ` `A ` `=` `[` `0` `] ` `*` `2` ` ` `B ` `=` `[` `0` `] ` `*` `2` ` ` ` ` `# Count number of odd and even Pi ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `A[P[i] ` `%` `2` `] ` `+` `=` `1` ` ` ` ` `# Count number of odd and even Qi ` ` ` `for` `i ` `in` `range` `(M): ` ` ` `B[Q[i] ` `%` `2` `] ` `+` `=` `1` ` ` ` ` `# Return the count of pairs ` ` ` `return` `(A[` `0` `] ` `*` `B[` `0` `] ` `+` `A[` `1` `] ` `*` `B[` `1` `]) ` ` ` `# Driver code ` ` ` `P ` `=` `[` `1` `, ` `3` `, ` `2` `] ` `Q ` `=` `[` `3` `, ` `0` `] ` `N ` `=` `len` `(P) ` `M ` `=` `len` `(Q) ` ` ` `print` `(countPairs(P, Q, N, M)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Number of pairs of lines ` `// having integer intersection points ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Count number of pairs of lines ` ` ` `// having integer intersection point ` ` ` `static` `int` `countPairs(` `int` `[]P, ` `int` `[]Q, ` ` ` `int` `N, ` `int` `M) ` ` ` `{ ` ` ` `// Initialize arrays to store counts ` ` ` `int` `[]A = ` `new` `int` `[2]; ` ` ` `int` `[]B = ` `new` `int` `[2]; ` ` ` ` ` `// Count number of odd and even Pi ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `A[P[i] % 2]++; ` ` ` ` ` `// Count number of odd and even Qi ` ` ` `for` `(` `int` `i = 0; i < M; i++) ` ` ` `B[Q[i] % 2]++; ` ` ` ` ` `// Return the count of pairs ` ` ` `return` `(A[0] * B[0] + A[1] * B[1]); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]P = { 1, 3, 2 }; ` ` ` `int` `[]Q = { 3, 0 }; ` ` ` `int` `N = P.Length; ` ` ` `int` `M = Q.Length; ` ` ` ` ` `Console.Write(countPairs(P, Q, N, M)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(P + Q)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum points of intersection n lines
- Number of lines from given N points not parallel to X or Y axis
- Number of triangles formed from a set of points on three lines
- Python - Find the maximum number of triangles with given points on three lines
- Count number of pairs of lines intersecting at a Point
- Program for Point of Intersection of Two Lines
- Find intersection point of lines inside a section
- Maximum points of intersection n circles
- Minimum lines to cover all points
- Non-crossing lines to connect points in a circle
- Find whether only two parallel lines contain all coordinates points or not
- Count of different straight lines with total n points with m collinear
- Maximum points of intersections possible among X circles and Y straight lines
- Minimum number of points to be removed to get remaining points on one side of axis
- Prime points (Points that split a number into two primes)
- Find the integer points (x, y) with Manhattan distance atleast N
- Sum of Manhattan distances between all pairs of points
- Number of Integral Points between Two Points
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Count pairs in Array whose product is a Kth power of any positive integer

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.