Open In App

Farthest position that can be reached in a binary string in K jumps by jumping on alternate digits

Improve
Improve
Like Article
Like
Save
Share
Report

Given a binary string S of length N and an integer K, the task is to calculate the farthest position that can be reached starting from the first position in exactly K jumps.
A jump can be made from index i to j only if:

  • i != j
  • If the character at one of them is ‘0’ and another is ‘1’.

Examples:

Input: S = “100101”, K = 2
Output: 6
Explanation: Following steps can be taken: 1 -> 5 -> 6, Therefore 6th index can be reached in exactly 2 jumps

Input: S = “10111”, K = 1
Output: 2
Explanation: Following steps can be taken: 1 -> 2, Therefore 2nd index can be reached in exactly 2 jumps

 

Approach: The main observation of the problem is that the continuous patterns of 0s and 1s can be replaced with single 0 or single 1 because one cannot jump between similar characters. After replacing these continuous patterns of 0s and 1s, Now, one can simply check if the new pattern says str formed is less than equal to K then it is not possible to reach some point with K moves, otherwise, the position is simply the first position from the right in the actual pattern which contains the character str[K].

Follow the steps below to solve the problem:

  • Iterate from start to end of the given string i = 0 to i = N-1 and generate new string str after replacing all continuous subpatterns of 0s with single 0 and of 1s with single 1.
  • Now, check if the length of the newly formed string is less than equal to K then print -1
  • Else if the length of the newly formed string is greater than K, then simply print the 1-based position of the character str[K] from the right.

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 farthest point
void get(string s, int k)
{
    // Store the answer
    int ans = 0;
 
    // Find the new string str
    string str = "";
    str += s[0];
 
    // Variables to count the zeroes and ones
    int cnt1 = 0, cnt0 = 0;
    int n = s.length();
 
    // Boolean variable to keep track of the subpattern
    bool isOne;
 
    if (s[0] == '0')
        isOne = false;
 
    // Iterate over the string and remove the
    // continuous patterns of 0s and 1s
    for (int i = 1; i < n; i++) {
        if (s[i] == '0' && isOne) {
            str += s[i];
            isOne = !isOne;
        }
        else if (s[i] == '1' && !isOne) {
            str += s[i];
            isOne = !isOne;
        }
    }
 
    // Count the number of zeroes and ones
    for (int i = 0; i < n; i++) {
        if (str[i] == '0')
            cnt0++;
        else
            cnt1++;
    }
 
    // Check if the K jumps are not possible
    if (str.length() <= k) {
        cout << -1 << endl;
        return;
    }
 
    // If K jumps are possible
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == str[k]) {
            ans = i + 1;
            break;
        }
    }
    cout << ans + 1 << endl;
}
 
// Driver Code
int main()
{
    string s = "100101";
    int k = 2;
    get(s, k);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the farthest point
static void get(String s, int k)
{
    // Store the answer
    int ans = 0;
 
    // Find the new string str
    String str = "";
    str += s.charAt(0);
 
    // Variables to count the zeroes and ones
    int cnt1 = 0, cnt0 = 0;
    int n = s.length();
 
    // Boolean variable to keep track of the subpattern
    boolean isOne = false;
 
    if (s.charAt(0) == '0')
        isOne = false;
 
    // Iterate over the string and remove the
    // continuous patterns of 0s and 1s
    for (int i = 1; i < n; i++) {
        if (s.charAt(i) == '0' && isOne) {
            str += s.charAt(i);
            isOne = !isOne;
        }
        else if (s.charAt(i) == '1' && !isOne) {
            str += s.charAt(i);
            isOne = !isOne;
        }
    }
 
    // Count the number of zeroes and ones
    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) == '0')
            cnt0++;
        else
            cnt1++;
    }
 
    // Check if the K jumps are not possible
    if (str.length() <= k) {
        System.out.print(-1);
        return;
    }
 
    // If K jumps are possible
    for (int i = n - 1; i >= 0; i--) {
        if (s.charAt(i) == str.charAt(k)) {
            ans = i + 1;
            break;
        }
    }
    System.out.print(ans + 1);
}
 
// Driver Code
public static void main(String args[])
{
    String s = "100101";
    int k = 2;
    get(s, k);
}
}
// This code is contributed by Samim Hossain Mondal


Python3




# Python3 program for the above approach
 
