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

 `// C++ code to implement the approach`   `#include ` `using` `namespace` `std;`   `// Function to find the minimum number of steps` `int` `Minstep(``int` `arr[], ``int` `n)` `{` `    ``// c is first value, d is last value` `    ``int` `c = arr, d = arr[n - 1];`   `    ``// x and y arrays for precomputation` `    ``int` `x[n + 1], y[n + 1];` `    ``x = 0;` `    ``y[n] = 0;`   `    ``// Traversing and cumulatively adding` `    ``// previous step values from start` `    ``// if value is same simply add 1` `    ``for` `(``int` `i = 1; i < n; i++) {` `        ``if` `(arr[i] == c)` `            ``x[i] = x[i - 1] + 1;` `        ``else` `            ``x[i] = x[i - 1];` `    ``}`   `    ``// Returning if same elements are` `    ``// there in first and last` `    ``if` `(arr == arr[n - 1]) {` `        ``return` `x[n - 1];` `    ``}`   `    ``// Traversing and cumulatively adding` `    ``// previous step values from end` `    ``// if value is same simply add 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];` `    ``}`   `    ``// Assigning ans as maximum` `    ``int` `ans = INT_MAX;`   `    ``for` `(``int` `i = 0; i < n; i++) {`   `        ``// As the task is to find` `        ``// optimal position, try to take values` `        ``// from all positions possible and` `        ``// take minimum as answer.` `        ``if` `(arr[i] != c)` `            ``continue``;` `        ``ans = min(ans, x[i] + y[i + 1]);` `    ``}`   `    ``// Returning answer` `    ``return` `ans;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `N = 5;` `    ``int` `arr[] = { 1, 0, 1, 0, 1 };`   `    ``// Function call` `    ``cout << Minstep(arr, N) << ``'\n'``;` `    ``return` `0;` `}`

## Java

 `// Java code to implement the approach` `import` `java.io.*;`   `class` `GFG {` `    ``// Function to find the minimum number of steps` `    ``public` `static` `int` `Minstep(``int` `arr[], ``int` `n)` `    ``{` `        ``// c is first value, d is last value` `        ``int` `c = arr[``0``], d = arr[n - ``1``];`   `        ``// x and y arrays for precomputation` `        ``int` `x[] = ``new` `int``[n + ``1``];` `        ``int` `y[] = ``new` `int``[n + ``1``];` `        ``x[``0``] = ``0``;` `        ``y[n] = ``0``;`   `        ``// Traversing and cumulatively adding` `        ``// previous step values from start` `        ``// if value is same simply add 1` `        ``for` `(``int` `i = ``1``; i < n; i++) {` `            ``if` `(arr[i] == c)` `                ``x[i] = x[i - ``1``] + ``1``;` `            ``else` `                ``x[i] = x[i - ``1``];` `        ``}`   `        ``// Returning if same elements are` `        ``// there in first and last` `        ``if` `(arr[``0``] == arr[n - ``1``]) {` `            ``return` `x[n - ``1``];` `        ``}`   `        ``// Traversing and cumulatively adding` `        ``// previous step values from end` `        ``// if value is same simply add 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``];` `        ``}`   `        ``// Assigning ans as maximum` `        ``int` `ans = Integer.MAX_VALUE;`   `        ``for` `(``int` `i = ``0``; i < n; i++) {`   `            ``// As the task is to find` `            ``// optimal position, try to take values` `            ``// from all positions possible and` `            ``// take minimum as answer.` `            ``if` `(arr[i] != c)` `                ``continue``;` `            ``ans = Math.min(ans, x[i] + y[i + ``1``]);` `        ``}`   `        ``// Returning answer` `        ``return` `ans;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `N = ``5``;` `        ``int` `arr[] = { ``1``, ``0``, ``1``, ``0``, ``1` `};`   `        ``// Function call` `        ``System.out.println(Minstep(arr, N));` `    ``}` `}`   `// This code is contributed by Rohit Pradhan`

