Open In App

Reach the numbers by making jumps of two given lengths

Last Updated : 12 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given integers k, d1, d2 and an integer array arr[]. Starting from number k you are allowed to make jumps of size d1 and d2 i.e. all the possible moves from k are: 

  • k + d1 and k – d1
  • k + d2 and k – d2

The task is to find which of the numbers from the array are reachable from k making any number of jumps and any given jump is either of size d1 or d2. For every number print 1 if its reachable else print 0.

Examples: 

Input: k = 10, d1 = 4, d2 = 6, arr[] = {10, 15, 20} 
Output: 1 0 1 
10 can be reached from k with no extra move. 
20 can be reached with k + d1 + d2 = 10 + 4 + 6 = 20

Input: k = 8, d1 = 3, d2 = 2, arr[] = {9, 4} 
Output: 1 1  

Approach: 

Any number x that is reachable from k with jumps d1 or d2 will be of the form x = k + (i * d1) + (j * d2) where i and j are integers. 
Let the GCD of d1 and d2 be gcd. Since, gcd divides both d1 and d2. Therefore we can write d1 = m1 * gcd and d2 = m2 * gcd where m1 and m2 are integers 
And x = k + gcd * (i * m1 + j * m2) = k + M * gcd
So, any number x that is reachable from k should satisfy (x – k) % gcd = 0.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <algorithm>
#include <iostream>
using namespace std;
 
// Function that returns the vector containing the
// result for the reachability of the required numbers
void reachTheNums(int nums[], int k, int d1, int d2, int n)
{
    int i, ans[n] = { 0 };
    int gcd = __gcd(d1, d2);
 
    for (i = 0; i < n; i++) {
        int x = nums[i] - k;
 
        // If distance x is coverable
        if (x % gcd == 0)
            ans[i] = 1;
        else
            ans[i] = 0;
    }
 
    for (i = 0; i < n; i++)
        cout << ans[i] << " ";
}
 
// Driver code
int main()
{
    // Numbers to be checked for reachability
    int nums[] = { 9, 4 };
    int n = sizeof(nums) / sizeof(nums[0]);
    // Starting number K
    int k = 8;
 
    // Sizes of jumps d1 and d2
    int d1 = 3, d2 = 2;
 
    reachTheNums(nums, k, d1, d2, n);
 
    return 0;
}


Java




// Java implementation of the approach
 
import java.io.*;
class GFG {
// Recursive function to return gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0 
        if (a == 0)
          return b;
        if (b == 0)
          return a;
        
        // base case
        if (a == b)
            return a;
        
        // a is greater
        if (a > b)
            return __gcd(a-b, b);
        return __gcd(a, b-a);
    }
 
 
     
 
// Function that returns the vector containing the
// result for the reachability of the required numbers
static void reachTheNums(int nums[], int k, int d1, int d2, int n)
{
    int i;
    int ans[] = new int[n];
    int gcd = __gcd(d1, d2);
 
    for (i = 0; i < n; i++) {
        int x = nums[i] - k;
 
        // If distance x is coverable
        if (x % gcd == 0)
            ans[i] = 1;
        else
            ans[i] = 0;
    }
 
    for (i = 0; i < n; i++)
        System.out.print(ans[i] + " ");
}
 
// Driver code
 
 
    public static void main (String[] args) {
        // Numbers to be checked for reachability
    int nums[] = { 9, 4 };
    int n =nums.length;
    // Starting number K
    int k = 8;
 
    // Sizes of jumps d1 and d2
    int d1 = 3, d2 = 2;
 
    reachTheNums(nums, k, d1, d2, n);
    }
}
 
// This code is contributed by inder_verma..


Python3




# Python3 implementation of the approach
import math as mt
 
# Function that returns the vector
# containing the result for the reachability
# of the required numbers
def reachTheNums(nums, k, d1, d2, n):
 
    ans = [0 for i in range(n)]
 
    gcd = mt.gcd(d1, d2)
 
    for i in range(n):
        x = nums[i] - k
 
        # If distance x is coverable
        if (x % gcd == 0):
            ans[i] = 1
        else:
            ans[i] = 0
 
    for i in range(n):
        print(ans[i], end = " ")
 
