Maximum power of jump required to reach the end of string in C++


In this tutorial, we will be discussing a program to find maximum power of jump required to reach the end of string.

For this we will be provided with a string of 0s and 1s. Our task is to find the maximum jump required to move from front to end of string given you can move to the same element as current one.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//finding maximum power jump
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);
}

Output

2

Updated on: 09-Sep-2020

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements