Number of pairs of lines having integer intersection points
Last Updated :
20 Feb, 2022
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[] = {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 x - y = -p and 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 p0 and p1 be the number of even and odd pi respectively.
- Similarly, q0 and q1 for the number of even and odd qi respectively.
- Therefore the required answers is p0 * q0 + p1 * q1.
Below is the implementation of the above approach:
C++
// 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;
}
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
Python3
# 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
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
JavaScript
<script>
// Javascript program to Number of
// pairs of lines having integer
// intersection points
// Count number of pairs of lines
// having integer intersection point
function countPairs(P, Q, N, M)
{
// Initialize arrays to store counts
var A = [0, 0], B = [0, 0];
// Count number of odd and even Pi
for(var i = 0; i < N; i++)
A[P[i] % 2]++;
// Count number of odd and even Qi
for(var 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
var P = [ 1, 3, 2 ], Q = [ 3, 0 ];
var N = P.length;
var M = Q.length;
document.write(countPairs(P, Q, N, M));
// This code is contributed by rrrtnx
</script>
Time Complexity: O(P + Q)
Auxiliary Space: O(1)
Similar Reads
Find intersection point of lines inside a section Given N lines in two-dimensional space in y = mx + b form and a vertical section. We need to find out whether there is an intersection point inside the given section or not. Examples: In below diagram four lines are there, L1 : y = x + 2 L2 : y = -x + 7 L3 : y = -3 L4 : y = 2x â 7 and vertical secti
9 min read
Maximum points of intersection n lines You are given n straight lines. You have to find a maximum number of points of intersection with these n lines.Examples: Input : n = 4 Output : 6 Input : n = 2Output :1 Approach : As we have n number of line, and we have to find the maximum point of intersection using this n line. So this can be don
3 min read
Count number of intersections points for given lines between (i, 0) and (j, 1) Given an array, lines[] of N pairs of the form (i, j) where (i, j) represents a line segment from coordinate (i, 0) to (j, 1), the task is to find the count of points of intersection of the given lines. Example: Input: lines[] = {{1, 2}, {2, 1}}Output: 1Explanation: For the given two pairs, the line
9 min read
Maximum number of line intersections formed through intersection of N planes Given N planes. The task is to find the maximum number of line intersections that can be formed through the intersections of N planes.Examples: Input: N = 3 Output: 3Input: N = 5 Output: 10 Approach:Let there be N planes such that no 3 planes intersect in a single line of intersection and no 2 plane
3 min read
Number of intersections between two ranges Given N ranges of type1 ranges and M ranges of type2.The task is to find the total number of intersections between all possible type1 and type2 range pairs. All start and end points of type1 and type2 ranges are given.Examples: Input : N = 2, M = 2 type1[ ] = { { 1, 3 }, { 5, 9 } } type2[ ] = { { 2,
8 min read