Open In App

Find Maximum number of intersecting points with at most K distance

Last Updated : 05 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Jumps-(1)-(1)

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;
 
// Euclidean algorithm for finding GCD
long long gcd(long long a, long long b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
 
// Method to calculate GCD for all the elements in the array
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;
}
 
// Method for finding distinct possible points
void Intersecting_points(vector<long long>& jumpA, vector<long long>& jumpB, long long K) {
    // Calculations as provided in the approach
    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;
 
    // Number of intersecting points
    cout << L << endl;
}
 
int main() {
    vector<long long> jumpA = {1, 5};
    vector<long long> jumpB = {6, 8};
    long long K = 5;
 
    // Function call
    Intersecting_points(jumpA, jumpB, K);
 
    return 0;
}


Java




// Java code to implement the approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Driver Class
class Main {
    // Driver Function
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Inputs
        long jumpA[] = { 1, 5 };
        long jumpB[] = { 6, 8 };
        long K = 5L;
 
        // Function call
        Intersecting_points(jumpA, jumpB, K);
    }
 
    // Euclidean algorithm for finding GCD
    public static long gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Method to calculate GCD for all
    // the elements in the array
    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;
    }
 
    // Method for finding distinct
    // possible points
    public static void
    Intersecting_points(long jumpA[], long jumpB[], long K)
    {
 
        // Calculations as provided
        // in the Approach
        long X = gcd_array(jumpA);
        long Y = gcd_array(jumpB);
        long LCM = (X * Y) / gcd(X, Y);
        long L = 2 * (K / LCM) + 1;
 
        // Number of intersecting points
        System.out.println(L);
    }
}


Python




# Python code for the above approach
 
# Euclidean algorithm for finding GCD
 
 
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
 
# Method to calculate GCD for all the elements in the array
 
 
def gcd_array(arr):
    ans = arr[0]
    for i in range(1, len(arr)):
        ans = gcd(ans, arr[i])
    return ans
 
# Method for finding distinct possible points
 
 
def intersecting_points(jumpA, jumpB, K):
    # Calculations as provided in the approach
    X = gcd_array(jumpA)
    Y = gcd_array(jumpB)
    LCM = (X * Y) // gcd(X, Y)
    L = 2 * (K // LCM) + 1
    # Number of intersecting points
    print(L)
 
 
# Driver code
jumpA = [1, 5]
jumpB = [6, 8]
K = 5
 
# Function call
intersecting_points(jumpA, jumpB, K)
 
# This code is contributed by Abhinav Mahajan (abhinav_m22)


C#




using System;
class GFG
{
    // Driver Function
    public static void Main(string[] args)
    {
        // Inputs
        long[] jumpA = { 1, 5 };
        long[] jumpB = { 6, 8 };
        long K = 5L;
 
        // Function call
        IntersectingPoints(jumpA, jumpB, K);
    }
 
    // Euclidean algorithm for finding GCD
    public static long GCD(long a, long b)
    {
        if (b == 0)
            return a;
        return GCD(b, a % b);
    }
 
    // Method to calculate GCD for all
    // the elements in the array
    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;
    }
 
    // Method for finding distinct
    // possible points
    public static void IntersectingPoints(long[] jumpA, long[] jumpB, long K)
    {
        // Calculations as provided
        // in the Approach
        long X = GCDArray(jumpA);
        long Y = GCDArray(jumpB);
        long LCM = (X * Y) / GCD(X, Y);
        long L = 2 * (K / LCM) + 1;
 
        // Number of intersecting points
        Console.WriteLine(L);
    }
}


Javascript




// Euclidean algorithm for finding GCD
function gcd(a, b) {
    if (b === 0) return a;
    return gcd(b, a % b);
}
 
// Function to calculate GCD for all the elements in the array
function gcdArray(arr) {
    let ans = arr[0];
    for (let i = 1; i < arr.length; i++) {
        ans = gcd(ans, arr[i]);
    }
    return ans;
}
 
// Function for finding distinct possible points
function intersectingPoints(jumpA, jumpB, K) {
    // Calculations as provided in the approach
    const X = gcdArray(jumpA);
    const Y = gcdArray(jumpB);
    const LCM = (X * Y) / gcd(X, Y);
    const L = 2 * Math.floor(K / LCM) + 1;
 
    // Number of intersecting points
    console.log(L);
}
 
// Driver code
const jumpA = [1, 5];
const jumpB = [6, 8];
const K = 5;
 
// Function call
intersectingPoints(jumpA, jumpB, K);


Output

5







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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads