Number of jumps for a thief to cross walls

Difficulty Level :
Easy
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++
Java
Python3
C#
PHP
Javascript
// 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 program to find the number of// jumps requiredpublic 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 program to find the number of# jumps required # function to calculate jumps required to# cross wallsdef 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 functionx = 10y = 1height = [ 11, 10, 10, 9 ]n = len(height)print (jumpcount(x, y, n, height)) # This code is contributed by Sachin Bisht
// C# program to find the // number of jumps requiredusing 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 program to find the// number of jumps required /* function to calculatejumps 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.?>
<script>     // JavaScript program to find the    // number of jumps required         // function to calculate jumps     // required to cross walls    function jumpcount(x, y, n, height)    {        let jumps = 0;               for (let i = 0; i < n; i++) {            if (height[i] <= x) {                jumps++;                continue;            }                   // If height of wall is             // greater than up move            let h = height[i];            while (h > x)            {                jumps++;                h = h - (x - y);            }            jumps++;        }        return jumps;    }         let x = 10, y = 1;    let height = [11, 10, 10, 9];    let n = height.length;    document.write(jumpcount(x, y, n, height));         </script>
Output:  
5
Time Complexity: O(height[i]*n)
Auxiliary Space: O(1)
We can optimize above solution by directly computing number of jumps. 
C++
Java
Python3
C#
PHP
Javascript
// 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 program to find the number of// jumps requiredpublic 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 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 functionx = 10y = 1height = [ 11, 34, 27, 9 ]n = len(height)print (jumpcount(x, y, n, height)) # This code is contributed by Sachin Bisht
// C# program to find the// number of jumps requiredusing 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 program to find the// number of jumps required // function to calculate jumps// required to cross wallsfunction 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.?>
<script>    // Javascript program to find the number of jumps required         // Function to calculate jumps    // required to cross walls    function jumpcount(x, y, n, height)   {        let jumps = 0;        for (let 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            let h = height[i] - (x - y);                   // Remaining jumps            jumps += parseInt(h / (x - y), 10);                   // 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;    }         let x = 10;    let y = 1;    let height = [11, 34, 27, 9];    let n = height.length;    document.write(jumpcount(x, y, n, height));         // This code is contributed by mukesh07.</script>
Output: 
10
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
vt_m
nitin mittal
divyeshrabadiya07
mukesh07
amartyaghoshgfg
prophet1999
Cisco
MakeMyTrip
Arrays
Mathematical
MakeMyTrip
Cisco
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Coin Change | DP-7