Largest index to be reached in Binary Array after K jumps between different values
Last Updated :
20 Apr, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int maxJump( int arr[], int N, int k)
{
int i;
int count = 0;
for ( int i = 1; i < N; i++) {
if (arr[i] != arr[i - 1]) {
count++;
}
}
if (count >= k) {
for (i = N - 1; i >= 0; i--) {
if (k % 2 == 0 && arr[i] == arr[0]) {
return i;
}
if (k % 2 != 0 && arr[i] != arr[0]) {
return i;
}
}
}
return -1;
}
int main()
{
int arr[] = { 0, 1, 1, 0, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
int k = 2;
cout << maxJump(arr, N, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxJump( int arr[], int N, int k)
{
int i;
int count = 0 ;
for ( i = 1 ; i < N; i++) {
if (arr[i] != arr[i - 1 ]) {
count++;
}
}
if (count >= k) {
for (i = N - 1 ; i >= 0 ; i--) {
if (k % 2 == 0 && arr[i] == arr[ 0 ]) {
return i;
}
if (k % 2 != 0 && arr[i] != arr[ 0 ]) {
return i;
}
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 0 , 1 , 1 , 0 , 1 , 0 };
int N = arr.length;
int k = 2 ;
System.out.println(maxJump(arr, N, k));
}
}
|
Python3
def maxJump(arr, N, k):
count = 0
for i in range ( 1 , N):
if arr[i] ! = arr[i - 1 ]:
count + = 1
if count > = k:
for i in range (N - 1 , - 1 , - 1 ):
if k % 2 = = 0 and arr[i] = = arr[ 0 ]:
return i
if k % 2 ! = 0 and arr[i] ! = arr[ 0 ]:
return i
return - 1
arr = [ 0 , 1 , 1 , 0 , 1 , 0 ]
N = len (arr)
k = 2
print (maxJump(arr, N, k))
|
C#
using System;
public class GFG{
static int maxJump( int [] arr, int N, int k)
{
int i;
int count = 0;
for ( i = 1; i < N; i++) {
if (arr[i] != arr[i - 1]) {
count++;
}
}
if (count >= k) {
for (i = N - 1; i >= 0; i--) {
if (k % 2 == 0 && arr[i] == arr[0]) {
return i;
}
if (k % 2 != 0 && arr[i] != arr[0]) {
return i;
}
}
}
return -1;
}
static public void Main (){
int [] arr = { 0, 1, 1, 0, 1, 0 };
int N = arr.Length;
int k = 2;
Console.Write(maxJump(arr, N, k));
}
}
|
Javascript
<script>
const maxJump = (arr, N, k) => {
let i;
let count = 0;
for (let i = 1; i < N; i++) {
if (arr[i] != arr[i - 1]) {
count++;
}
}
if (count >= k) {
for (i = N - 1; i >= 0; i--) {
if (k % 2 == 0 && arr[i] == arr[0]) {
return i;
}
if (k % 2 != 0 && arr[i] != arr[0]) {
return i;
}
}
}
return -1;
}
let arr = [0, 1, 1, 0, 1, 0];
let N = arr.length;
let k = 2;
document.write(maxJump(arr, N, k));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...