'Explanation of test case in the prisoner wall jump program

This would be the general problem statement:

A prisoner escapes from the jail by jumping over N walls each with height of each wall given in an array. He can jump x meters of height, but after every jump he slips y meters due to some uncontrollable factors(wind, slippery wall, etc).

Similar problem statement mentioned here

The programming task given was to debug a function which included four parameters -

NoOfJumps(int x, int y, int N, int Height[])
  1. Number of meters he jumps
  2. Number of meters he slips down the wall
  3. Number of walls
  4. Height of the walls as an array

The first test case was for parameters - (10, 1, 1, {10})

10 being the meters he jumps, 1 meter he slips down, Number of walls being 1, and height of the wall being 10. Now:

effectiveJump = x - y = 9.

So he would have to jump twice to jump over the walls. So, this function should return 2 (total number of jumps required to escape).

There was also another test case for the parameters - (3, 1, 5, {20,5,12,11,3})

3 being the meters he jumps, 1 meter he slips down, Number of walls being 5, and height of the walls given as 20m, 5m, 12m, 11m, 3m. Now:

effectiveJump = x - y = 2.

We were given the output for the above parameter values as 24.

NoOfJumps(3, 1, 5, {20,5,12,11,3})

I can't understand how this output value is obtained. How exactly are the walls arranged?

I can only think of one solution for the corner case, i.e, when the person jumps over the wall

(when (x) > remaining height of the wall),

he should not slip down else I can't obtain the required solution. For example, in the second test case at first wall, when the person is at 18m height, and he jumps 3m to 21m and doesn't slip down as he has crossed that wall. Next he starts jumping from 21 and not 20. The sequence of jumping would be :

0->2->4->6->8->10->12->14->16->18->21->23->26->28->30->32->34->36->39->41->43->45->47->50->53

Assuming walls at height, 20, 25, 37, 48, 51.

Is this a correct assumption for solving the problem?



Solution 1:[1]

C code on given case 2, will work for case 1 on changing the parameters to (10,1,1,10).

#include<conio.h>
#include<stdio.h>
int jump(int x,int y,int n,int z[]);
int jump(int x,int y,int n,int z[])
{
    int i, j, countjump, total = 0, extra = 0;
    clrscr();
    printf("\n%d\n", n);
    for (i = 0; i < n; i++) {
        printf("\n%d", z[i]);
    }
    printf("\n");
    for (j = 0; j < n; j++) {
        countjump = 1;
        z[j] = z[j] + (extra) - x;
        while (z[j] >= 0) {
            z[j] = z[j] + y;
            z[j] = z[j] - x;
            countjump = countjump + 1;
            if (z[j] < 0) {
                extra = z[j];
            }
        }
        total = (countjump + total);
    }
    return total;
}
void main()
{
    int res, manjump = 3, slip = 1, nwalls = 5;
    int wallheights[] = {20, 5, 12, 11, 3};
    clrscr();
    res = jump(manjump, slip, nwalls, wallheights);
    printf("\n\ntotal jumps:%d", res);
    getch();
}

Solution 2:[2]

Try this code. May not be optimized

$input1 = Jump Height

$input2 = Slipage

$input = Array of walls height

function GetJumpCount($input1,$input2,$input3)
{
    $jumps = 0;
    $wallsCrossed = 0;
    while($wallsCrossed != count($input3)){
        $jumps++;
        $input3[$wallsCrossed] = $input3[$wallsCrossed] - $input1;
        if($input3[$wallsCrossed] > 0){
            $input3[$wallsCrossed] = $input3[$wallsCrossed] + $input2;
        }else{
            $wallsCrossed++;
        }
    }
    return $jumps;
}

Solution 3:[3]

The walls come one after another. After jumping wall one the position should start from zero and not from the last jump height. For the first case the output should really be 1 as the height and jump are same. In the second test case, 24 is the right output. I've seen the exact same question on techgig contest. For the first test case the output should be 1. The test case had been explained by themselves where there is no slipping if the jump and height are same.

Solution 4:[4]

Try this

You don't require the number of walls as it equals to size of array

