Reach the target | Practice | GeeksforGeeks

Given four integers A, B, C, D. A represents the initial position of the geek on the x-axis. In each step, geek can go to X+B or X-B if he is standing at X. The tasks is to check if it is possible for the geek to reach C exactly after D steps.
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains four space-separated integers A, B, C, and D.

Output: For each test case, print "yes" if it is possible to reach C in exactly D steps. Otherwise, print "no" (without quotes).

Constraints:
1. 1 <= T <= 5
2. -10^9 <= A, C <= 10^9
3. 1 <= B <= 10^9
4. 1 <= D <= 15

Example:
Input:
3
2 3 8 4
-3 1 0 3
-3 1 1 3
Output:
yes
yes
no

Explanation:
Test case 1: one of the possible ways is {2, 5, 8, 11, 8}.
+1
harshit68rocking4 months ago
#include <iostream>
using namespace std;


bool solve(long long a,long long b,long long c, long long d){

    if(d<0){ return false;}
    if(c==a && d==0) {return true;}    
    
    return solve(a,b,c-b,d-1) || solve(a,b,c+b,d-1);
}

int main() {
	//code
	int t;
	cin>>t;
	while(t--){
	    long long a,b,c,d;
	    cin>>a>>b>>c>>d;
	    if(solve(a,b,c,d)) cout<<"yes \n";
	    else cout<<"no \n";
	}
	return 0;
}
+2
imranwahid6 months ago
Easy C++ solution
+3
kaalel7 months ago
An simple Iterative solution as recursive takes extra AUXs.
Time.C -→ O(T → no of testcases) with no extra space.
 
Scanner scanner = new Scanner(System.in);
        int loop = scanner.nextInt();
        while (loop != 0) {
            int A = scanner.nextInt();
            int B = scanner.nextInt();
            int C = scanner.nextInt();
            int D = scanner.nextInt();
            int difference = C - A;
            int minimumSteps = difference / B;
            if (difference % B == 0 && minimumSteps <= D) {
                if (minimumSteps == D) System.out.println("yes");
                else if(((D - minimumSteps) & 1) == 0) System.out.println("yes");
                else System.out.println("no");
            } else System.out.println("no");
            loop--;
        }
The (else if) statement is to handle this test case :- (2,3,11,6).
0
AMAN RAJ8 months ago
AMAN RAJ
#include <bits stdc++.h="">using namespace std;int main() {    int t;    long long a,b,c,d;    cin>>t;    while(t--){        cin>>a>>b>>c>>d;        long long k=abs(c-a);        if(k%b){            cout<<"no\n";            continue;        }        long long s=k/b;        if(s>d){            cout<<"no\n";            continue;        }        d-=s;        if(d%2)        cout<<"no\n";        else        cout<<"yes\n";    }    return 0;}
0
AMAN RAJ
This comment was deleted.
0
Rishabh Verma9 months ago
Rishabh Verma
#include<iostream>using namespace std;#define ll  long long int;bool func(ll a,ll b,ll c,ll d){    if(d == 0 ){        return a == c;    }    if(a <= c)func(a + b, b, c, d - 1);elsefunc(a - b, b, c, d - 1);}int main() {     ll t;     cin>>t;     while(t--){    ll a,b,c,d;cin>>a>>b>>c>>d;if(func(a,b,c,d)){    cout<<"yes"<<endl; }="" else{="" cout<<"no"<<endl;="" }="" }="" return="" 0;="" }="" <="" code="">
0
Devendra Yadav10 months ago
Devendra Yadav
My code is not giving right output for the below input what changes should I make in my code . Input:-729315417 750626557 -1688734020 13
Here is the Code!!bool isTrue(int a,int b,int c,int d){     bool flag = false;     if(d==0){         if(a==c)            return true;         else            return false;     }     return isTrue(a+b,b,c,d-1)||isTrue(a-b,b,c,d-1);}
0
ANAS MALVAT10 months ago
ANAS MALVAT
int flag;void recur(long long  a, long long  b , long long c, long long d){    if(d ==0)    {        if(a == c)        {         flag = 1;           }        return;    }
    recur(a + b,b,c, d - 1);    recur(a - b,b,c, d- 1);}int main() {//codeint T;cin >> T;while(T--){    int a,b,c,d;    flag = 0;    cin>>a>>b>>c>>d;        recur(a,b,c,d);    if(flag)    {        cout<<"yes";    }    else{        cout<<"no";    }    cout<<"\n";}return 0;}
0
Shivam kumar_09211 months ago
Shivam kumar_092
import java.util.*;import java.lang.*;import java.io.*;class GFG{    public static void main(String[] args){
      Scanner input=new Scanner(System.in);      int T=input.nextInt();      for(int i=0;i<t;i++) {="" long="" a="input.nextInt();" long="" b="input.nextInt();" long="" c="input.nextInt();" long="" d="input.nextInt();" int="" m="fun(A,B,C,D);" if(m="">0)              System.out.println("yes");          else              System.out.println("no");      }    }
 public static int fun(long A,long B,long C,long D) {      if(D==0) {          if(A==C)              return 1;          else              return 0;      }
      return (fun(A+B,B,C,D-1)+fun(A-B,B,C,D-1));
 }
}
0
jack11 months ago
jack
def target(a,b,c,d):    for i in range(d):        if(a<c): a+="b" else:="" a-="b" if(a="=c):" return="" "yes"="" return="" "no"="" t="int(input())" for="" i="" in="" range(t):="" a,b,c,d="map(int,input().split())" print(target(a,b,c,d))="" <="" code="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.