Maximum power of jump required to reach the end of string
Last Updated :
30 Nov, 2022
Given a string consisting of 1 and 0, the task is to find out the maximum power of jump required to reach the end of the string. At a time you can jump from one 1 to the next 1 or from one 0 to the next 0.
Note: Power of jump is defined as the distance between two consecutive 1’s or two consecutive 0’s.
Examples:
Input: 10101
Output: 2
First, make a power jump of 1 to reach first 1,
then a power jump of 2 to reach second 1
and then finally a power jump of 2
to reach the end of the string
hence the maximum power of jump is 2.
Input: 11110
Output: 5
Since to reach the end of the string, we have to make
power jump of 5 to reach 0 and end of the string
Approach:
- Check if the last character of a given string is 1 or 0.
- If the last character is 1 then search for the first 1 in the string and continue to jump to the next 1 to reach the last of the string. Update the maximum jumps.
- Similarly, if the last character is 0 then search for the first 0 in the string and continue to jump to the next 0 to reach the last of the string. Update the maximum jumps.
- Return the maximum number of jumps taken to reach the end.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int powerOfJump(string s)
{
int count = 1;
int max_so_far = INT_MIN;
char ch = s[s.length() - 1];
for ( int i = 0; i < s.length(); i++)
{
if (s[i] == ch)
{
if (count > max_so_far) {
max_so_far = count;
}
count = 1;
}
else
count++;
}
return max_so_far;
}
int main(){
string st = "1010101" ;
cout<<powerOfJump(st);
}
|
Java
import java.util.ArrayList;
public class string_sort {
public static int powerOfJump(String s)
{
int count = 1 ;
int max_so_far = Integer.MIN_VALUE;
char ch = s.charAt(s.length() - 1 );
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == ch) {
if (count > max_so_far) {
max_so_far = count;
}
count = 1 ;
}
else
count++;
}
return max_so_far;
}
public static void main(String[] args)
{
String st = "1010101" ;
System.out.println(powerOfJump(st));
}
}
|
Python3
def powerOfJump(s):
count = 1
max_so_far = 0
ch = s[ - 1 ]
for i in range ( 0 , len (s)):
if s[i] = = ch:
if count > max_so_far:
max_so_far = count
count = 1
else :
count + = 1
return max_so_far
if __name__ = = "__main__" :
st = "1010101"
print (powerOfJump(st))
|
C#
using System;
class GFG
{
public static int powerOfJump(String s)
{
int count = 1;
int max_so_far = int .MinValue;
char ch = s[s.Length - 1];
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == ch)
{
if (count > max_so_far)
{
max_so_far = count;
}
count = 1;
}
else
count++;
}
return max_so_far;
}
public static void Main()
{
String st = "1010101" ;
Console.WriteLine(powerOfJump(st));
}
}
|
PHP
<?php
function powerOfJump( $s )
{
$count = 1;
$max_so_far = PHP_INT_MIN;
$ch = $s [ strlen ( $s ) - 1];
for ( $i = 0;
$i < strlen ( $s ); $i ++)
{
if ( $s [ $i ] == $ch )
{
if ( $count > $max_so_far )
{
$max_so_far = $count ;
}
$count = 1;
}
else
$count ++;
}
return $max_so_far ;
}
$st = "1010101" ;
echo powerOfJump( $st );
?>
|
Javascript
<script>
function powerOfJump( s) {
let count = 1;
let max_so_far = Number.MIN_VALUE;
let ch = s[s.length - 1];
for (let i = 0; i < s.length; i++) {
if (s[i] == ch) {
if (count > max_so_far) {
max_so_far = count;
}
count = 1;
}
else
count++;
}
return max_so_far;
}
let st = "1010101" ;
document.write(powerOfJump(st));
</script>
|
Time Complexity: O(n) where n is the length of the string
Auxiliary Space: O(1), no extra space is required, so it is constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...