Reach the numbers by making jumps of two given lengths
Last Updated :
12 Sep, 2022
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++
#include <algorithm>
#include <iostream>
using namespace std;
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 (x % gcd == 0)
ans[i] = 1;
else
ans[i] = 0;
}
for (i = 0; i < n; i++)
cout << ans[i] << " " ;
}
int main()
{
int nums[] = { 9, 4 };
int n = sizeof (nums) / sizeof (nums[0]);
int k = 8;
int d1 = 3, d2 = 2;
reachTheNums(nums, k, d1, d2, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
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 (x % gcd == 0 )
ans[i] = 1 ;
else
ans[i] = 0 ;
}
for (i = 0 ; i < n; i++)
System.out.print(ans[i] + " " );
}
public static void main (String[] args) {
int nums[] = { 9 , 4 };
int n =nums.length;
int k = 8 ;
int d1 = 3 , d2 = 2 ;
reachTheNums(nums, k, d1, d2, n);
}
}
|
Python3
import math as mt
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 (x % gcd = = 0 ):
ans[i] = 1
else :
ans[i] = 0
for i in range (n):
print (ans[i], end = " " )
nums = [ 9 , 4 ]
n = len (nums)
k = 8
d1, d2 = 3 , 2
reachTheNums(nums, k, d1, d2, n)
|
C#
using System ;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
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 (x % gcd == 0)
ans[i] = 1;
else
ans[i] = 0;
}
for (i = 0; i < n; i++)
Console.Write(ans[i] + " " );
}
public static void Main () {
int []nums = { 9, 4 };
int n =nums.Length;
int k = 8;
int d1 = 3, d2 = 2;
reachTheNums(nums, k, d1, d2, n);
}
}
|
PHP
<?php
function GCD( $a , $b )
{
if ( $b == 0)
return $a ;
return GCD( $b , $a % $b );
}
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 ( $x % $gcd == 0)
$ans [ $i ] = 1;
else
$ans [ $i ] = 0;
}
for ( $i = 0; $i < $n ; $i ++)
echo $ans [ $i ] . " " ;
}
$nums = array (9, 4);
$n = 2;
$k = 8;
$d1 = 3; $d2 = 2;
reachTheNums( $nums , $k , $d1 , $d2 , $n );
?>
|
Javascript
<script>
function __gcd(a , b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
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 (x % gcd == 0)
ans[i] = 1;
else
ans[i] = 0;
}
for (let i = 0; i < n; i++)
document.write(ans[i] + " " );
}
var nums = [ 9, 4 ];
var n = nums.length;
var k = 8;
var d1 = 3, d2 = 2;
reachTheNums(nums, k, d1, d2, n);
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...