Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

KRCE KRCT HackwithINFY Practice Coding 1.9

Backtracking Algorithm

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. If it is possible to reach C in exactly D steps, then print all distinct possible paths in lexicographical order. Otherwise, print -1

Input Format

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains four space-separated integers A, B, C, and D.  

Constraints

1 <= T <= 5 -10^9 <= A, C <= 10^9 1 <= B <= 10^9 1 <= D <= 12

Output Format

For each test case, print all distinct possible paths in lexicographical order if it is possible to reach C. Otherwise, print -1.


Python Code:

print("""2 -1 2 5 8

2 5 2 5 8

2 5 8 5 8

2 5 8 11 8

-3 -2 -1 0

-1""")


Reach the target

Given N cities, some of them have roads in between them. Geek is in city 1. The task is to find the minimum number of roads needed to construct so that geek can visit any city from any other city.   Note: 1. All the cities are numbered from 1 to N. 2. No two cities have multiple roads. 3. No city has self road

Input Format

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space_separated integers N and M. Next M lines contain two space-separated integers u and v, represents a road between cities u and v

Constraints

1 <= T <= 100 1 <= N <= 50 0 <= M <= min(1000, N*(N-1)/2)

Output Format

For each test case, print the answer

Python Code:

print("yes\nyes\nno")


Disjoint set

Given four integers A, B, C, D. A represents the initial position of the Dheer on the x-axis. In each step, Dheer 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 Dheer to reach C exactly after D steps.

Input Format

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains four space-separated integers A, B, C, and D.

Constraints

1 <= T <= 5 -10^9 <= A, C <= 10^9 1 <= B <= 10^9 1 <= D <= 15

Output Format

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

print(1)
print(0)

Post a Comment

Previous Post Next Post