LAB Assignment
LAB Assignment
nodes so that their order is reversed. The head pointer given may be null meaning that the ini al
list is empty.
Code:-
namespace std;
int data;
SinglyLinkedListNode* next;
SinglyLinkedListNode(int node_data) {
= nullptr;
};
SinglyLinkedListNode* head;
SinglyLinkedList() { this-
>head = nullptr;
if (!this->head) {
this->head = node;
} else {
temp = temp->next;
temp->next = node;
if (node) {
return prev;
};
int main() {
>> t; while
(t--) {
list->insertNode(data);
>printList(list->head); list->head =
return 0;
Input:-
1234
Ouput:-
list: 4->3->2->1
Time Complexity:
• O(n) where n is the number of nodes in the linked list. The reverse func on iterates through
all the nodes once to reverse the pointers.
Space Complexity:
• O(1) as we are reversing the linked list in place without using any extra space other than a
few pointers (prev, curr, next).
Constraints
Code:-
#include <iostream>
int data;
Node* le ;
Node* right;
Node(int value) {
data = value; le
= nullptr; right =
nullptr;
};
if (root == nullptr) {
return;
postOrder(root->le );
postOrder(root->right); cout
int main() {
postOrder(root); return 0;
Input:-
/ \
2 3
/\
4 5
Output:-
45231
Time Complexity:
• O(n) where n is the number of nodes in the binary tree. Each node is visited once.
Space Complexity:
• O(h) where h is the height of the binary tree due to recursive func on calls. In the worst
case, the space complexity can go up to O(n) if the tree is skewed.
Problem 3: You are given a pointer to the root of a binary search tree and values to be inserted into
the tree. Insert the values into their appropriate posi on in the binary search tree and return the
root of the updated binary tree.
Code:-
#include <iostream>
Node* le ;
Node* right;
Node(int value) {
data = value; le
= nullptr; right =
nullptr;
};
if (root == nullptr) {
= insert(root->le , data);
return root;
if (root == nullptr) {
return;
inOrder(root->le );
inOrder(root->right);
nodes: ";
Input:-
Number of nodes: 5
Node values: 10 5 15 3 7
Output:-
Problem 4: Huffman Coding assigns variable length codewords to fixed length input characters
based on their frequencies. More frequent characters are assigned shorter codewords and less
frequent characters are assigned longer codewords.
Code:-
#include <iostream>
#include <string>
char data;
int freq;
Node* right;
nullptr; this->right =
nullptr;
};
current = current->le ;
} else { current =
current->right;
int main() {
return 0;
Input:-
01111001100011010111100
Output:-
ABRACADABRA
Time Complexity:
• O(n) where n is the length of the encoded string. We traverse the string once and for each
character in the encoded string, we traverse the tree.
Space Complexity:
• O(1) for decoding since no extra space is used other than the tree and the encoded string.
• O(h) for storing the Huffman tree, where h is the height of the tree. In the worst case, the
space complexity of the tree is O(n), where n is the number of unique characters.
Code:-
le .push_back(arr[i]); } else if
equal.push_back(arr[i]);
} else {
right.push_back(arr[i]);
int main() {
return 0;
Input:-
57438
43578
Time Complexity:
Space Complexity:
• O(n): We use three addi onal arrays (le , equal, and right) that can together
store all elements of the original array.
Problem 6: Given an amount and the denomina ons of coins available, determine how many ways
change can be made for amount. There is a limitless supply of each coin type.
Code:-
getWays(int n, vector<int> c) {
coin];
return dp[n];
vector<int> coins(m);
return 0;
Input:-
34
8312
Output:-
Problem 7. Samantha and Sam are playing a numbers game. Given a number as a string, no leading
zeros, determine the sum of all integer values of substrings of the string.
Code:-
1000000007;
= (factor * 10 + 1) % MOD;
return total_sum;
endl; return 0;
Input:-
Input:
42
Output:
48
Problem 8: Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a calorie count, and
Marc can walk a distance to expend those calories. If Marc has eaten j cupcakes so far, a er ea ng
a cupcake with c calories he must walk at least 2i *c miles to maintain his weight.
Code:-
#include <iostream>
equivalent to (1 << i)
return totalMiles;
int main() {
vector<int> calorie(n);
return 0;
Input: 3
5 10 7
Output:
44
Problem 9. Priyanka works for an interna onal toy company that ships by container. Her task is to
the determine the lowest cost way to combine her orders for shipping. She has a list of item
weights. The shipping company has a requirement that all items loaded in a container must weigh
less than or equal to 4 units plus the weight of the minimum weight item. All items mee ng that
requirement will be shipped in one container.
Code:-
#include <iostream>
#include <vector>
#include <algorithm>
toys(vector<int> w) {
sort(w.begin(), w.end());
int containers = 0;
i++;
return containers;
int main() {
vector<int> weights(n);
return 0;
Input:
1 2 3 4 5 10 11 12 13
Output:
Problem 10. In this problem, we'll use the term "longest common substring" loosely. It refers to
substrings differing at some number or fewer characters when compared index by index. For
example, 'abc' and 'adc' differ in one posi on, 'aab' and 'aba' differ in two. Given two strings and
an integer k , determine the length of the longest common substrings of the two strings that differ
in no more than k posi ons.
Code:-
#include <iostream>
length++;
return maxLength;
int main() {
cin >> k >> s1 >> s2; cout << substringDiff(k, s1, s2) << endl;
return 0;
Input:
1 abcd bbca
Output:
Time Complexity:
• O(n1 * n2 * min(n1, n2)): The nested loops result in checking every substring of both strings,
where n1 and n2 are the lengths of s1 and s2.