![]() |
VOOZH | about |
Given an array A[] of N integers, You have to move from index 1 to N by doing jumps. Return the value of NUM (NUM is defined as a number that has a power of 2 and remains fixed while jumping throughout the array) so that you can reach the other end with minimum jumps. If more than one value of NUM makes us reach with minimum jumps, return the lowest NUM value. A jump from index i to index j is said to be valid if the following conditions are satisfied.
Examples:
Input: N = 5, A = {3, 4, 2, 4, 9}
Output: 4
Explanation: If NUM=4, our jumps will be from index 1->3->5 these jumps satisfy the above three conditions, and this gives a minimum number of jumps = 2Input: N = 5, A = {1, 6, 2, 4, 9}
Output: 2
Explanation: If NUM=2, our jumps will be like this 1->4->5, and this minimizes the number of jumps = 2.
Approach: To solve the problem follow the below idea:
Using masking for each element, if the bit is not set, then a jump would be required. for each i from 0 to 31, we need to check the minimum number of jumps required.
Below are the steps involved:
Below is the implementation of the following code:
4
Time Complexity: O(N)
Auxiliary space: O(1)