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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment2.2
Student Name: Manohar Chaudhary UID: 21BCS4422
Branch: B.E-CSE Section/Group: FL-603-B
Semester: 6th Date of Performance: 27-02-24
Subject Name: AP Lab-2 Subject Code: 21CSP-351

1. Aim: To Demonstrate the Concept of Graph.

2. Objective:
a) The Objective is to determine whether Player 1 can win a game played with
an integer array where two players take turns selecting numbers from either
end of the array, adding to their score. Player 1 starts the game, and the
game ends when there are no more elements. The task is to ascertain if
Player 1 can achieve a higher or equal score to Player 2, assuming optimal
play, and returning true if so, false otherwise.

b) The objective is to find the letter that was added to string t, which was
generated by randomly shuffling string s and then adding one more letter at
a random position. The task is to identify and return the additional letter.

3. Code (A):
class Solution {
public boolean predictTheWinner(int[] nums) {
int n = nums.length;
int[][][] table = new int[n][n][2];
int maxScore = recurse(true, table, 0, n - 1, nums);
int total = Arrays.stream(nums).sum();
return maxScore * 2 >= total;
}
int recurse(boolean isP1, int[][][] table, int start, int end, int[] nums) {
if (start >= end) return isP1 ? nums[start] : 0;
if (table[start][end][isP1 ? 0 : 1] != 0) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return table[start][end][isP1 ? 0 : 1];


}

if (isP1) {
int chooseFirst = recurse(false, table, start + 1, end, nums);
int chooseLast = recurse(false, table, start, end - 1, nums);
table[start][end][0] = Math.max(chooseFirst + nums[start],
chooseLast + nums[end]);
return table[start][end][0];
} else {
int chooseFirst = recurse(true, table, start + 1, end, nums);
int chooseLast = recurse(true, table, start, end - 1, nums);
table[start][end][1] = Math.min(chooseFirst, chooseLast);
return table[start][end][1];
}
}
}

• Code (B):
class Solution {
public:
char findTheDifference(string s, string t)
{
for(int i=0;i<s.size();i++)
t[i+1]+=t[i]-s[i];
return t[t.size()-1];
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Output (A):

• Output (B):

5. Learning Outcomes

➢ Learnt the concept of graphs.


➢ Learnt about dynamic programming.
➢ Learnt String manipulation techniques.
➢ Learnt the concepts of BFS and DFS.

You might also like