Skip to content
Related Articles

Related Articles

Largest index to be reached in Binary Array after K jumps between different values

Improve Article
Save Article
  • Last Updated : 20 Apr, 2022
Improve Article
Save Article

Given a binary array arr[] of size N and an integer K, the task is to find the highest index which can be reached in exactly K jumps starting from first index, when one jump can be made between indices having different values.

Examples:

Input: arr[] = {0, 1, 1, 0, 1, 0}, K = 2
Output: 5
Explanation: All possible jumps are:
{0, 1, 3}, {0, 2, 3}, {0, 1, 5}, {0, 2, 5}, {0, 4, 5}
So, the highest index that can be reached is index 5.

Input: arr[] = {1, 0, 1, 1, 0}, K = 3
Output: 4

 

Approach: The problem can be solved based on the following observation:

  • The highest possible value of K is same as the total number of shifts between consecutive 1s and consecutive 0s.
  • As in a jump, the two values are different, so if K is even then the value at starting index and value at last index will be same and if K is odd ten they will be different.
  • Now to  find the highest index (when it is possible to make K jumps), iterate from end of array and based on K being even or odd return the first index i such that arr[i] = arr[0] or arr[i] ≠ arr[0] (because it is already found that a total of K jumps can be made between them).

Given below is an illustration for better understanding:

Illustration:

Consider arr[] = {0, 1, 1, 0, 1, 0}, K = 2.

Highest possible value of K = 4:
=> Consecutive 0s in range [0, 0]. Total shifts = 0
=> Consecutive 1s in range [1, 2]. Shift from consecutive 0s to 1s. Total shifts = 1.
=> Consecutive 0s in range [3, 3]. Shift from consecutive 1s to 0s. Total shifts = 1+1 = 2.
=> Consecutive 1s in range [4, 4]. Shift from consecutive 0s to 1s. Total shifts = 2+1 = 3.
=> Consecutive 0s in range [5, 5]. Shift from consecutive 1s to 0s. Total shifts = 3+1 = 4.

Iterate from i = 5 to 0:
=>For i = 5: arr[5] = arr[0] = 0. K = 2 i.e. even. Stop iterating
Highest index that can be reached is 5.

One such path is (0->1->5).

Follow the steps mentioned below to solve the problem:

  • Traverse the array from i = 0 to N-1:
    • Find total shifts from consecutive 0s to consecutive 1s and vice versa (say count).
  • If K > count, return -1 as K jumps is not possible.
  • Otherwise, traverse from i = N-1 to 0:
    • If K is even, stop iteration when arr[i] = arr[0].
    • If K is odd, stop iteration when arr[i] ≠ arr[0].
  • Return the highest index achieved from the above step.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the index to which
// the longest jump can be made
int maxJump(int arr[], int N, int k)
{
    int i;
 
    // To store possible cases count
    int count = 0;
    for (int i = 1; i < N; i++) {
        if (arr[i] != arr[i - 1]) {
            count++;
        }
    }
    if (count >= k) {
 
        // Traversing the array A[]
        // from the end
        // to find longest index
        for (i = N - 1; i >= 0; i--) {
 
            // Firstly checking
            // if k is even and
            // if first and last element
            // match
            if (k % 2 == 0 && arr[i] == arr[0]) {
 
                // Return the required index
                return i;
            }
 
            // Or, if k is odd
            // and first and last
            // element doesn't match
            if (k % 2 != 0 && arr[i] != arr[0]) {
 
                // Return the required index
                return i;
            }
        }
    }
    return -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 1, 0, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
 
    // Function call
    cout << maxJump(arr, N, k);
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
 
  // Function to find the index to which
  // the longest jump can be made
  static int maxJump(int arr[], int N, int k)
  {
    int i;
 
    // To store possible cases count
    int count = 0;
    for ( i = 1; i < N; i++) {
      if (arr[i] != arr[i - 1]) {
        count++;
      }
    }
    if (count >= k) {
 
      // Traversing the array A[]
      // from the end
      // to find longest index
      for (i = N - 1; i >= 0; i--) {
 
        // Firstly checking
        // if k is even and
        // if first and last element
        // match
        if (k % 2 == 0 && arr[i] == arr[0]) {
 
          // Return the required index
          return i;
        }
 
        // Or, if k is odd
        // and first and last
        // element doesn't match
        if (k % 2 != 0 && arr[i] != arr[0]) {
 
          // Return the required index
          return i;
        }
      }
    }
    return -1;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 0, 1, 1, 0, 1, 0 };
    int N = arr.length;
    int k = 2;
 
    // Function call
    System.out.println(maxJump(arr, N, k));
  }
}
 
