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

DEPARTMENTOF

COMPUTERSCIENCE&ENGINEERING

Experiment-2.2

Student Name: Jaydeep Manchanda UID: 20BCS1207


Branch: CSE Section/Group: 20BCS-DM-704/A
Semester: 6th Date of Performance:12/04/2023
Subject Name: Competitive Coding II Subject Code: 20CSP- 351

Aim - To demonstrate the concept of Graph


Objective-
 The objective is to build problem solving capability and to learn the basic concepts of data
structures.

 The implementation of Find the difference which shows and brushes up the concept of Graph and
dynamic programming can be solved through various approaches.

 The implementation of Predict the winner using .

Problem1: Find the difference

https://leetcode.com/problems/find-the-difference/
Code –

class Solution {
public:
char findTheDifference(string s, string t) {
char count=0;
for(char c : s)
count^=c;
for(char ct : t)
count^=ct;
return count;
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

};
OUTPUT:

Probem2: Predict the winner

https://leetcode.com/problems/predict-the-winner/

Code –

class Solution {
public:
long long dp[21][21][2];
int check(vector<int>& nums,int i,int j,int ch){
if(i>j)
return 0;
if(dp[i][j][ch]!=-1)
return dp[i][j][ch];
if(ch==0)
return
dp[i][j][ch]=max(nums[i]+check(nums,i+1,j,1),nums[j]+check(
nums,i,j-1,1));
else
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

return
dp[i][j][ch]=min(check(nums,i+1,j,0),check(nums,i,j-1,0));
}
bool PredictTheWinner(vector<int>& nums) {
long long sum=0;
memset(dp,-1,sizeof(dp));
for(int i=0;i<nums.size();i++)
sum+=nums[i];
long long first=check(nums,0,nums.size()-1,0);
sum-=first;
if(first>=sum)
return true;
else
return false;
}
};
Output –

You might also like