0% found this document useful (0 votes)
23 views5 pages

AP 2.2 Tanmay

The document describes two programming tasks involving strings and arrays. The first task involves finding the added letter between two related strings. The second task involves predicting the winner of a game between two players taking turns removing numbers from an array.

Uploaded by

eyboss0099
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

AP 2.2 Tanmay

The document describes two programming tasks involving strings and arrays. The first task involves finding the added letter between two related strings. The second task involves predicting the winner of a game between two players taking turns removing numbers from an array.

Uploaded by

eyboss0099
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment: 2.2

Student Name: Tanmay Ranjan UID: 22BCS80072


Branch: BE-CSE Section/Group: 21BCS_SC-902-A
Semester: 6 Date of Performance:23-02-2024
Subject Name: Advanced Programming lab-2 Subject Code:21CSP-351

Objective: To demonstrate the concept of Graph.

Task 1

Aim: Find the Difference

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random
position.

Return the letter that was added to t.

Algorithm:

1. Initialize two integer variables, sum1 and sum2, to zero.


2. Iterate through each character in strings s and t.
3. Add the ASCII value of each character to sum1 and sum2, respectively.
4. Add the ASCII value of the last character in t to sum2.
5. Calculate the difference between sum2 and sum1.
6. Return the character corresponding to the difference.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Code:
class Solution {
public:
char findTheDifference(string s, string t)
{
for(int i=0;i<s.size();i++)
t[i+1]+=t[i]-s[i]; //Passing the diff: (t[i]-s[i]) to t[i+1]
return t[t.size()-1]; //The diff will be carried over to the last element eventually
}
};
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Task 2
Aim: Predict the Winner

You are given an integer array nums. Two players are playing a game with this array: player 1 and
player 2.

Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score
of 0. At each turn, the player takes one of the numbers from either end of the array
(i.e., nums[0] or nums[nums.length - 1]) which reduces the size of the array by 1. The player adds the
chosen number to their score. The game ends when there are no more elements in the array.

Return true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still
the winner, and you should also return true. You may assume that both players are playing
optimally.

Algorithm:

1. Define a recursive function solve that takes nums, start index i, end index j, and chance indicating
player's turn.
2. Base case: If i surpasses j, return 0.
3. If it's player's turn (chance = = 0), return max of choosing either nums[i] or nums[j] and
recursively call for opponent's turn.
4. If it's opponent's turn (chance = =1), return the minimum of recursive calls for both possible
moves.
5. Define predictTheWinner function to calculate the total sum of nums and call solve initially.
6. Calculate the remaining sum after player one's turn.
7. Return true if player one's score is greater or equal to remaining sum.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Code:
class Solution {
public:

vector<vector<int>> dp;

int helper(vector<int> arr, int st, int end){

if(st==end) return arr[st];

if(dp[st][end]!=-1) return dp[st][end];

int a = arr[st] - helper(arr,st+1,end);


int b = arr[end] - helper(arr,st,end-1);

return dp[st][end]=max(a,b);
}

bool predictTheWinner(vector<int>& arr) {

dp.clear();

dp.resize(22,vector<int>(22,-1));
int ans = helper(arr,0,arr.size()-1);

if(ans>=0) return true;


else return false;
}

};
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning Outcomes:

1. Recognize how recursive functions traverse decision paths resembling a tree-like


structure, where each node represents a function call.
2. Realize the significance of memorization or tabulation to optimize recursive solutions,
reducing redundant computations and improving efficiency.
3. Learn to model game scenarios using recursive functions, considering players' optimal
strategies and outcomes.
4. Apply the concept of graph traversal implicitly, as recursive calls explore different
decision paths analogous to traversing a directed acyclic graph (DAG) representing
game states.

You might also like