# Driver code
 
# Numbers to be checked for
# reachability
nums = [9, 4]
n = len(nums)
 
# Starting number K
k = 8
 
# Sizes of jumps d1 and d2
d1, d2 = 3, 2
 
reachTheNums(nums, k, d1, d2, n)
 
# This code is contributed
# by mohit kumar 29


C#




// C# implementation of the above approach
 
using System ;
 
class GFG {
     
    // Recursive function to return gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0
        if (a == 0)
        return b;
        if (b == 0)
        return a;
         
        // base case
        if (a == b)
            return a;
         
        // a is greater
        if (a > b)
            return __gcd(a-b, b);
             
        return __gcd(a, b-a);
    }
 
 
     
 
    // Function that returns the vector containing the
    // result for the reachability of the required numbers
    static void reachTheNums(int []nums, int k, int d1, int d2, int n)
    {
        int i;
        int []ans = new int[n];
        int gcd = __gcd(d1, d2);
     
        for (i = 0; i < n; i++) {
            int x = nums[i] - k;
     
            // If distance x is coverable
            if (x % gcd == 0)
                ans[i] = 1;
            else
                ans[i] = 0;
        }
     
        for (i = 0; i < n; i++)
            Console.Write(ans[i] + " ");
    }
 
    // Driver code
    public static void Main () {
        // Numbers to be checked for reachability
    int []nums = { 9, 4 };
    int n =nums.Length;
    // Starting number K
    int k = 8;
 
    // Sizes of jumps d1 and d2
    int d1 = 3, d2 = 2;
 
    reachTheNums(nums, k, d1, d2, n);
    }
    // This code is contributed by Ryuga
}


PHP




<?php
// PHP implementation of the approach
// gcd function
function GCD($a, $b)
{
    if ($b == 0)
        return $a;
    return GCD($b, $a % $b);
}
 
// Function that returns the vector
// containing the result for the
// reachability of the required numbers
function reachTheNums($nums, $k, $d1,
                             $d2, $n)
{
    $i = 0; $ans = array(0, 0);
    $gcd = GCD($d1, $d2);
 
    for ($i = 0; $i < $n; $i++)
    {
        $x = $nums[$i] - $k;
 
        // if distance x is coverable
        if ($x % $gcd == 0)
            $ans[$i] = 1;
        else
            $ans[$i] = 0;
    }
 
    for ($i = 0; $i < $n; $i++)
    echo $ans[$i] . " ";
}
 
// Driver Code
 
// Numbers to be checked for reachability
$nums = array(9, 4);
$n = 2;
 
// Starting number $K
$k = 8;
 
// Sizes of jumps $d1 and $d2
$d1 = 3; $d2 = 2;
 
reachTheNums($nums, $k, $d1, $d2, $n);
     
// This code is contributed by Adesh Singh1
?>


Javascript




<script>
// javascript implementation of the approach
 
    // Recursive function to return gcd of a and b
    function __gcd(a , b)
    {
     
        // Everything divides 0
        if (a == 0)
            return b;
        if (b == 0)
            return a;
 
        // base case
        if (a == b)
            return a;
 
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
        return __gcd(a, b - a);
    }
 
    // Function that returns the vector containing the
    // result for the reachability of the required numbers
    function reachTheNums(nums , k , d1 , d2 , n)
    {
        var i;
        var ans = Array(n).fill(0);
        var gcd = __gcd(d1, d2);
 
        for (let i = 0; i < n; i++)
        {
            var x = nums[i] - k;
 
            // If distance x is coverable
            if (x % gcd == 0)
                ans[i] = 1;
            else
                ans[i] = 0;
        }
 
        for (let i = 0; i < n; i++)
            document.write(ans[i] + " ");
    }
 
    // Driver code
     
    // Numbers to be checked for reachability
    var nums = [ 9, 4 ];
    var n = nums.length;
     
    // Starting number K
    var k = 8;
 
    // Sizes of jumps d1 and d2
    var d1 = 3, d2 = 2;
 
    reachTheNums(nums, k, d1, d2, n);
 
// This code is contributed by aashish1995.
</script>


Output

1 1 

Complexity Analysis:

  • Time Complexity: O(N), since there runs a loop from 0 to (n – 1).
  • Auxiliary Space: O(N), since N extra space has been taken.


Similar Reads

Check if it is possible to reach a number by making jumps of two given length
Given a starting position 'k' and two jump sizes 'd1' and 'd2', our task is to find the minimum number of jumps needed to reach 'x' if it is possible.At any position P, we are allowed to jump to positions : P + d1 and P - d1P + d2 and P - d2 Examples: Input : k = 10, d1 = 4, d2 = 6 and x = 8 Output : 2 1st step 10 + d1 = 14 2nd step 14 - d2 = 8 Inp
9 min read
Maximum jumps to reach end of Array with condition that index i can make arr[i] jumps
Given an integer N and an array arr[ ] of size N, the task is to find the maximum jumps to reach the end of the array given the constraint that from index i can make arr[i] jumps and reach the position i+arr[i]. Examples: Input: N = 5, arr[] = {2, 3, 5, 7, 9}Output: 12 Explanation:At index 0 make 2 jumps and move to index 2 and make 5 jumps after t
6 min read
Minimize cost to reach end of an array by two forward jumps or one backward jump in each move
Given an array arr[] consisting of N positive integers, the task is to find the minimum cost required to either cross the array or reach the end of the array by only moving to indices (i + 2) and (i - 1) from the ith index. Examples: Input: arr[] = {5, 1, 2, 10, 100}Output: 18Explanation:Optimal cost path (0 based indexing): 0 ? 2 ? 1 ? 3 ? 5Theref
11 min read
Minimum jumps from either end to reach largest and smallest character in given String
Given a string str, the task is to find the minimum number of moves required to reach lexicographically largest and smallest characters. In one move, a jump can be made from leftmost side or the rightmost side of given string. Examples: Input: str = AEDCB, N = 5 Output: 2Explanation: take two steps from leftmost side to reach A and E Input: str = B
6 min read
Minimum jumps to reach end of the array with given conditions
Given an array A[], the task is to print the minimum number of jumps needed to reach the last element of A[] starting from the first element. If it is impossible to reach the last element print -1. A jump can be made from index i to j if all the below conditions are met: i &lt; jA[i] &gt;= A[j]There must be no elements between index i and j or all
6 min read
Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
Given an array arr[] of integers, where the ith integer represents the position where an island is present, and an integer k (1 ? k &lt; N). A person is standing on the 0th island and has to reach the last island, by jumping from one island to another in exactly k jumps, the task is to find the minimum of the maximum length of a jump a person will
9 min read
Minimum number of Fibonacci jumps to reach end
Given an array of 0s and 1s, If any particular index i has value 1 then it is a safe index and if the value is 0 then it is an unsafe index. A man is standing at index -1(source) can only land on a safe index and he has to reach the Nth index (last position). At each jump, the man can only travel a distance equal to any Fibonacci Number. You have t
11 min read
Minimum steps to reach the Nth stair in jumps of perfect power of 2
Given N stairs, the task is to find the minimum number of jumps of perfect power of 2 requires to reach the Nth stair. Examples: Input: N = 5 Output: Explanation: We can take jumps from 0-&gt;4-&gt;5.So the minimum jumps require are 2. Input: N = 23 Output: 4 Explanation: We can take jumps from 0-&gt;1-&gt;3-&gt;7-&gt;23. So the minimum jumps requi
6 min read
Count minimum factor jumps required to reach the end of an Array
Given an array of positive integers arr[], the task is to count the minimum factor jumps required to reach the end of an array. From any particular index i, the jump can be made only for K indices where K is a factor of arr[i]. Examples: Input: arr[] = {2, 8, 16, 55, 99, 100} Output: 2 Explanation: The optimal jumps are: a) Start from 2. b) Since f
20 min read
Paths requiring minimum number of jumps to reach end of array
Given an array arr[], where each element represents the maximum number of steps that can be made forward from that element, the task is to print all possible paths that require the minimum number of jumps to reach the end of the given array starting from the first array element. Note: If an element is 0, then there are no moves allowed from that el
12 min read