Akhanda Complex Ap
Akhanda Complex Ap
Akhanda Complex Ap
Complex Problems
Student Name: Akhanda Pal Biswas UID: 21BCS5828
Branch: CSE Section/Group: KRG_CC_2(A)
th
Semester: 06 Date of Performance:02/04/2024
Subject Name: Advance Programming Lab-II Subject Code: 21CSP-351
Question 1
Code:
#include <iostream>
#include <vector>
using namespace std;
Output:
Question 2
Serialization is the process of converting a data structure or object into a
sequence of bits so that it can be stored in a file or memory buffer, or
transmitted across a network connection link to be reconstructed later in the
same or another computer environment. Design an algorithm to serialize and
deserialize a binary tree. There is no restriction on how your
serialization/deserialization algorithm should work. You just need to ensure
that a binary tree can be serialized to a string and this string can be
deserialized to the original tree structure. Clarification: The input/output
format is the same as how LeetCode serializes a binary tree. You do not
necessarily need to follow this format, so please be creative and come up
with different approaches yourself.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if (!root)
return "null";
return to_string(root->val) + "," + serialize(root->left) + "," + serialize(root->right);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
stringstream ss(data);
queue<string> nodes;
string str;
while (getline(ss, str, ','))
nodes.push(str);
return buildTree(nodes);
}
private:
TreeNode* buildTree(queue<string>& nodes) {
string val = nodes.front();
nodes.pop();
if (val == "null")
return nullptr;
int main() {
// Example usage
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
root->right = new TreeNode(3);
root->right->left = new TreeNode(4);
root->right->right = new TreeNode(5);
Codec codec;
string serialized = codec.serialize(root);
cout << "Serialized tree: " << serialized << endl;
TreeNode* deserialized = codec.deserialize(serialized);
cout << "Deserialized tree: ";
printTree(deserialized);
cout << endl;
return 0;
}
Output:
Question 3
Code:
#include <iostream>
#include <queue>
#include <vector>
#include <utility>
#include <climits>
// BFS
while (!q.empty()) {
Cell current = q.front();
q.pop();
return 0;
}
Output:
Question 4
Given an array arr[] of size N, the task is to find the length of the Longest
Increasing Subsequence (LIS) i.e., the longest possible subsequence in
which the elements of the subsequence are sorted in increasing order.
Examples: Input: arr[] = {3, 10, 2, 1, 20} Output: 3 Explanation: The longest
increasing subsequence is 3, 10, 20Given an arrayarr[] of size N, the task is
to find the length of the Longest Increasing Subsequence (LIS) i.e., the
longest possible subsequence in which the elements of the subsequence are
sorted in increasing order. Examples: Input: arr[] = {3, 10, 2, 1, 20} Output:
3 Explanation: The longest increasing
subsequence is 3, 10, 20.
Code:
#include <iostream>
#include <vector>
return maxLength;
}
int main() {
vector<int> arr = {3, 10, 2, 1, 20};
cout << "Length of Longest Increasing Subsequence: " <<
longestIncreasingSubsequence(arr) << endl;
return 0;
}
Output:
Question 5
Given two strings, string, and pattern, the task is to find the smallest substring
in string containing all characters of pattern. Examples:Input: string = “this
is a test string”, pattern = “tist” Output: “t stri” Explanation: “t stri” contains
all the characters of pattern.
Input: string = “geeksforgeeks”, pattern = “ork”
Output: “ksfor”Given two strings, string, and pattern, the task is to findthe
smallest substring in string containing all characters of pattern.
Code:
#include <iostream>
#include <string>
#include <unordered_map>
#include <climits>
if (startIdx == -1) {
return ""; // No substring containing all characters of the pattern found
} else {
return str.substr(startIdx, minLength);}}
int main() {
string str1 = "this is a test string";
string pattern1 = "tist";
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cout << "Smallest substring containing all characters of pattern: "
<< smallestSubstring(str1, pattern1) << endl;
string str2 = "geeksforgeeks";
string pattern2 = "ork";
cout << "Smallest substring containing all characters of pattern: "
<< smallestSubstring(str2, pattern2) << endl;
return 0;
}
Output:
Question 6
Code:
#include <iostream>
#include <vector>
#include <deque>
vector<int> result;
deque<int> dq;
dq.pop_back();
dq.push_back(i);
result.push_back(arr[dq.front()]);
dq.pop_front();
dq.pop_back();
dq.push_back(i);
result.push_back(arr[dq.front()]);
return result;
int main() {
int k = 3;
cout << "Maximum of each subarray of size " << k << ":\n";
return 0;
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Question 7:
Given a Binary Tree with all unique values and two node values, n1 and
n2. The task is to find the lowest common ancestor of the given two
nodes. We may assume that either both n1 and n2 are present in the tree
or none of them are present. LCA: It is the first common ancestor of both
the nodes n1 and n2 from the bottom of the tree.
Code:
#include <iostream>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
};
return root;
return root;}
if (lca != nullptr) {
cout << "Lowest Common Ancestor of " << n1->val << " and " << n2->val <<
" is: " << lca->val << endl;
} else {
cout << "One or both of the nodes are not present in the tree." << endl;}}
Output:
Question 8:
Given an array of points [i][j] of size N, where points[i] represents a
balloon over the area of X-coordinates from points[i][0] to points[i][1].
The Y-coordinates don’t matter. All the balloons are required to be burst.
To burst a balloon, an arrow can be launched at point (x, 0) and it travels
vertically upwards and bursts all the balloons which satisfy the condition
points[i][0] <= x <= points[i][1]. The task is to find the minimum number
of arrows required to burst all the balloons.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (points.empty()) {
return 0;
});
int arrows = 1;
arrows++;
end = points[i][1];}}
return arrows;
int main() {
vector<vector<int>> balloons = {{10, 16}, {2, 8}, {1, 6}, {7, 12}};
return 0;
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Question 9:
Given a sorted dictionary (array of words) of an alien language, find the
order of characters in the language. Examples: Input: words[] = {“baa”,
“abcd”, “abca”, “cab”, “cad”} Output: Order of characters is ‘b’, ‘d’, ‘a’,
‘c’.
Code:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
if (word1[j] != word2[j]) {
graph[word1[j]].insert(word2[j]);
break;}}}}
buildGraph(words, graph);
inDegree[neighbor]++;}}
queue<char> q;
if (inDegree[entry.first] == 0) {
q.push(entry.first);}}
while (!q.empty()) {
q.pop();
result += curr;
inDegree[neighbor]--;
if (inDegree[neighbor] == 0) {
return result;
int main() {
cout << "Order of characters is: " << order << endl;
return 0;}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Question 10:
Given two strings str1 and str2 of length M and N respectively and below
operations that can be performed on str1. Find the minimum number of
edits (operations) to convert ‘str1‘into ‘str2‘. Operation 1 (INSERT):
Insert any character before or after any index of str1 Operation 2
(REMOVE): Remove a character of str1 Operation 3 (Replace): Replace
a character at any index of str1 with some other character. Note: All of
the above operations are of equal cost. Examples: Input: str1 = “geek”,
str2 = “geek” Output: 1 Explanation: We can convert str1 into str2 by
inserting a ‘s’ between two consecutive ‘e’ in str2. Input: str1 = “cat”,
str2 = “cut” Output: 1.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
int m = str1.length();
int n = str2.length();
dp[i][0] = i;
dp[0][j] = j;
// Fill DP table
} else {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
return dp[m][n];
int main() {
str1 = "cat";
str2 = "cut";
return 0;
Output:
Learning Outcomes:
1. Learnt about complex problems.
2. Learnt about important concepts like Dynamic Programming,
Recursion etc.
3. Learnt about different Programming concepts.