Amazon Coding Question – Solved

12 Live
Prime Jumps There is a game that is played as follows: There is a pawn located at cell 0, and its score is 0. There is a row of n cells numbered from 0 to n-1 from left to right. Each cell has a value, and the first cell always has a value of 0. In a single move, the pawn can move either: one cell to the right, or some number p cells to the right where p is a prime number ending in 3, e.g., 3 and 13. The pawn cannot move beyond the last cell. When the pawn enters a cell, its value is added to the score. The game ends after the pawn lands on the last cell at n-1. Determine the maximum possible score. Example cell = [0, -10, -20, -30, 50] There are n = 5 cells in the array, with cells numbered from 0 to 4.

Asked in: Amazon

Image of the Question

Question Image

All Testcases Passed βœ”



Passcode Image

Solution


#!/bin/python3

import math
import os
// ... rest of solution available after purchase

πŸ”’ Please login to view the solution

Explanation


```
To approach the "Prime Jumps" problem, begin by carefully understanding the game mechanics and the constraints on the pawn's movement. The goal is to maximize the total score collected by moving the pawn from the first cell (index 0) to the last cell (index n-1), summing the values of all visited cells along the way.

Step 1: Understand the Moves Allowed
- The pawn starts at cell 0 with a score initialized at 0.
- At each move, it can move one cell to the right or jump a prime number of cells to the right where the prime ends with the digit 3 (e.g., 3, 13, 23, 43, etc.).
- The pawn cannot overshoot beyond the last cell, so the jump length must be within the range of the array indices.

Step 2: Analyze the Problem Constraints and Input
- The array contains n cells, each with an integer value that can be positive, negative, or zero.
- The first cell's value is always 0.
- The pawn accumulates score by adding the value of the cell it lands on after each move.
- The objective is to maximize this cumulative score upon reaching the last cell.

Step 3: Identify the Problem Type
- This is essentially an optimization problem that involves making sequential decisions under constraints.
- It can be modeled as a dynamic programming (DP) problem where you try to find the best score achievable at each cell based on possible previous positions.
- Since the pawn can move from various cells, some jumps could skip negative values or land on high-value cells to maximize the total.

Step 4: Define State and Transition
- The DP state can be defined as the maximum score achievable when the pawn lands on cell i.
- At the start, the maximum score for cell 0 is 0 because the pawn begins there.
- To find the maximum score at cell i, consider all possible previous positions from which the pawn could have arrived at i.
- These previous positions include:
- The cell i-1 (a move of 1 cell)
- Any cell i-p where p is a prime ending with 3 and i-p >= 0 (the pawn jumped p cells to reach i)
- For each valid previous position, the maximum score to reach i is the maximum of (score at previous position + value at cell i).

Step 5: Precompute the Prime Jumps
- Since the prime jumps are constrained to primes ending with 3, and the jumps must be within the range of the board, precompute all prime numbers ending with 3 up to n (the size of the cells).
- This precomputation will avoid recalculating primes repeatedly and will optimize the solution.

Step 6: Iterative DP Calculation
- Initialize a DP array of size n with very low values (like negative infinity) except for the first cell which is 0.
- Iterate through cells from left to right starting from 1 to n-1.
- For each cell i, calculate the maximum score by considering:
- DP[i-1] + cell value at i (for the one-step move)
- DP[i-p] + cell value at i for every prime jump p where i-p >= 0
- Update DP[i] with the maximum of these possible scores.

Step 7: Result Extraction
- After filling the DP array, the value at DP[n-1] will represent the maximum possible score achievable when the pawn reaches the last cell.

Step 8: Handle Negative Values and Edge Cases
- Since some cell values can be negative, it is important to choose jumps that skip negative cells if possible.
- For example, jumping over a sequence of negative values to land on a high positive value might be beneficial.
- Also, ensure to handle the case when there is no valid jump other than moving one cell at a time.
- Additionally, if all possible paths yield negative or zero values, the DP should correctly reflect the maximum achievable (which might be 0 or a negative number depending on the board).

Step 9: Complexity Considerations
- The DP approach is efficient as you only compute each cell once and consider possible jumps.
- Precomputing primes up to n is manageable and can be done using standard prime sieve methods.
- The overall time complexity is roughly O(n * k) where k is the number of prime jumps considered. Since primes ending with 3 are sparse, k is relatively small compared to n.

Step 10: Verify with Example
- Take the example given with cells = [0, -10, -20, -30, 50].
- Compute DP values by evaluating all possible moves and prime jumps.
- Confirm that the approach identifies the path that yields the maximum score, which may include jumping over negative values to reach the high-value last cell.

Summary:
- Model the problem using dynamic programming to store maximum scores for each cell.
- Use precomputed primes ending with 3 to consider all prime jumps.
- At each cell, update the maximum score by considering all valid moves from previous cells.
- The DP value at the last cell will give the final answer.
- This approach ensures an optimal and efficient solution.
```


Related Questions