Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C++ code to find minimum jump to reach home by frog
Suppose we have one binary string S with n bits and another number d. On a number line, a frog wants to reach point n, starting from the point 1. The frog can jump to the right at a distance not more than d. For each point from 1 to n if there is a lily flower it is marked as 1, and 0 if not. The frog can jump only in points with a lilies. We have to find the minimal number of jumps that the frog needs to reach n. If not possible, return -1.
So, if the input is like S = "10010101"; d = 4, then the output will be 2, because from position 1, it jumps to 4, then at the index 8(n).
Steps
To solve this, we will follow these steps −
n := size of s x := 0 y := 0 while (x = n, then: return -1 Otherwise return y
Example
Let us see the following implementation to get better understanding −
#includeusing namespace std; int solve(string s, int d){ int n = s.size(); int x = 0, y = 0; while (x = n) return -1; else return y; } int main(){ string S = "10010101"; int d = 4; cout Input
"10010101", 4Output
2
Advertisements
