Farthest position that can be reached in a binary string in K jumps by jumping on alternate digits
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++
#include <bits/stdc++.h>
using namespace std;
void get(string s, int k)
{
int ans = 0;
string str = "" ;
str += s[0];
int cnt1 = 0, cnt0 = 0;
int n = s.length();
bool isOne;
if (s[0] == '0' )
isOne = false ;
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;
}
}
for ( int i = 0; i < n; i++) {
if (str[i] == '0' )
cnt0++;
else
cnt1++;
}
if (str.length() <= k) {
cout << -1 << endl;
return ;
}
for ( int i = n - 1; i >= 0; i--) {
if (s[i] == str[k]) {
ans = i + 1;
break ;
}
}
cout << ans + 1 << endl;
}
int main()
{
string s = "100101" ;
int k = 2;
get(s, k);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void get(String s, int k)
{
int ans = 0 ;
String str = "" ;
str += s.charAt( 0 );
int cnt1 = 0 , cnt0 = 0 ;
int n = s.length();
boolean isOne = false ;
if (s.charAt( 0 ) == '0' )
isOne = false ;
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;
}
}
for ( int i = 0 ; i < str.length(); i++) {
if (str.charAt(i) == '0' )
cnt0++;
else
cnt1++;
}
if (str.length() <= k) {
System.out.print(- 1 );
return ;
}
for ( int i = n - 1 ; i >= 0 ; i--) {
if (s.charAt(i) == str.charAt(k)) {
ans = i + 1 ;
break ;
}
}
System.out.print(ans + 1 );
}
public static void main(String args[])
{
String s = "100101" ;
int k = 2 ;
get(s, k);
}
}
|
Python3
def get(s, k) :
ans = 0 ;
string = "";
string + = s[ 0 ];
cnt1 = 0 ; cnt0 = 0 ;
n = len (s);
isOne = False ;
if (s[ 0 ] = = '0' ) :
isOne = False ;
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;
for i in range ( len (string)) :
if (string[i] = = '0' ) :
cnt0 + = 1 ;
else :
cnt1 + = 1 ;
if ( len (string) < = k) :
print ( - 1 ) ;
return ;
for i in range (n - 1 , - 1 , - 1 ) :
if (s[i] = = string[k]) :
ans = i + 1 ;
break ;
print (ans + 1 );
if __name__ = = "__main__" :
s = "100101" ;
k = 2 ;
get(s, k);
|
C#
using System;
class GFG
{
static void get ( string s, int k)
{
int ans = 0;
string str = "" ;
str += s[0];
int cnt1 = 0, cnt0 = 0;
int n = s.Length;
bool isOne = false ;
if (s[0] == '0' )
isOne = false ;
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;
}
}
for ( int i = 0; i < str.Length; i++) {
if (str[i] == '0' )
cnt0++;
else
cnt1++;
}
if (str.Length <= k) {
Console.Write(-1);
return ;
}
for ( int i = n - 1; i >= 0; i--) {
if (s[i] == str[k]) {
ans = i + 1;
break ;
}
}
Console.Write(ans + 1);
}
public static void Main()
{
string s = "100101" ;
int k = 2;
get (s, k);
}
}
|
Javascript
<script>
function get(s, k)
{
let ans = 0;
let str = "" ;
str += s[0];
let cnt1 = 0, cnt0 = 0;
let n = s.length;
let isOne;
if (s[0] == '0' )
isOne = false ;
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;
}
}
for (let i = 0; i < n; i++) {
if (str[i] == '0' )
cnt0++;
else
cnt1++;
}
if (str.length <= k) {
document.write(-1 + '<br>' );
return ;
}
for (let i = n - 1; i >= 0; i--) {
if (s[i] == str[k]) {
ans = i + 1;
break ;
}
}
document.write(ans + 1 + '<br>' );
}
let s = "100101" ;
let k = 2;
get(s, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
12 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...