Avoiding traps

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
37 upvotes
Asked in companies
Deutsche BankAmerican ExpressMicrosoft

Problem statement

Given an array of 'N' elements 'OBSTACLES', where each element represents the coordinate of the obstacle on a straight line. We start jumping from point 0 and we need to reach the end of the line avoiding all the obstacles which are present on the line. The length of every jump should be the same. For example, if we jump from 0 to 3, the jump is of 3 units hence the next jump should also be of 3 units that is from 3 to 6 and so on.

Find the minimum length of the jump so that we can reach the end of the line avoiding all obstacles.

Note:
1.The end will be a minimum possible coordinate, greater than the maximum element in the given array of elements.

2.Avoiding obstacles means that we cannot stop at the given coordinates.

3.The elements may not be in sorted order.

4.The last jump can be of any unit, provided it crosses the endpoint.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next 2 * T lines represent the ‘T’ test cases.

The first line of each test case contains an integer ‘N’, denoting the number of obstacles on the straight line.

The second line of each test case contains an array 'OBSTACLES' of 'N' elements, denoting the obstacles on the straight line.
Output format:
For each test case, print a single line containing a single integer denoting the minimum length of the jump to reach the end, avoiding all the obstacles.

The output of each test case will be printed in a separate line.
Note
You don't have to print anything. It has already been taken care of. Just implement the given function.
Constraints
1 <= T <= 50
1 <= N <= 1000
1 <= OBSTACLES[i] <= 10 ^ 6

Where ‘T’ is the total number of test cases, ‘N’ denotes the number of obstacles on the straight line, and ‘OBSTACLES[i]’ denotes the coordinates of obstacles on the straight line.

Time limit: 1 sec.
Sample Input 1:
2
5
5 3 6 7 9
5
5 8 9 13 14
Sample Output 1:
4
6
Explanation of sample input 1:
Test case 1

alt text

Obstacles are at positions 5, 3, 6, 7, 9. 
If we take a jump of size 1, it will touch 3 and so it is not viable
For jump size 2, it will touch 6 and so it is discarded.
For jump size 3, it will touch 3 and so it is also discarded.

For jump size 4, we will jump from 0 to 4, then 4 to 8, then 8 to 12. Hence, we reach the end by avoiding all the obstacles.

Test case 2:

Obstacles are at positions 5, 8, 9, 13, 14.
With jump size 6, we will jump from 0 to 6, then 6 to 12, followed by 12 to 18. Hence, we reach the end by avoiding all the obstacles.
Sample Input 2:
1
6
10 1 2 3 6 9
Sample Output 2:
4
Hint

What could be the possible range of jumps?

Approaches (2)
Brute force
  1. Find the largest element in the array of elements and store it in variable ‘maxElement’.
  2. Create a boolean vector, ‘B’ of size equal to the value of the largest element in vector ‘A’. Initialize the vector ‘B’ with value ‘false’.
  3. Considering the elements of vector ‘A’ as an index in vector ‘B’, mark these indexes as ‘true’.
  4. Run a loop where ‘i’ ranges from ‘1’ to ’maxElement’.
        A. Consider ‘i’ as the possible length of the jump.
        B. Initialize a boolean variable flag to ‘false’.
        C. For every ‘i’, run a loop where ‘j’ ranges from ‘i’ to ’maxElement’. ‘j’ stores the multiple of ‘i’. So, after every iteration, increment ‘j’ with value ‘i’.
            a. Check whether the current multiple i.e, ‘j’ is present in the vector ‘B’ or not.
            b. If it is present, it means that the length of the jump, ‘i’ will touch the obstacle ‘j’. Hence, break the loop, as ‘i’ is not the possible length of jump and turn the boolean variable flag to true.
  5. If for length ‘i’, the value of the flag is ‘false’ after the completion of the ‘j’ loop, it means ‘i’ did not hit any obstacle and is the required answer.
  6. Finally, return ‘i’.
  7. If the answer is greater than ’maxElement’, then after completing all the loops, we return ’maxElement+1’.
Time Complexity

O(maxElement * log(maxElement)), ‘maxElement’ is the largest element in the given array of elements. 

 

In the worst case, the minimum length of a jump will be ‘maxElement + 1’. So, we are considering all jumps from 1 to ‘maxElement + 1’. For each jump length ‘i’, the number of jump points we are checking for obstacles is ‘maxElement + 1’ / i. So, the time complexity becomes ‘maxElement + 1’ / 1 + ‘maxElement + 1’ / 2 + … + 1. Hence, the overall complexity becomes O(maxElement * log(maxElement)).

Space Complexity

O(maxElement), where ‘maxElement’ is the largest element in the given array of elements.

 

In the worst case, we will be using O(maxElement) space, therefore net space complexity will be O(maxElement).

Code Solution
(100% EXP penalty)
Avoiding traps
Full screen
Console