# 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++

 `// C++ program to find the number of``// jumps required``#include ``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);``    ``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`

## Python3

 `# 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 ``=` `10``y ``=` `1``height ``=` `[ ``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

 ` ``\$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.``?>`

## Javascript

 ``

Output:

`5`

Time Complexity: O(height[i]*n)

Auxiliary Space: O(1)

We can optimize above solution by directly computing number of jumps.

## C++

 `// C++ program to find the number of``// jumps required``#include ``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);``    ``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`

## Python3

 `# 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 ``=` `10``y ``=` `1``height ``=` `[ ``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

 ` ``\$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``);``    ``echo` `jumpcount(``\$x``, ``\$y``, ``\$n``, ``\$height``);``    ``return` `0;``}` `// This code is contributed by nitin mittal.``?>`

## Javascript

 ``

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.