Number of jumps for a thief to cross walls
A thief trying to escape from a jail. He has to cross N walls each with varying heights (every height is greater than 0). He climbs X feet every time. But, due to the slippery nature of those walls, every time he slips back by Y feet. Now the task is to calculate the total number of jumps required to cross all walls and escape from the jail.
Examples :
Input : heights[] = {11, 11}
X = 10;
Y = 1;
Output : 4
He needs to make 2 jumps for first wall
and 2 jumps for second wall.
Input : heights[] = {11, 10, 10, 9}
X = 10;
Y = 1;
Output : 5
The solution is quite simple if the height of wall is less than or equal to x, only one jump in that wall is required else we can calculate it by height of wall-(climb up-climb down) and get the jumps required.
C++
// C++ program to find the number of // jumps required #include <iostream> using namespace std; /* function to calculate jumps required to cross walls */int jumpcount(int x, int y, int n, int height[]) { int jumps = 0; for (int i = 0; i < n; i++) { if (height[i] <= x) { jumps++; continue; } /* If height of wall is greater than up move */ int h = height[i]; while (h > x) { jumps++; h = h - (x - y); } jumps++; } return jumps; } /*driver function*/int main() { int x = 10, y = 1; int height[] = { 11, 10, 10, 9 }; int n = sizeof(height)/sizeof(height[0]); cout << jumpcount(x, y, n, height); return 0; } |
Java
// Java program to find the number of // jumps required public class GFG { /* function to calculate jumps required to cross walls */ static int jumpcount(int x, int y, int n, int height[]) { int jumps = 0; for (int i = 0; i < n; i++) { if (height[i] <= x) { jumps++; continue; } /* If height of wall is greater than up move */ int h = height[i]; while (h > x) { jumps++; h = h - (x - y); } jumps++; } return jumps; } /*driver function*/ public static void main(String args[]) { int x = 10, y = 1; int height[] = { 11, 10, 10, 9 }; int n = height.length; System.out.println(jumpcount(x, y, n, height)); } } // This code is contributed by Sumit Ghosh |
Python
# Python program to find the number of # jumps required # function to calculate jumps required to # cross walls def jumpcount(x, y, n, height): jumps = 0 for i in range(n): if (height[i] <= x): jumps += 1 continue """ If height of wall is greater than up move """ h = height[i] while (h > x): jumps += 1 h = h - (x - y) jumps += 1 return jumps # driver function x = 10y = 1height = [ 11, 10, 10, 9 ] n = len(height) print jumpcount(x, y, n, height) # This code is contributed by Sachin Bisht |
C#
// C# program to find the // number of jumps required using System; public class GFG { // function to calculate jumps // required to cross walls static int jumpcount(int x, int y, int n, int []height) { int jumps = 0; for (int i = 0; i < n; i++) { if (height[i] <= x) { jumps++; continue; } // If height of wall is // greater than up move int h = height[i]; while (h > x) { jumps++; h = h - (x - y); } jumps++; } return jumps; } // Driver Code public static void Main(String []args) { int x = 10, y = 1; int []height = {11, 10, 10, 9}; int n = height.Length; Console.WriteLine(jumpcount(x, y, n, height)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find the // number of jumps required /* function to calculate jumps required to cross walls */function jumpcount($x, $y, $n, $height) { $jumps = 0; for ( $i = 0; $i < $n; $i++) { if ($height[$i] <= $x) { $jumps++; continue; } /* If height of wall is greater than up move */ $h = $height[$i]; while ($h > $x) { $jumps++; $h = $h - ($x - $y); } $jumps++; } return $jumps; } // Driver Code $x = 10; $y = 1; $height = array( 11, 10, 10, 9 ); $n = count($height); echo jumpcount($x, $y, $n, $height); // This code is contributed by anuj_67. ?> |
Output :
5
We can optimize above solution by directly computing number of jumps.
C++
// C++ program to find the number of // jumps required #include <iostream> using namespace std; /* function to calculate jumps required to cross walls */int jumpcount(int x, int y, int n, int height[]) { int jumps = 0; for (int i = 0; i < n; i++) { // Since all heights are // greater than 1, at-least // one jump is always required jumps++; // More jumps required if height // is greater than x. if (height[i] > x) { // Since we have already counted // one jump int h = height[i] - (x - y); // Remaining jumps jumps += h/(x - y); // If there was a remainder greater // than 1. 1 is there to handle cases // like x = 11, y = 1, height[i] = 21. if (h % (x-y) > 1) jumps++; } } return jumps; } /* driver function */int main() { int x = 10; int y = 1; int height[] = { 11, 34, 27, 9 }; int n = sizeof(height)/sizeof(height[0]); cout << jumpcount(x, y, n, height); return 0; } |
Java
// Java program to find the number of // jumps required public class GFG { /* function to calculate jumps required to cross walls */ static int jumpcount(int x, int y, int n, int height[]) { int jumps = 0; for (int i = 0; i < n; i++) { // Since all heights are // greater than 1, at-least // one jump is always required jumps++; // More jumps required if height // is greater than x. if (height[i] > x) { // Since we have already counted // one jump int h = height[i] - (x - y); // Remaining jumps jumps += h/(x - y); // If there was a remainder greater // than 1. 1 is there to handle cases // like x = 11, y = 1, height[i] = 21. if (h % (x-y) > 1) jumps++; } } return jumps; } /* driver function */ public static void main(String args[]) { int x = 10; int y = 1; int height[] = { 11, 34, 27, 9 }; int n = height.length; System.out.println(jumpcount(x, y, n, height)); } } // This code is contributed by Sumit Ghosh |
Python
# Python program to find the number of # jumps required """ function to calculate jumps required to cross walls """def jumpcount(x, y, n, height): jumps = 0 for i in range(n): # Since all heights are # greater than 1, at-least # one jump is always required jumps += 1 # More jumps required if height # is greater than x. if (height[i] > x): # Since we have already counted # one jump h = height[i] - (x - y) # Remaining jumps jumps += h/(x - y) # If there was a remainder greater # than 1. 1 is there to handle cases # like x = 11, y = 1, height[i] = 21. if (h % (x-y) > 1): jumps += 1 return jumps # driver function x = 10y = 1height = [ 11, 34, 27, 9 ] n = len(height) print jumpcount(x, y, n, height) # This code is contributed by Sachin Bisht |
C#
// C# program to find the // number of jumps required using System; public class GFG { // Function to calculate jumps // required to cross walls static int jumpcount(int x, int y, int n, int []height) { int jumps = 0; for (int i = 0; i < n; i++) { // Since all heights are // greater than 1, at-least // one jump is always required jumps++; // More jumps required if // height is greater than x. if (height[i] > x) { // Since we have already // counted one jump int h = height[i] - (x - y); // Remaining jumps jumps += h / (x - y); // If there was a remainder greater // than 1. 1 is there to handle cases // like x = 11, y = 1, height[i] = 21. if (h % (x - y) > 1) jumps++; } } return jumps; } // Driver Code public static void Main(String []args) { int x = 10; int y = 1; int []height = {11, 34, 27, 9}; int n = height.Length; Console.WriteLine(jumpcount(x, y, n, height)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find the // number of jumps required // function to calculate jumps // required to cross walls function jumpcount($x, $y, $n, $height) { $jumps = 0; for ($i = 0; $i < $n; $i++) { // Since all heights are // greater than 1, at-least // one jump is always required $jumps++; // More jumps required if height // is greater than x. if ($height[$i] > $x) { // Since we have // already counted // one jump $h = $height[$i] - ($x - $y); // Remaining jumps $jumps += $h / ($x - $y); // If there was a // remainder greater // than 1. 1 is there // to handle cases // like x = 11, y = 1, // height[i] = 21. if ($h % ($x - $y) > 1) $jumps++; } } return $jumps; } // Driver Code { $x = 10; $y = 1; $height = array(11, 34, 27, 9); $n = sizeof($height) / sizeof($height[0]); echo jumpcount($x, $y, $n, $height); return 0; } // This code is contributed by nitin mittal. ?> |
Output:
10
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find the number of jumps to reach X in the number line from zero
- Minimum number of jumps to reach end
- Find if two people ever meet after same number of jumps
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Minimum number of Fibonacci jumps to reach end
- Check if it is possible to reach a number by making jumps of two given length
- Minimum Initial Energy Required To Cross Street
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)
- Reach the numbers by making jumps of two given lengths
- Minimum length of jumps to avoid given array of obstacles
- Maximum path sum for each position with jumps under divisibility condition
- Source to destination in 2-D path with fixed sized jumps
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Program for dot product and cross product of two vectors