// This code is contributed by lokeshpotta20.


Python3




# Python3 Program for the above approach
 
# Function to find the index to which
# the longest jump can be made
def maxJump(arr, N, k):
   
    # To store possible cases count
    count = 0
    for i in range(1, N):
        if arr[i] != arr[i - 1]:
            count += 1
 
    if count >= k:
       
        # Traversing the array A[]
        # from the end
        # to find longest index
        for i in range(N - 1, -1, -1):
           
            # Firstly checking
            # if k is even and
            # if first and last element
            # match
            if k % 2 == 0 and arr[i] == arr[0]:
               
                # Return the required index
                return i
               
            # Or, if k is odd
            # and first and last
            # element doesn't match
            if k % 2 != 0 and arr[i] != arr[0]:
               
                # Return the required index
                return i
    return -1
 
# Driver Code
arr = [0, 1, 1, 0, 1, 0]
N = len(arr)
k = 2
 
# function call
print(maxJump(arr, N, k))
 
# This code is contributed by phasing17.


C#




using System;
 
public class GFG{
 
  // Function to find the index to which
  // the longest jump can be made
  static int maxJump(int[] arr, int N, int k)
  {
    int i;
 
    // To store possible cases count
    int count = 0;
    for ( i = 1; i < N; i++) {
      if (arr[i] != arr[i - 1]) {
        count++;
      }
    }
    if (count >= k) {
 
      // Traversing the array A[]
      // from the end
      // to find longest index
      for (i = N - 1; i >= 0; i--) {
 
        // Firstly checking
        // if k is even and
        // if first and last element
        // match
        if (k % 2 == 0 && arr[i] == arr[0]) {
 
          // Return the required index
          return i;
        }
 
        // Or, if k is odd
        // and first and last
        // element doesn't match
        if (k % 2 != 0 && arr[i] != arr[0]) {
 
          // Return the required index
          return i;
        }
      }
    }
    return -1;
  }
 
  // Driver Code
  static public void Main (){
 
    int[] arr = { 0, 1, 1, 0, 1, 0 };
    int N = arr.Length;
    int k = 2;
 
    // Function call
    Console.Write(maxJump(arr, N, k));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
    // JavaScript program for the above approach
 
    // Function to find the index to which
    // the longest jump can be made
    const maxJump = (arr, N, k) => {
        let i;
 
        // To store possible cases count
        let count = 0;
        for (let i = 1; i < N; i++) {
            if (arr[i] != arr[i - 1]) {
                count++;
            }
        }
        if (count >= k) {
 
            // Traversing the array A[]
            // from the end
            // to find longest index
            for (i = N - 1; i >= 0; i--) {
 
                // Firstly checking
                // if k is even and
                // if first and last element
                // match
                if (k % 2 == 0 && arr[i] == arr[0]) {
 
                    // Return the required index
                    return i;
                }
 
                // Or, if k is odd
                // and first and last
                // element doesn't match
                if (k % 2 != 0 && arr[i] != arr[0]) {
 
                    // Return the required index
                    return i;
                }
            }
        }
        return -1;
    }
 
    // Driver Code
 
    let arr = [0, 1, 1, 0, 1, 0];
    let N = arr.length;
    let k = 2;
 
    // Function call
    document.write(maxJump(arr, N, k));
 
// This code is contributed by rakeshsahni
 
</script>


Output

5

Time complexity: O(N)
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!