Skip to content
Related Articles

Related Articles

Improve Article

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 <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 = 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




<?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.
?>

Javascript




<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

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 = 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




<?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.
?>

Javascript




<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

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 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.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :