Find Maximum number of intersecting points with at most K distance
Last Updated :
05 Dec, 2023
Two players A and B are standing at starting point 0 of an infinite horizontal line. Player A is given an array with jumpA[] of size M and Player B is given an array with jumpB[] of size N. Both players can make jumps as:
- Player A can jump D distance in both directions (right or left), from its current point, such that D is present in jumpA[].
- Player B can jump D distance in both directions (right or left), from its current point, such that D is present in jumpB[].
Calculate the maximum number of intersection points on the horizontal line, which are not far from K distance from the starting point. Formally, all intersection points between +K and -K are on a horizontal line.
Examples:
Input: jumpA[] = {2, 4}, jumpB[] = {8, 10}, K = 2
Output: 3
Explanation: There can be maximum three intersections points between +K and -K or between [-2, 2] on horizontal line and those 3 points are -2, 0 and 2. Both players are initially at point 0. Player A can move 2 or 4 distance left or right and Player B can move 8 or 10 distance right or left.
- First Intersection point = (-2): Player A moves 4 distances left and then 2 distances right, Then it will be at -2 at horizontal line. Same Player B will jump 10 distances left and 8 distance right, After that B will be at -2 on horizontal line. So the both intersects at -2.
- Second Intersection point = (0): Player A moves 4 distances left and then 4 distances right, Then it will be again same at initial position. Player B will jump 10 distances left and 10 distance right, After that B will be at 0 on horizontal line. So the both intersects at 0.
- Third Intersection point = (2): Player A moves 4 distances right and then 2 distances left, Then it will be at 2 at horizontal line. Player B will jump 10 distances right and 8 distance left, After that B will be at 2 on horizontal line. So the both intersects at 2.
Therefore, there will be total 3 intersecting points, -2, 0 and 2. These all points are in the range of -K and +K or [-2, 2]. So output is 3.
Input: jumpA[] = {1}, jumpB[] = {2}, K = 3
Output: 5
Explanation: It can be verified that there will be maximum 5 points of intersection for both the players A and B.
Approach: Implement the idea below to solve the problem
The problem is based on the Number Theory and can be solved by calculating the GCD of the given inputs. From the observations it can be observed that:
- If we calculate the GCD over all the elements in jumpA[], let’s say X, then X is the minimum distance which Player A can move in any direction.
- Similarly, the GCD over all the elements in jumpB[], let’s say Y, then Y is the minimum distance which Player B can move in any direction.
- Now, in order to find all the intersection points we need to find the LCM of X and Y as the LCM is the first place where both the persons will intersect each other.
- LCM = (X * Y)/GCD(X, Y), In order to get all the intersection points at a distance K, we can simply divide K by the LCM of X and Y, but since the distance can be in both directions (positive as well as negative), we will have to multiply it by 2
- Total intersection points, L = 2*(K/LCM) + 1
- Here, we have added 1 to count the starting point 0.
Steps were taken to solve the problem:
- Create a variable X, and calculate the GCD of all elements in jumpA[].
- Create a variable Y, and Calculate the GCD of all elements in jumpB[].
- Create a variable LCM and initialize it equal to (X*Y)/ GCD(X, Y)
- Then update L as, L = 2*(K/LCM)+1
- Output the value of L.
Code to implement the approach:
C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
long long gcd( long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long gcd_array(vector< long long >& arr) {
long long ans = arr[0];
for ( int i = 1; i < arr.size(); i++) {
ans = gcd(ans, arr[i]);
}
return ans;
}
void Intersecting_points(vector< long long >& jumpA, vector< long long >& jumpB, long long K) {
long long X = gcd_array(jumpA);
long long Y = gcd_array(jumpB);
long long LCM = (X * Y) / gcd(X, Y);
long long L = 2 * (K / LCM) + 1;
cout << L << endl;
}
int main() {
vector< long long > jumpA = {1, 5};
vector< long long > jumpB = {6, 8};
long long K = 5;
Intersecting_points(jumpA, jumpB, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
public static void main(String[] args)
throws java.lang.Exception
{
long jumpA[] = { 1 , 5 };
long jumpB[] = { 6 , 8 };
long K = 5L;
Intersecting_points(jumpA, jumpB, K);
}
public static long gcd( long a, long b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static long gcd_array( long arr[])
{
long ans = arr[ 0 ];
for ( int i = 1 ; i < arr.length; i++) {
ans = gcd(ans, arr[i]);
}
return ans;
}
public static void
Intersecting_points( long jumpA[], long jumpB[], long K)
{
long X = gcd_array(jumpA);
long Y = gcd_array(jumpB);
long LCM = (X * Y) / gcd(X, Y);
long L = 2 * (K / LCM) + 1 ;
System.out.println(L);
}
}
|
Python
def gcd(a, b):
while b:
a, b = b, a % b
return a
def gcd_array(arr):
ans = arr[ 0 ]
for i in range ( 1 , len (arr)):
ans = gcd(ans, arr[i])
return ans
def intersecting_points(jumpA, jumpB, K):
X = gcd_array(jumpA)
Y = gcd_array(jumpB)
LCM = (X * Y) / / gcd(X, Y)
L = 2 * (K / / LCM) + 1
print (L)
jumpA = [ 1 , 5 ]
jumpB = [ 6 , 8 ]
K = 5
intersecting_points(jumpA, jumpB, K)
|
C#
using System;
class GFG
{
public static void Main( string [] args)
{
long [] jumpA = { 1, 5 };
long [] jumpB = { 6, 8 };
long K = 5L;
IntersectingPoints(jumpA, jumpB, K);
}
public static long GCD( long a, long b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static long GCDArray( long [] arr)
{
long ans = arr[0];
for ( int i = 1; i < arr.Length; i++)
{
ans = GCD(ans, arr[i]);
}
return ans;
}
public static void IntersectingPoints( long [] jumpA, long [] jumpB, long K)
{
long X = GCDArray(jumpA);
long Y = GCDArray(jumpB);
long LCM = (X * Y) / GCD(X, Y);
long L = 2 * (K / LCM) + 1;
Console.WriteLine(L);
}
}
|
Javascript
function gcd(a, b) {
if (b === 0) return a;
return gcd(b, a % b);
}
function gcdArray(arr) {
let ans = arr[0];
for (let i = 1; i < arr.length; i++) {
ans = gcd(ans, arr[i]);
}
return ans;
}
function intersectingPoints(jumpA, jumpB, K) {
const X = gcdArray(jumpA);
const Y = gcdArray(jumpB);
const LCM = (X * Y) / gcd(X, Y);
const L = 2 * Math.floor(K / LCM) + 1;
console.log(L);
}
const jumpA = [1, 5];
const jumpB = [6, 8];
const K = 5;
intersectingPoints(jumpA, jumpB, K);
|
Time Complexity: O(N*log(X) + M*log(Y)), where N is the size of jumpA[], M is the size of jumpB[], X is the largest element in jumpA[] and Y is the largest element in jumpB[].
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...