# Function to find the farthest point
def get(s, k) :
 
    # Store the answer
    ans = 0;
 
    # Find the new string str
    string = "";
    string += s[0];
 
    # Variables to count the zeroes and ones
    cnt1 = 0; cnt0 = 0;
    n = len(s);
 
    # Boolean variable to keep track of the subpattern
    isOne=False;
 
    if (s[0] == '0') :
        isOne = False;
 
    # Iterate over the string and remove the
    # continuous patterns of 0s and 1s
    for i in range(1, n) :
        if (s[i] == '0' and isOne) :
            string += s[i];
            isOne = not isOne;
         
        elif (s[i] == '1' and (not isOne)) :
            string += s[i];
            isOne = not isOne;
 
    # Count the number of zeroes and ones
    for i in range(len(string)) :
        if (string[i] == '0') :
            cnt0 += 1;
        else :
            cnt1 += 1;
 
    # Check if the K jumps are not possible
    if (len(string) <= k) :
        print(-1) ;
        return;
 
    # If K jumps are possible
    for i in range(n - 1, -1, -1) :
         
        if (s[i] == string[k]) :
            ans = i + 1;
            break;
    print(ans + 1);
 
 
# Driver Code
if __name__ ==  "__main__" :
 
    s = "100101";
    k = 2;
    get(s, k);
 
    # This code is contributed by AnkThon


C#




// C# program for the above approach
using System;
class GFG
{
// Function to find the farthest point
static void get(string s, int k)
{
    // Store the answer
    int ans = 0;
 
    // Find the new string str
    string str = "";
    str += s[0];
 
    // Variables to count the zeroes and ones
    int cnt1 = 0, cnt0 = 0;
    int n = s.Length;
 
    // Bool variable to keep track of the subpattern
    bool isOne = false;
 
    if (s[0] == '0')
        isOne = false;
 
    // Iterate over the string and remove the
    // continuous patterns of 0s and 1s
    for (int i = 1; i < n; i++) {
        if (s[i] == '0' && isOne) {
            str += s[i];
            isOne = !isOne;
        }
        else if (s[i] == '1' && !isOne) {
            str += s[i];
            isOne = !isOne;
        }
    }
 
    // Count the number of zeroes and ones
    for (int i = 0; i < str.Length; i++) {
        if (str[i] == '0')
            cnt0++;
        else
            cnt1++;
    }
 
    // Check if the K jumps are not possible
    if (str.Length <= k) {
        Console.Write(-1);
        return;
    }
 
    // If K jumps are possible
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == str[k]) {
            ans = i + 1;
            break;
        }
    }
    Console.Write(ans + 1);
}
 
// Driver Code
public static void Main()
{
    string s = "100101";
    int k = 2;
    get(s, k);
}
}
// This code is contributed by Samim Hossain Mondal


Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to find the farthest point
        function get(s, k)
        {
         
            // Store the answer
            let ans = 0;
 
            // Find the new string str
            let str = "";
            str += s[0];
 
            // Variables to count the zeroes and ones
            let cnt1 = 0, cnt0 = 0;
            let n = s.length;
 
            // Boolean variable to keep track of the subpattern
            let isOne;
 
            if (s[0] == '0')
                isOne = false;
 
            // Iterate over the string and remove the
            // continuous patterns of 0s and 1s
            for (let i = 1; i < n; i++) {
                if (s[i] == '0' && isOne) {
                    str += s[i];
                    isOne = !isOne;
                }
                else if (s[i] == '1' && !isOne) {
                    str += s[i];
                    isOne = !isOne;
                }
            }
 
            // Count the number of zeroes and ones
            for (let i = 0; i < n; i++) {
                if (str[i] == '0')
                    cnt0++;
                else
                    cnt1++;
            }
 
            // Check if the K jumps are not possible
            if (str.length <= k) {
                document.write(-1 + '<br>');
                return;
            }
 
            // If K jumps are possible
            for (let i = n - 1; i >= 0; i--) {
                if (s[i] == str[k]) {
                    ans = i + 1;
                    break;
                }
            }
            document.write(ans + 1 + '<br>');
        }
 
        // Driver Code
 
        let s = "100101";
        let k = 2;
        get(s, k);
 
    // This code is contributed by Potta Lokesh
    </script>


Output

6

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



Last Updated : 12 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads