Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

POTS OF GOLD

TEST TIME ON THE

URL:
POTS OF GOLD

EXPLANATION

The "Pots of Gold" game can be a turn-based game where two players take

turns choosing pots of gold. Each pot of gold contains a certain number of

coins. The goal is to maximize the total number of coins collected by the

end of the game.


POTS OF GOLD

ALGORITHM
✔ Create an array to represent the pots of gold and initialize it with the number of

coins in each pot.

✔ Players take turns choosing pots.

✔ On each turn, a player can choose either the pot at the beginning or the pot at

the end of the array.

✔ Update the array after each turn by removing the chosen pot.

✔ Continue until no pots are left.

✔ The player with the maximum total number of coins wins.


POTS OF GOLD

PSEUDOCODE
function maxCoins(pots):

player1 = 0

player2 = 0

while pots is not empty:

// Player 1's turn

player1 += max(pots[0], pots[last])

remove chosen pot from pots

// Check if pots are empty

if pots is empty:

break
POTS OF GOLD

PSEUDOCODE
// Player 2's turn

player2 += max(pots[0], pots[last])

remove chosen pot from pots

// Determine the winner

if player1 > player2:

return "Player 1 wins with " + player1 + " coins."

else:

return "Player 2 wins with " + player2 + " coins."


POTS OF GOLD

EXAMPLE

In the Pots of Gold game, two players, A and B, take turns choosing pots from a line

of gold pots, each containing a certain number of coins. Players can pick a pot from

either end of the line during their alternating turns. The winner is the player with

the higher total coins at the end, and the goal for player A is to maximize their

coin collection, assuming player B plays optimally. In the given scenario:


POTS OF GOLD

EXAMPLE

Player A Player B

4, 6, 2 ,3 3

4, 6, 2 4

6, 2 6

2 2

9 coins 6 coins
POTS OF GOLD

EXPLANATION

Player A starts with pots [4, 6, 2, 3].

Player A chooses the pot at the end (3).

Player B responds with pots [4, 6, 2].

Player B chooses the pot at the beginning (4).

Player A is left with pots [6, 2].


POTS OF GOLD

EXPLANATION

Player A chooses the pot at the end (2).

Player B takes the remaining pot.

Player B chooses the only available pot (6).

At the end of the game, Player A has 9 coins, while Player B has 6 coins. Player A

successfully maximizes their coin collection, securing a victory with 9 coins

compared to Player B's 6 coins.


POTS OF GOLD

APPROACH TO SOLVE

1. Recursive Approach

2. Dynamic Programming Approach


POTS OF GOLD

APPROACH 1:RECURSIVE APPROACH

Pots of Gold game using a recursive approach. The game is modeled as

a two-player game where each player tries to maximize the number of coins

collected by choosing pots optimally. The recursive function explores all

possible choices for both players, and the maximum coins are calculated

based on the optimal strategy.


POTS OF GOLD

public class Main {

public static int findMaxCoins(int[] coin, int i, int j) {


if (i == j) return coin[i];
if (i + 1 == j) return Integer.max(coin[i], coin[j]);

int start = coin[i] + Integer.min(findMaxCoins(coin, i + 2, j), findMaxCoins(coin, i +


1, j - 1));
int end = coin[j] + Integer.min(findMaxCoins(coin, i + 1, j - 1), findMaxCoins(coin, i,
j - 2));

return Integer.max(start, end);


}

public static void main(String[] args) {


int[] coin = {4, 6, 2, 3};
System.out.print("Maximum coins collected by player: " + findMaxCoins(coin, 0,
coin.length - 1));
}
}
POTS OF GOLD

TIME AND SPACE COMPLEXITY

Time Complexity: Exponential (O(2^n)) due to the recursive exploration of

all possible choices.

Space Complexity: Linear (O(n)) due to the maximum depth of the recursion

being proportional to the number of pots.


POTS OF GOLD

APPROACH 2:DYNAMIC PROGRAMMING(MEMORIZATION)

This Java code utilizes dynamic programming with memorization to

find the maximum number of coins a player can collect, assuming both

players play optimally in a game of pots of gold arranged in a line.


POTS OF GOLD
APPROACH 2:DYNAMIC PROGRAMMING(MEMORIZATION)
public class Main {
public static int findMaxCoins(int[] coin, int i, int j, int[][] lookup) {
if (i == j) return coin[i];
if (i + 1 == j) return Math.max(coin[i], coin[j]);
if (lookup[i][j] == 0) {
int start = coin[i] + Math.min(findMaxCoins(coin, i + 2, j, lookup), findMaxCoins(coin,
i + 1, j - 1, lookup));
int end = coin[j] + Math.min(findMaxCoins(coin, i + 1, j - 1, lookup),
findMaxCoins(coin, i, j - 2, lookup));
lookup[i][j] = Math.max(start, end);
}
return lookup[i][j];
}

public static void main(String[] args) {


int[] coin = {4, 6, 2, 3};
int[][] lookup = new int[coin.length][coin.length];
System.out.println("Maximum coins collected by player: " + findMaxCoins(coin, 0, coin.length
- 1, lookup));
}
}
POTS OF GOLD

TIME AND SPACE COMPLEXITY

Time Complexity: O(n^2) where n is the number of pots.

Space Complexity: O(n^2) due to the memoization table.


POTS OF GOLD

TIME AND SPACE COMPLEXITY

Time Complexity: O(n^2) where n is the number of pots.

Space Complexity: O(n^2) due to the memoization table.


POTS OF GOLD

RECURSIVE APPROACH VS DYNAMIC PROGRAMMING APPROACH

1. The dynamic programming approach is generally better for the given

problem of maximizing coins in the pots of gold game.

2. It efficiently avoids redundant computations by storing and reusing

solutions to overlapping subproblems, leading to a significant

improvement in both time and space complexity compared to the recursive

approach.

3. This makes it more scalable and suitable for larger instances of the

problem.
INTERVIEW QUESTION

1. What is the objective of the "Pots of Gold" game?

Answer:The objective is to maximize the total number of coins

collected by choosing pots strategically in a turn-based game.


INTERVIEW QUESTION

2.How do players take turns in the "Pots of Gold" game?

Answer: Players take turns choosing pots, and each pot contains a

certain number of coins. The game continues until no pots are left.
INTERVIEW QUESTION

3. What is the significance of the recursive approach in solving the "Pots


of Gold" problem?

Answer: The recursive approach explores all possible choices of pots,

making it a straightforward way to understand the problem. However, it may

suffer from redundant computations.


INTERVIEW QUESTION

4. Why is dynamic programming used in the "Pots of Gold" problem?

Answer: Dynamic programming is employed to optimize the solution by

avoiding redundant calculations. It uses a memoization table to store and

reuse previously solved subproblems, improving efficiency.


INTERVIEW QUESTION

5. How does the dynamic programming approach enhance the efficiency of


solving the "Pots of Gold" problem?

Answer: Dynamic programming optimizes efficiency by storing solutions

to subproblems in a table, preventing the recalculation of previously

solved cases. This eliminates redundant computations, making it suitable

for larger instances of the problem.


/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like