Number of jumps required for a thief to cross walls


Imagine a prisoner (or thief) wants to escape from a jail. In order to do so, he needs to cross N number of walls of varying lengths. He can climb X feet for each jump. But, since the walls are slippery, he falls down by Y feet after each jump. So, we need to calculate number of jumps required to cross all the walls. In this article, we will explore different C++ techniques to find the number of jumps required to escape the jail.

Input Output Scenarios

We have different heights of the N walls in the form of an array. X is the jump length while Y is length by which he falls back. We have number of jumps as the output.

Input: height[] = {5, 18, 10, 3}
       N = 4, X = 5, Y = 2
Output: 11
Input: height[] = {15, 8, 10, 3, 5, 12}
       N = 6, X = 5, Y = 2
Output: 16

Using Iterative Approach

Here, we use for and while loop for finding the number of jumps.

When the height of the wall is less than the jump length (x), then it can be crossed in a single jump. Hence, numJumps is incremented by one. We use continue statement to stop the remaining loop and move on to the upcoming loop.

When the height is more than jump length, we use while loop to calculate the number of jumps by h – (x – y) until the remaining height becomes less than or equal to the jump length.

Next, we add one more jump for the final wall.

Example

#include <iostream>
using namespace std;

int numOfJumps(int x, int y, int N, int heights[]) {
   int numJumps = 0;

   // When the height is less than jump length
   for (int j = 0; j < N; j++) {
      if (x >= heights[j]) {
         numJumps++;
         continue;
      }

      // When the height is more than jump length
      int h = heights[j];
      while (h > x) {
         numJumps++;
         h = h - (x - y);
      }
      numJumps++;
   }
   return numJumps;
}

int main() {
   int N = 5; // Number of walls
   int x = 4; // jump height
   int y = 1; // length after he slips back
   int heights[] = {5, 18, 10, 3, 5};
   int minJumpsRequired = numOfJumps(x, y, N, heights);
   cout << "Minimum number of jumps required: " << minJumpsRequired << endl;
   return 0;
}

Output

Minimum number of jumps required: 14

Using Direct Calculation

Following is the formula to calculate the Number of jumps required for a thief to cross walls −

Jumps = ceil((h - y) / static_cast<double>(x - y))

We use a for loop to iterate over each wall. The height of the current wall is stored in variable h.

Then, we use the formula to directly calculate the number of jumps required. We use ceil function to round off the value to the nearest integer.

Example

#include <iostream>
#include <cmath>
using namespace std;

int numOfJumps(int x, int y, int N, int height[]) {
    int numJumps = 0;
    
    for (int j = 0; j < N; j++) {
        int h = height[j];
        int jumpsRequired = ceil((h - y) / static_cast<double>(x - y));
        numJumps += jumpsRequired;
    }
    
    return numJumps;
}


int main() {
    int x = 8, y = 2;
    int height[] = { 4, 14, 8, 16, 20, 11 };
    int N = sizeof(height) / sizeof(height[0]);
    
    int minJumpsRequired = numOfJumps(x, y, N, height);
    cout << "Minimum number of jumps required: " << minJumpsRequired << endl;
    return 0;
}

Output

Minimum number of jumps required: 12

Using Division and Modulo Operators

We can also use the division (/) and modulo (%) operators for the number of jumps. Here, we calculate the difference between the height of wall and jump length. If the difference is greater than 0, we calculate the number of jumps by dividing it by (x-y). If there is remainder, we increment by one. Whereas if the difference is zero or negative, then we need only one jump.

Example

#include <iostream>

using namespace std;

int numOfJumps(int x, int y, int N, int height[]) {
   int jumps = 0;
   for (int j = 0; j < N; j++) {
      int diff = height[j] - x;

      // When height is greater than jump length
      if (diff > 0) {
         jumps++;

         // Additional jumps
         jumps += diff / (x - y);

         // If there is a remainder, increment the jumps
         if (diff % (x - y) != 0)
            jumps++;
      }

      // When height is less than jump length
      else {
         jumps++;
      }
   }
   return jumps;
}
int main() {
   int N = 5; // Number of walls
   int x = 5; // jump height
   int y = 2; // length after he slips back
   int height[] = { 15, 8, 10, 3, 5, 12};
   int minJumpsRequired = numOfJumps(x, y, N, height);
   cout << "Minimum number of jumps required: " << minJumpsRequired << endl;
   return 0;
}

Output

Minimum number of jumps required: 12

Conclusion

We have discussed various approaches to find the number of jumps for a thief to cross the walls. We may use the iteration method. Instead of such iterations, we can directly use the formula. Also, we can use the division and modulo operators to solve this problem.

Updated on: 12-Jul-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements