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 next 1 or from one 0 to next 0.
Note: Power of jump is defined as the distance between two consecutive 1’s or two consecutive 0’s.
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
- Check if the last character of 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.
Time Complexity : O(n) where n is the length of the string
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Minimum moves to reach from i to j in a cyclic string
- Find minimum number of steps to reach the end of String
- Find time taken for signal to reach all positions in a string
- Find sub-string with given power
- Minimum number of sub-strings of a string such that all are power of 5
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum rotations required to get the same String | Set-2
- Minimum rotations required to get the same string
- Count of lines required to write the given String
- Operations required to make the string empty
- Minimum changes required such that the string satisfies the given condition
- Minimum flips required to keep all 1s together in a Binary string
- Count of sticks required to represent the given string
- Minimum swaps required to convert one binary string to another
- Minimum number of operations required to sum to binary string S
- Minimum operations required to convert a binary string to all 0s or all 1s
- Repeat substrings of the given String required number of times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.