Professional Documents
Culture Documents
Ap All
Ap All
1. Aim:
● To Solve the Jump Game 2
● ● To Solve the 3 SUM Problem
2. Objective:
● You are given a 0-indexed array of integers nums of length n. You are initially
positioned at nums[0].
● Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such
that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
class Solu)on {
Arrays.sort(nums);
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
if (sum == target) {
j++;
k--;
j++;
} else {
k--;
output.addAll(s);
return output;
}
Experiment1.2
Probem2: https://leetcode.com/problems/repeated-string-match/
2. Objective:
The problems are to find an integer s, called a valid shift where 0 ≤ s < n-m and T [s+1……s+m]
= P [1…… m]. In other words, to find even if P in T, i.e., where P is a substring of T. The items of
P and T are characters drawn from some finite alphabet such as {0, 1} or {A, B.
class Solu)on {
public int
repeatedStringMatch(String a, String b) {
String copyA = a; int count
=1; int repeat =
b.length()/a.length(); for(int
i=0;i<repeat+2;i++){
if(a.contains(b)){
return count;
}
else{
a+=copyA;
count++;
}
}
return -1;
}
}
Time Complexity: O(N * M)
Learning Outcomes :
• Understanding String Repetition.
• Substring Matching Strategies.
•Problem Solving Skills.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 1.3
Student Name: Yash Raj UID: 21BCS11765
Branch: BE-CSE Section/Group: CC-649-A
Semester: 6th Date of Performance: 31-01-2024
Subject Name: Advance Programming-2 Subject Code: 21CSP-251
1. Aim:
• To Solve the Last Stone Weight.
• To Solve the Cheapest Flight Booking with K stops.
2. Objective:
• You are given an array of integers stones where stones[i] is the weight of
the ith stone. We are playing a game with the stones. On each turn, we
choose the heaviest two stones and smash them together. Suppose the
heaviest two stones have weights x and y with x <= y.
• There are n cities connected by some number of flights. You are given an
array flights where flights[i] = [fromi, toi, pricei] indicates that there is a
flight from city fromi to city toi with cost pricei.
if(val1>val2 || val2>val1) {
list.add(Math.abs(val1-val2));
}
Collections.sort(list);
}
if(list.size()==1) {
return list.get(0);
}
else {
return 0;
}
}
}
class Solution {
public int findCheapestPrice(int n, int[][] a, int src, int dst, int k) {
List<List<List<Integer>>> graph = new ArrayList<>();
int i,m=a.length;
for(i=0;i<n;i++)
graph.add(new ArrayList<>());
for(i=0;i<m;i++)
{
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes:
2.Problem-Solving Skills.
3.Optimization Techniques.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment – 2.1
Student Name: Yash Raj UID: 21BCS11765
Branch: BE-CSE Section/Group: 649 ‘A’
Date of Performance: 7th Feb 2024 Semester: 6
Subject Name: Advanced Programming Lab - II Subject Code: 21CSP-351
1. Aim:
• To Solve symmetric tree.
• To Solve balanced binary tree.
2. Objective:
Problem 1:
Algorithm:
1) isMirror Function:
• This function takes two tree nodes, left and right, as input and checks
if they are mirror images of each other.
• If both left and right are nullptr, it returns true, indicating that they are
symmetric.
• If either left or right is nullptr (but not both), it returns false, indicating
asymmetry.
• Otherwise, it checks if the values of the current nodes are equal and
recursively calls isMirror for the left subtree of left with the right
subtree of right and vice versa.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
•If all recursive calls return true and the values of the current nodes are
equal, it returns true; otherwise, it returns false.
2) isSymmetric Function:
• This function checks if the given tree root is symmetric.
• If root is nullptr, it returns true (an empty tree is symmetric by
definition).
• Otherwise, it calls isMirror with the left and right subtrees of the root.
• If isMirror returns true, it means the tree is symmetric, so it returns
true; otherwise, it returns false.
Code:
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) {
return false;
}
Output:
Problem 2: Algorithm:
Code:
class Solution {
public boolean isBalanced(TreeNode root) {
// If the tree is empty, we can say it’s balanced...
if (root == null) return true;
// Height Function will return -1, when it’s an unbalanced tree...
if (Height(root) == -1) return false;
return true;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes:
Experiment – 2.2
Student Name: Yash Raj UID: 21BCS11765
Branch: BE-CSE Section/Group: 649 ‘A’
Date of Performance: 21st Feb 2024 Semester: 6
Subject Name: Advanced Programming Lab - II Subject Code: 21CSP-351
1. Aim:
To Solve the skyline problem.
To Solve is graph bipartite.
2. Objective:
Problem 1:
Algorithm:
Code:
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<vector<int>> ans;
multiset<int> pq{0};
for(auto b: buildings){
points.push_back({b[0], -b[2]});
points.push_back({b[1], b[2]});
}
sort(points.begin(), points.end());
int ongoingHeight = 0;
return ans;
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Problem 2:
Algorithm:
Code:
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int len = graph.size();
stack<int> s;
vector<int> vis(len);
for (int i = 0; i < len; i++) {
if (vis[i] > 0) continue;
vis[i] = 1;
s.push(i);
while (s.size() > 0) {
int curr = s.top();
s.pop();
vector<int> edges = graph[curr];
for (int next:edges)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (vis[next] == 0) {
vis[next] = vis[curr] ^ 3;
s.push(next);
} else if (vis[curr] == vis[next]) return false;
}
}
return true;
}
};
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes:
1. Aim:
• To Solve Count and Say
• To Solve Jewels and Stones
2. Objective:
• Given a string representing types of stones that are jewels (J) and another
string representing the stones you have (S), the goal is to determine how
many stones in S are also jewels represented by J. The task is to provide
an efficient solution for this counting problem.
class Solution {
if(n==1)
return "1";
String s=countAndSay(n-1);
int c=0;
StringBuilder ans=new StringBuilder();
for(int i=0;i<s.length();i++){
c++;
if(i==s.length()-1 || s.charAt(i)!=s.charAt(i+1)){
ans.append(c).append(s.charAt(i));
c=0;
return ans.toString();
Learning Outcomes:
1. Aim:
● To Solve the candy Problem
● To Solve Time to buy and sell the stock II
2. Objective:
● There are n children standing in a line. Each child is assigned a rating value given in the integer array
ratings.
● You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On
each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the
stock at any time. However, you can buy it then immediately sell it on the same day.
3. Algo. /Approach and output:
class Solution {
public:
int candy(vector<int> &ratings)
{
int size=ratings.size();
if(size<=1)
return size;
vector<int> num(size,1);
for (int i = 1; i < size; i++)
{
if(ratings[i]>ratings[i-1])
num[i]=num[i-1]+1;
}
for (int i= size-1; i>0 ; i--)
{
if(ratings[i-1]>ratings[i])
num[i-1]=max(num[i]+1,num[i-1]);
}
int result=0;
for (int i = 0; i < size; i++)
{
result+=num[i];
// cout<<num[i]<<" ";
}
return result;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit=0;
for(int i=1;i<prices.size();i++){
if(prices[i]>prices[i-1]){
profit+=(prices[i]-prices[i-1]);
}
}
return profit;
}
};
Experiment 3.2
1. Aim:
● To Solve the binary watch
● To Solve the Sticker to spell word
2. Objective:
● A binary watch has 4 LEDs on the top to represent the hours (0-11), and
6 LEDs on the bottom to represent the minutes (0-59). Each LED
represents a zero or one, with the least significant bit on the right.
● You would like to spell out the given string target by cutting
individual letters from your collection of stickers and rearranging
them. You can use each sticker more than once if you want, and you have
infinite quantities of each sticker.
Return the minimum number of stickers that you need to spell out target.
If the task is impossible, return -1.
class Solution {
public:
vector<string> readBinaryWatch(int turnedOn) {
vector<string> result;
for(int h=0;h<12;h++){
for(int m=0;m<60;m++){
if((__builtin_popcount(h)+__builtin_popcount(m))==turnedOn){
result.push_back(to_string(h)+(m<10 ? ":0" :
":")+to_string(m));
}
}
}
return result;
}
};
class Solution {
public:
int minStickers(vector<string>& stickers, string& target) {
int n = size(stickers);
unordered_set<string> visited;
vector<vector<int>> s_frequencies(n, vector<int>(26, 0));
for (int i = 0; i < n; ++i)
for (auto& c : stickers[i])
++s_frequencies[i][c - 'a'];
vector<int> t_frequency(26, 0);
for (auto& c : target)
++t_frequency[c - 'a'];
queue<vector<int>> q;
q.push(t_frequency);
return -1;
}
};
Experiment 3.3
1. Aim:
● To Solve the Best time to buy and sell the stock
● To Solve the longest Palindromic substring
2. Objective:
● You are given an array prices where prices[i] is the price of a given stock
on the ith day.You want to maximize your profit by choosing a single day to
buy one stock and choosing a different day in the future to sell that
stock.
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.empty()){
return 0;
}
int maxprice=0;
int minprice=prices[0];
for(int i=1;i<prices.size();i++){
if(prices[i]<minprice){
minprice=prices[i];
}
else{
int currprofit=prices[i]-minprice;
maxprice=max(maxprice,currprofit);
}
}
return maxprice;
}
};
class Solution {
public:
std::string longestPalindrome(std::string s) {
if (s.length() <= 1) {
return s;
}
return max_str;
}
};