Open In App

Minimum Jumps with Specific NUM Value

Last Updated : 03 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ implementation of the code
#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 bit is not set
                if ((A[j] & mask) == 0)
                    count++;
            }
 
            if (count < jumps) {
                jumps = count;
                ans = mask;
            }
        }
    }
    return ans;
}
 
// Driver code
int main()
{
 
    int N = 5;
    int A[] = { 3, 4, 2, 4, 9 };
 
    // Function call
    cout << jumpOverArray(N, A);
 
    return 0;
}


Java




public class JumpOverArray {
 
    // Function to find the bit that allows jumping over the
    // array
    public static int jumpOverArray(int N, int[] A)
    {
        int jumps
            = Integer.MAX_VALUE; // Initialize jumps to a
                                 // very large value
        int ans = Integer.MAX_VALUE; // Initialize ans to a
                                     // very large value
 
        // Iterate through each bit from 0 to 31
        for (int i = 0; i <= 31; i++) {
            int mask
                = (1 << i); // Create a mask with a single
                            // bit set to 1 at position i
 
            // Check if the first and last elements of the
            // array have the bit unset
            if (((mask & A[0]) == 0)
                && ((mask & A[N - 1]) == 0)) {
                int count = 0;
 
                // Count the number of elements in the array
                // where the bit is unset
                for (int j = 1; j < N; j++) {
                    // If bit is not set
                    if ((A[j] & mask) == 0)
                        count++;
                }
 
                // If the current bit allows fewer jumps,
                // update the jumps and ans
                if (count < jumps) {
                    jumps = count;
                    ans = mask;
                }
            }
        }
        return ans; // Return the bitmask that allows
                    // jumping over the array
    }
 
    public static void main(String[] args)
    {
        int N = 5;
        int[] A = { 3, 4, 2, 4, 9 };
 
        // Function call and print the result
        System.out.println(jumpOverArray(N, A));
    }
}


Python3




# Python Implementation
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
 
# Driver code
N = 5
A = [3, 4, 2, 4, 9]
 
# Function call
print(jump_over_array(N, A))
 
# This code is contributed by Sakshi


C#




using System;
 
class Program
{
    // Function to find the mask that minimizes the number of jumps over the array
    static int JumpOverArray(int N, int[] A)
    {
        // Initialize jumps and answer to maximum possible values
        int jumps = int.MaxValue;
        int ans = int.MaxValue;
 
        // Iterate over each bit position (0 to 31)
        for (int i = 0; i <= 31; i++)
        {
            // Create a mask with a single bit set at position i
            int mask = (1 << i);
 
            // Check if the first and last elements of the array have the bit at position i unset
            if (((mask & A[0]) == 0) && ((mask & A[N - 1]) == 0))
            {
                int count = 0;
 
                // Count the number of elements that have the bit at position i unset
                for (int j = 1; j < N; j++)
                {
                    // If bit is not set
                    if ((A[j] & mask) == 0)
                        count++;
                }
 
                // If the current mask results in fewer jumps, update jumps and answer
                if (count < jumps)
                {
                    jumps = count;
                    ans = mask;
                }
            }
        }
        return ans;
    }
 
    static void Main()
    {
        int N = 5;
        int[] A = { 3, 4, 2, 4, 9 };
 
        // Function call and display the result
        Console.WriteLine("The mask that minimizes jumps: " + JumpOverArray(N, A));
    }
}


Javascript




// Function to find the bit mask with the minimum number of set bits
function jumpOverArray(N, A) {
    // Initialize variables for minimum jumps and corresponding mask
    let jumps = Number.MAX_SAFE_INTEGER;
    let ans = Number.MAX_SAFE_INTEGER;
 
    // Iterate through each bit position (0 to 31)
    for (let i = 0; i <= 31; i++) {
        // Create a bitmask with the current bit position set to 1
        let mask = 1 << i;
 
        // Check if the current bit is not set in both the first and last elements of the array
        if ((mask & A[0]) === 0 && (mask & A[N - 1]) === 0) {
            // Initialize a counter for the number of unset bits at the current position
            let count = 0;
 
            // Iterate through the array elements (excluding the first element)
            for (let j = 1; j < N; j++) {
                // If the current bit is not set in the array element, increment the counter
                if ((A[j] & mask) === 0) {
                    count++;
                }
            }
 
            // Update the minimum jumps and corresponding mask if the current count is less than the current minimum jumps
            if (count < jumps) {
                jumps = count;
                ans = mask;
            }
        }
    }
    // Return the bitmask with the minimum number of set bits
    return ans;
}
 
// Driver code
const N = 5;
const A = [3, 4, 2, 4, 9];
 
// Call the function and output the result
console.log(jumpOverArray(N, A));


Output

4







Time Complexity: O(N)
Auxiliary space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads