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++
#include <iostream>
using namespace std;
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 ;
}
int h = height[i];
while (h > x)
{
jumps++;
h = h - (x - y);
}
jumps++;
}
return jumps;
}
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
public class GFG {
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 ;
}
int h = height[i];
while (h > x)
{
jumps++;
h = h - (x - y);
}
jumps++;
}
return jumps;
}
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));
}
}
|
Python3
def jumpcount(x, y, n, height):
jumps = 0
for i in range (n):
if (height[i] < = x):
jumps + = 1
continue
h = height[i]
while (h > x):
jumps + = 1
h = h - (x - y)
jumps + = 1
return jumps
x = 10
y = 1
height = [ 11 , 10 , 10 , 9 ]
n = len (height)
print (jumpcount(x, y, n, height))
|
C#
using System;
public class GFG {
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 ;
}
int h = height[i];
while (h > x)
{
jumps++;
h = h - (x - y);
}
jumps++;
}
return jumps;
}
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));
}
}
|
PHP
<?php
function jumpcount( $x , $y , $n ,
$height )
{
$jumps = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $height [ $i ] <= $x )
{
$jumps ++;
continue ;
}
$h = $height [ $i ];
while ( $h > $x )
{
$jumps ++;
$h = $h - ( $x - $y );
}
$jumps ++;
}
return $jumps ;
}
$x = 10; $y = 1;
$height = array ( 11, 10, 10, 9 );
$n = count ( $height );
echo jumpcount( $x , $y , $n , $height );
?>
|
Javascript
<script>
function jumpcount(x, y, n, height)
{
let jumps = 0;
for (let i = 0; i < n; i++) {
if (height[i] <= x) {
jumps++;
continue ;
}
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++
#include <iostream>
using namespace std;
int jumpcount( int x, int y, int n, int height[])
{
int jumps = 0;
for ( int i = 0; i < n; i++) {
jumps++;
if (height[i] > x)
{
int h = height[i] - (x - y);
jumps += h/(x - y);
if (h % (x-y) > 1)
jumps++;
}
}
return jumps;
}
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
public class GFG {
static int jumpcount( int x, int y, int n,
int height[])
{
int jumps = 0 ;
for ( int i = 0 ; i < n; i++) {
jumps++;
if (height[i] > x)
{
int h = height[i] - (x - y);
jumps += h/(x - y);
if (h % (x-y) > 1 )
jumps++;
}
}
return jumps;
}
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));
}
}
|
Python3
def jumpcount(x, y, n, height):
jumps = 0
for i in range (n):
jumps + = 1
if (height[i] > x):
h = height[i] - (x - y)
jumps + = h / / (x - y)
if (h % (x - y) > 1 ):
jumps + = 1
return jumps
x = 10
y = 1
height = [ 11 , 34 , 27 , 9 ]
n = len (height)
print (jumpcount(x, y, n, height))
|
C#
using System;
public class GFG {
static int jumpcount( int x, int y,
int n, int []height)
{
int jumps = 0;
for ( int i = 0; i < n; i++) {
jumps++;
if (height[i] > x)
{
int h = height[i] - (x - y);
jumps += h / (x - y);
if (h % (x - y) > 1)
jumps++;
}
}
return jumps;
}
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));
}
}
|
PHP
<?php
function jumpcount( $x , $y ,
$n , $height )
{
$jumps = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$jumps ++;
if ( $height [ $i ] > $x )
{
$h = $height [ $i ] - ( $x - $y );
$jumps += $h / ( $x - $y );
if ( $h % ( $x - $y ) > 1)
$jumps ++;
}
}
return $jumps ;
}
{
$x = 10;
$y = 1;
$height = array (11, 34, 27, 9);
$n = sizeof( $height ) / sizeof( $height [0]);
echo jumpcount( $x , $y , $n , $height );
return 0;
}
?>
|
Javascript
<script>
function jumpcount(x, y, n, height)
{
let jumps = 0;
for (let i = 0; i < n; i++) {
jumps++;
if (height[i] > x)
{
let h = height[i] - (x - y);
jumps += parseInt(h / (x - y), 10);
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));
</script>
|
Output:
10
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...