public class Jump {
    public static void main(String[] a) {
        int jump = 3;
        int slip = 1;
        int[] hights = {20,5,12,11,3};

        int count = 0;
        for (int hight : hights) {
            int temp = hight - jump;
            if (temp >= 0) {
                count = count + temp / (jump - slip)+1;
            }
            if (temp % (jump - slip) > 0) {
                count++;
            }
        }
        System.out.println(count);
    }
}

Solution 5:[5]

Logic is here Plz check if this solves your problem.

package puzeels;

public class Jump
{
    int jump=6;
    int slip=1;
    int numberOfWals=4;
    int height[] ={21,16,10,5};

    static int count=0;
    int wallheight=0;

    private int findJump()
    {
        for(int i=0;i<height.length;i++)
        {             
            wallheight=height[i];
            while((wallheight>0))
            {
                count=count+1;
                wallheight=wallheight-(jump-slip);
                System.out.println(wallheight+"  "+count);
            }
            System.out.println("Out of while loop");
        }
        return count;
    }
    public static void main(String arr[])
    {
        Jump obj = new Jump();
        int countOfJumps=obj.findJump();

        System.out.println("number of jumps is==>   "+countOfJumps);
    }
}

Solution 6:[6]

You can use this one.

Sample Code

public static int calculateJumps(int X, int Y, int height[]) {
    int tn=0,n;
    for(int i=0; i<height.length; i++) {
        if(height[i]<=X) {
            tn+=1;
            continue;
        }
        n=((height[i]-X)/(X-Y));
        n+=height[i]-((X-Y)*n)==X?1:2;
        tn+=n;
    }
    return tn;
}

You need to pass only X , Y and Array than you can get you output.

Solution 7:[7]

I think 12 is a wrong answer, as I tried this code I got 11, last jump doesn`t have a slip:

public static void main(String [] args) {

    int T;
    int jcapacity, jslip, nwalls;

    //BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

    Scanner sc = new Scanner(System.in);

    T = sc.nextInt();
    jcapacity = sc.nextInt();
    jslip = sc.nextInt();
    nwalls = sc.nextInt();
    int [] wallHeightArr = new int [nwalls];

    for (int i = 0; i< nwalls; i++) {

        wallHeightArr[i] = sc.nextInt();
    }

    sc.close();

    while(T-->0) {

        int distance = log(jcapacity,jslip,wallHeightArr);
        System.out.println(distance);
    }
}

private static int log(int jcapacity, int jslip, int[] wallHeightArr) {
    // TODO Auto-generated method stub

    int distance = 0;

    for(int i = 0; i< wallHeightArr.length; i++) {

        int cHeight = 0;
        int count = 0;

        while (wallHeightArr[i] - cHeight > jcapacity) {

            cHeight += (jcapacity - jslip);
            count++;
        }
        count++;
        distance += count;
    }

    return distance;
}

Solution 8:[8]

def jumpTheifCount(arr, X, Y):
    jump = 0
    remheight = 0

    for i in range(len(arr)):
        if X == arr[i]:
            jump = jump + 1
            continue
        if X < arr[i]:
            jump = jump + 1
            remheight = arr[i] - X + Y
        if remheight > X:
            jump = jump + 1
            remheight = arr[i] - X + Y
        if remheight < X:
            jump = jump + 1
            continue
    return jump

arr = [11, 10, 10, 9]
X = 10
Y = 1

print(jumpTheifCount(arr, X, Y))

Solution 9:[9]

check if this solves your problem

def GetJumpCount(jump, slips, walls):
    """
    @jump:int, Height of 1 jump
    @slips:int, height of slip
    @walls:array, height of walls
    """
    jumps = []
    for wall_height in walls:
        wall_jump = 1
        wall_height -= jump
        while wall_height > 0:
            wall_height += slips
            wall_height -= jump
            wall_jump += 1
        jumps.append(wall_jump)
    return sum(jumps)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S.B
Solution 2
Solution 3
Solution 4
Solution 5
Solution 6
Solution 7 Abhishek Sa
Solution 8 S.B
Solution 9 S.B