Minimum steps to reach end by jumping to next different bit once
Last Updated :
18 May, 2022
Given a binary array arr[] of size N which is starting from index 0, the task is to reach the end of the array in the minimum steps, movement within the array can be done in 2 types of steps.
- Type1: Move to the immediate next index having the same value.
- Type2: Move to the immediate next index having a different value.
Note: Type 2 can be used just once while traversing.
Examples:
Input: arr[] = {1, 0, 1, 0, 1}
Output: 2
Explanation: Starting from index 0
First step: type1: 0->2
Second step: type1: 2->4
Input: arr[] = {1, 0, 0, 0, 1, 0, 1, 0}
Output: 3
Explanation: Starting from index 0
First step: type1: 0->4
Second step: type1: 4->6
Third step: type2: 6->7
Approach: The problem can be solved using pre-computation technique based on the following idea:
To figure out, from which type of move we should proceed, simple check that the first and last elements are same or not, if yes then simply proceed with type 1 step otherwise find an optimal position for switching with type2 stepping
Follow the steps to solve the problem:
- If first and last are same then move with type1 steps only and we can directly print number of steps by calculating number of elements having same value excluding first.
- If first and last elements are of different value then find an optimal position for switching with type2 stepping.
- This can be done by pre-computation for switching at all possible indices.
- Use 2 arrays X and Y, X holding number of steps from start (i.e total occurrences of arr[0] from start) while Y holding the number of steps from end (i.e. total occurrences of arr[N-1] from the end).
- Where switching is possible, check the total steps required and update the minimum steps accordingly.
- Return the minimum number of steps required.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Minstep( int arr[], int n)
{
int c = arr[0], d = arr[n - 1];
int x[n + 1], y[n + 1];
x[0] = 0;
y[n] = 0;
for ( int i = 1; i < n; i++) {
if (arr[i] == c)
x[i] = x[i - 1] + 1;
else
x[i] = x[i - 1];
}
if (arr[0] == arr[n - 1]) {
return x[n - 1];
}
for ( int i = n - 1; i >= 0; i--) {
if (arr[i] == d)
y[i] = y[i + 1] + 1;
else
y[i] = y[i + 1];
}
int ans = INT_MAX;
for ( int i = 0; i < n; i++) {
if (arr[i] != c)
continue ;
ans = min(ans, x[i] + y[i + 1]);
}
return ans;
}
int main()
{
int N = 5;
int arr[] = { 1, 0, 1, 0, 1 };
cout << Minstep(arr, N) << '\n' ;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int Minstep( int arr[], int n)
{
int c = arr[ 0 ], d = arr[n - 1 ];
int x[] = new int [n + 1 ];
int y[] = new int [n + 1 ];
x[ 0 ] = 0 ;
y[n] = 0 ;
for ( int i = 1 ; i < n; i++) {
if (arr[i] == c)
x[i] = x[i - 1 ] + 1 ;
else
x[i] = x[i - 1 ];
}
if (arr[ 0 ] == arr[n - 1 ]) {
return x[n - 1 ];
}
for ( int i = n - 1 ; i >= 0 ; i--) {
if (arr[i] == d)
y[i] = y[i + 1 ] + 1 ;
else
y[i] = y[i + 1 ];
}
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
if (arr[i] != c)
continue ;
ans = Math.min(ans, x[i] + y[i + 1 ]);
}
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
int arr[] = { 1 , 0 , 1 , 0 , 1 };
System.out.println(Minstep(arr, N));
}
}
|
Python3
INT_MAX = 2147483647
def Minstep(arr, n):
c, d = arr[ 0 ], arr[n - 1 ]
x, y = [ 0 for i in range (n + 1 )],[ 0 for i in range (n + 1 )]
x[ 0 ] = 0
y[n] = 0
for i in range ( 1 , n):
if (arr[i] = = c):
x[i] = x[i - 1 ] + 1
else :
x[i] = x[i - 1 ]
if (arr[ 0 ] = = arr[n - 1 ]):
return x[n - 1 ]
for i in range (n - 1 , - 1 , - 1 ):
if (arr[i] = = d):
y[i] = y[i + 1 ] + 1
else :
y[i] = y[i + 1 ]
ans = INT_MAX
for i in range (n):
if (arr[i] ! = c):
continue
ans = min (ans, x[i] + y[i + 1 ])
return ans
N = 5
arr = [ 1 , 0 , 1 , 0 , 1 ]
print (Minstep(arr, N))
|
C#
using System;
class GFG {
static int Minstep( int [] arr, int n)
{
int c = arr[0], d = arr[n - 1];
int [] x = new int [n + 1];
int [] y = new int [n + 1];
x[0] = 0;
y[n] = 0;
for ( int i = 1; i < n; i++) {
if (arr[i] == c)
x[i] = x[i - 1] + 1;
else
x[i] = x[i - 1];
}
if (arr[0] == arr[n - 1]) {
return x[n - 1];
}
for ( int i = n - 1; i >= 0; i--) {
if (arr[i] == d)
y[i] = y[i + 1] + 1;
else
y[i] = y[i + 1];
}
int ans = Int32.MaxValue;
for ( int i = 0; i < n; i++) {
if (arr[i] != c)
continue ;
ans = Math.Min(ans, x[i] + y[i + 1]);
}
return ans;
}
public static void Main()
{
int N = 5;
int [] arr = { 1, 0, 1, 0, 1 };
Console.Write(Minstep(arr, N));
}
}
|
Javascript
<script>
const INT_MAX = 2147483647;
const Minstep = (arr, n) => {
let c = arr[0], d = arr[n - 1];
let x = new Array(n + 1).fill(0), y = new Array(n + 1).fill(0);
x[0] = 0;
y[n] = 0;
for (let i = 1; i < n; i++) {
if (arr[i] == c)
x[i] = x[i - 1] + 1;
else
x[i] = x[i - 1];
}
if (arr[0] == arr[n - 1]) {
return x[n - 1];
}
for (let i = n - 1; i >= 0; i--) {
if (arr[i] == d)
y[i] = y[i + 1] + 1;
else
y[i] = y[i + 1];
}
let ans = INT_MAX;
for (let i = 0; i < n; i++) {
if (arr[i] != c)
continue ;
ans = Math.min(ans, x[i] + y[i + 1]);
}
return ans;
}
let N = 5;
let arr = [1, 0, 1, 0, 1]
document.write(Minstep(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...