Minimum Jumps with Specific NUM Value
Last Updated :
03 Dec, 2023
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.
- A[i] & NUM == 0
- A[j] & NUM == 0
- A[k] & NUM != 0 (i<k<j)
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 = 2
Input: 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:
- Iterating for each i i.e. from 0 to 31, calculating mask value.
- For particular mask value, calculate A[j] & mask:
- If it is unset bit, we can increase the number of jumps.
- If the bit is set, the jump is not required.
Below is the implementation of the following code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int jumpOverArray( int N, int A[])
{
int jumps = INT_MAX;
int ans = INT_MAX;
for ( int i = 0; i <= 31; i++) {
int mask = (1 << i);
if (((mask & A[0]) == 0)
&& ((mask & A[N - 1]) == 0)) {
int count = 0;
for ( int j = 1; j < N; j++) {
if ((A[j] & mask) == 0)
count++;
}
if (count < jumps) {
jumps = count;
ans = mask;
}
}
}
return ans;
}
int main()
{
int N = 5;
int A[] = { 3, 4, 2, 4, 9 };
cout << jumpOverArray(N, A);
return 0;
}
|
Java
public class JumpOverArray {
public static int jumpOverArray( int N, int [] A)
{
int jumps
= Integer.MAX_VALUE;
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i <= 31 ; i++) {
int mask
= ( 1 << i);
if (((mask & A[ 0 ]) == 0 )
&& ((mask & A[N - 1 ]) == 0 )) {
int count = 0 ;
for ( int j = 1 ; j < N; j++) {
if ((A[j] & mask) == 0 )
count++;
}
if (count < jumps) {
jumps = count;
ans = mask;
}
}
}
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
int [] A = { 3 , 4 , 2 , 4 , 9 };
System.out.println(jumpOverArray(N, A));
}
}
|
Python3
def jump_over_array(N, A):
jumps = float ( 'inf' )
ans = float ( 'inf' )
for i in range ( 32 ):
mask = 1 << i
if (A[ 0 ] & mask = = 0 ) and (A[N - 1 ] & mask = = 0 ):
count = 0
for j in range ( 1 , N):
if A[j] & mask = = 0 :
count + = 1
if count < jumps:
jumps = count
ans = mask
return ans
N = 5
A = [ 3 , 4 , 2 , 4 , 9 ]
print (jump_over_array(N, A))
|
C#
using System;
class Program
{
static int JumpOverArray( int N, int [] A)
{
int jumps = int .MaxValue;
int ans = int .MaxValue;
for ( int i = 0; i <= 31; i++)
{
int mask = (1 << i);
if (((mask & A[0]) == 0) && ((mask & A[N - 1]) == 0))
{
int count = 0;
for ( int j = 1; j < N; j++)
{
if ((A[j] & mask) == 0)
count++;
}
if (count < jumps)
{
jumps = count;
ans = mask;
}
}
}
return ans;
}
static void Main()
{
int N = 5;
int [] A = { 3, 4, 2, 4, 9 };
Console.WriteLine( "The mask that minimizes jumps: " + JumpOverArray(N, A));
}
}
|
Javascript
function jumpOverArray(N, A) {
let jumps = Number.MAX_SAFE_INTEGER;
let ans = Number.MAX_SAFE_INTEGER;
for (let i = 0; i <= 31; i++) {
let mask = 1 << i;
if ((mask & A[0]) === 0 && (mask & A[N - 1]) === 0) {
let count = 0;
for (let j = 1; j < N; j++) {
if ((A[j] & mask) === 0) {
count++;
}
}
if (count < jumps) {
jumps = count;
ans = mask;
}
}
}
return ans;
}
const N = 5;
const A = [3, 4, 2, 4, 9];
console.log(jumpOverArray(N, A));
|
Time Complexity: O(N)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...