## Python3

 `# Python implementation of above approach` `INT_MAX ``=` `2147483647`   `# Function to find the minimum number of steps` `def` `Minstep(arr, n):` `  `  `    ``# c is first value, d is last value` `    ``c, d ``=` `arr[``0``], arr[n``-``1``]`   `    ``# x and y arrays for precomputation` `    ``x, y ``=` `[``0` `for` `i ``in` `range``(n ``+` `1``)],[``0` `for` `i ``in` `range``(n ``+` `1``)]` `    ``x[``0``] ``=` `0` `    ``y[n] ``=` `0`   `    ``# Traversing and cumulatively adding` `    ``# previous step values from start` `    ``# if value is same simply add 1` `    ``for` `i ``in` `range``(``1``, n):` `        ``if` `(arr[i] ``=``=` `c):` `            ``x[i] ``=` `x[i ``-` `1``] ``+` `1` `        ``else``:` `            ``x[i] ``=` `x[i ``-` `1``]`   `    ``# Returning if same elements are` `    ``# there in first and last` `    ``if` `(arr[``0``] ``=``=` `arr[n ``-` `1``]):` `        ``return` `x[n ``-` `1``]`   `    ``# Traversing and cumulatively adding` `    ``# previous step values from end` `    ``# if value is same simply add 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``]`   `    ``# Assigning ans as maximum` `    ``ans ``=` `INT_MAX`   `    ``for` `i ``in` `range``(n):`   `        ``# As the task is to find` `        ``# optimal position, try to take values` `        ``# from all positions possible and` `        ``# take minimum as answer.` `        ``if` `(arr[i] !``=` `c):` `            ``continue` `        ``ans ``=` `min``(ans, x[i] ``+` `y[i ``+` `1``])`     `    ``# Returning answer` `    ``return` `ans`   `# Driver code` `N ``=` `5` `arr ``=` `[``1``, ``0``, ``1``, ``0``, ``1``]`   `# Function call` `print``(Minstep(arr, N))`   `# This code is contributed by shinjanpatra`

## C#

 `// C# code to implement the approach` `using` `System;`   `class` `GFG {` `  ``// Function to find the minimum number of steps` `  ``static` `int` `Minstep(``int``[] arr, ``int` `n)` `  ``{` `    ``// c is first value, d is last value` `    ``int` `c = arr, d = arr[n - 1];`   `    ``// x and y arrays for precomputation` `    ``int``[] x = ``new` `int``[n + 1];` `    ``int``[] y = ``new` `int``[n + 1];` `    ``x = 0;` `    ``y[n] = 0;`   `    ``// Traversing and cumulatively adding` `    ``// previous step values from start` `    ``// if value is same simply add 1` `    ``for` `(``int` `i = 1; i < n; i++) {` `      ``if` `(arr[i] == c)` `        ``x[i] = x[i - 1] + 1;` `      ``else` `        ``x[i] = x[i - 1];` `    ``}`   `    ``// Returning if same elements are` `    ``// there in first and last` `    ``if` `(arr == arr[n - 1]) {` `      ``return` `x[n - 1];` `    ``}`   `    ``// Traversing and cumulatively adding` `    ``// previous step values from end` `    ``// if value is same simply add 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];` `    ``}`   `    ``// Assigning ans as maximum` `    ``int` `ans = Int32.MaxValue;`   `    ``for` `(``int` `i = 0; i < n; i++) {`   `      ``// As the task is to find` `      ``// optimal position, try to take values` `      ``// from all positions possible and` `      ``// take minimum as answer.` `      ``if` `(arr[i] != c)` `        ``continue``;` `      ``ans = Math.Min(ans, x[i] + y[i + 1]);` `    ``}`   `    ``// Returning answer` `    ``return` `ans;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main()` `  ``{` `    ``int` `N = 5;` `    ``int``[] arr = { 1, 0, 1, 0, 1 };`   `    ``// Function call` `    ``Console.Write(Minstep(arr, N));` `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`2`

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :