River Crossing: Help Ninja Cross Over
Ninja stands at the start of a wooden bridge over a river. Some segments of the bridge are damaged. The undamaged segments, marked as 'safe', are listed in an array SAFE
, sorted in increasing order. Ninja must navigate from the first to the last safe segment by jumping from one safe segment to another.
Can Ninja make it across?
Input:
The first line contains an integer ‘T’ - the number of test cases.
For each test case, the first line contains an integer ‘N’ - the number of safe units.
The second line contains ‘N’ distinct integers representing the ‘SAFE’ units.
Output:
For each test case, return 'true' if Ninja can cross the river. Otherwise, return 'false'.
Example:
Input:
1
4
1 2 4 7
Output:
true
Explanation:
Ninja can cross by jumping 1 unit to 2, then 2 units to 4, and finally 3 units to 7.
Constraints:
1 <= T <= 10
2 <= N <= 10^3
0 <= SAFE[i] <= 10^5
- Time limit: 1 second
Note:
The first jump is always 1 unit. If the previous jump was 'X' units, the next can be 'X' - 1, 'X', or 'X' + 1 units. Implement the function without printing directly.
AnswerBot
5d
Check if Ninja can cross the river by jumping from one safe segment to another based on given constraints.
Iterate through the 'SAFE' array and check if Ninja can make the required jumps to reach the l...read more
Help your peers!
Add answer anonymously...
Top ThoughtWorks Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in ThoughtWorks Software Developer
>
ThoughtWorks Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app