Can somebody help me with Mockvita 2 HOP GAME question?

Problem Description

Dr Felix Kline, the Math teacher at Gauss School introduced the following game to teach his students problem solving. He places a series of “hopping stones” (pieces of paper) in a line with points (a positive number) marked on each of the stones.

Students start from one end and hop to the other end. One can step on a stone and add the number on the stone to their cumulative score or jump over a stone and land on the next stone. In this case, they get twice the points marked on the stone they land but do not get the points marked on the stone they jumped over.

At most once in the journey, the student is allowed (if they choose) to do a “double jump”– that is, they jump over two consecutive stones – where they would get three times the points of the stone they land on, but not the points of the stone they jump over.

The teacher expected his students to do some thinking and come up with a plan to get the maximum score possible. Given the numbers on the sequence of stones, write a program to determine the maximum score possible.


The number of stones in the sequence< 30

Input Format

The first line contains N, the number of integers (this is a positive integer)

The next line contains the N points (each a positive integer) separated by commas. These are the points on the stones in the order the stones are placed.


One integer representing the maximum score

Test Case


Example 1







There are 3 stones (N=3), and the points (in the order laid out) are 4,2 and 3 respectively.

If we step on the first stone and skip the second to get 4 + 2 x 3 = 10. A double jump to the third stone will get only 9. Hence the result is 10, and the double jump is not used

Example 2







N=6, and the sequence of points is given.One way of getting 35 is to start with a double jump to stone 3 (3 x 6=18), go to stone 4 (7) and jump to stone 6 (10 points) for a total of 35. The double jump was used only once, and the result is 35.

1 Like

Yes , it’s a bit tough . Can any one help us out please :slight_smile:

I solved it using simple dp.
Logic : for each stone calculate all possible way out to reach that stone . There are 3 ways to reach a stone :

  1. By using no hop
  2. By using a single hop
  3. By using a double hop
    What you need to take care is that whenever you use the double hop, use a flag so that you dont use double hop for the rest of the stones. The maximum value one can achieve in this way on the last stone is the answer .
    Python Soln : Hop Game
    Java Soln : Hop Game
1 Like

Thank you sayan_000

I still have many questions can we connect in LinkedIn

Yeah sure… will try to clarify your doubts

can you please connect in hangouts???

Where can I test my solution? Do you have the problem link?

You have to register for Codevita within 24th june … Only then you will be able to test your solution. Practice round will be active till 24th june


Here is memoized solution for given problem
import java.util.;
public class Main
static long dp[][][];
public static long getAns(int arr[],int index,int jumptype,int allowed)
if(index>=arr.length)return Integer.MIN_VALUE;
if(index==arr.length-1)return jumptype
if(jumptype!=-1 && dp[index][jumptype][allowed]!=-1)return dp[index][jumptype][allowed];
long temp1=Integer.MIN_VALUE,temp2=Integer.MIN_VALUE,temp3=Integer.MIN_VALUE;
if(jumptype==-1)return Math.max(temp1,Math.max(temp2,temp3));
return dp[index][jumptype][allowed]=Math.max(temp1,Math.max(temp2,temp3));
public static void main(String[] args) {
Scanner kb=new Scanner(;
int n=kb.nextInt();
int arr[]=new int[n];
dp=new long[n+1][4][2];
for(int i=0;i<dp.length;i++)
for(int j=0;j<dp[i].length;j++)
for(int i=0;i<n;i++)arr[i]=kb.nextInt();