Lab Manual TT
Lab Manual TT
B.E. – CSE
Experiement-3 You have three stacks of cylinders where each cylinder has the same CO5
diameter, but they may vary in height. You can change the height of a stack by removing
Find the maximum possible height of the stacks such that all of the stacks are exactly the
same height. This means you must remove zero or more cylinders from the top of zero or
more of the three stacks until they are all the same height, then return the height.
Experiment- 4 Given a pointer to the head of a linked list, insert a new node before the CO4
head. The next value in the new node should point to head and the data value should be
replaced with a given value. Return a reference to the new head of the list. The head
pointer given may be null meaning that the initial list is empty.
Unit-2
Experiment-5 Given an undirected weighted connected graph, find the Really Special CO2
SubTree in it. The Really Special SubTree is defined as a subgraph consisting of all the
The subgraph is of minimum overall weight (sum of all edges) among all such subgraphs.
To create the Really Special SubTree, always pick the edge with smallest weight.
Determine if including it will create a cycle. If so, ignore the edge. If there are edges of
Choose the edge that minimizes the sum u+v+wt where u and v are vertices and wt is the
edge weight.
Print the overall weight of the tree formed using the rules.
u v wt
1 2 2
2 3 3
3 1 5
Experiment-6 Complete the preOrder function in the editor below, which CO5
has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's
infinitely many times. Given an integer,n , find and print the number of letter a's in the
Experiment-8 Given an array of integers and a target sum, determine the sum nearest CO1
to but not exceeding the target that can be created. To create the sum, use any element of
your array zero or more times.
Unit-3
Experiment-9 To implement traveling salesman problem using naive CO2
approach.
Experiment-1
Aim: An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an
array,A , of size N , each memory location has some unique index, (where ), that can be referenced as or Reverse an
array of integers.
Example
A=[1,2,3,4]
Return [3,2,1].
Function Description
Returns
Input Format
Constraints
1<= N<=10^3
Sample Input 1
Array: arr1432
4
1432
Sample Output 1
2341
Code:-
#include <iostream>
#include <vector>
vector<int> reverseArray(vector<int> A) {
int start = 0;
int end = A.size() - 1;
// Swap elements from the start and end until they meet in the middle
while (start < end) {
swap(A[start], A[end]);
start++;
end--;
}
return A;
}
int main() {
int N;
cin >> N;
vector<int> A(N);
return 0;
}
Steps to be done:
1. Open this link.( https://www.programiz.com/cpp-programming/online-compiler/)
2. Paste the above-given code and then click on run.
3. Input should be “Enter the size of array” (e.g. 3)
4. Show your conduct to the class teacher for evaluation.
Input:-
Output:-
Viva-Voce Questions
Q.1 What is an array?
Q.2 How are arrays different from other data structures?
Q.3Explain the concept of contiguous memory allocation in arrays
Q.4 How do you declare an array in C++?
Q.5 Can the size of an array be changed after declaration?
Experiment-2
Aim:- A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left.
Given an integer, d, rotate the array that many steps left and return the result.
Example
d=2
arr=[1,2,3,4,5]
After rotations, .
Function Description
Returns
Input Format
The first line contains two space-separated integers that denote n, the number of integers, and d, the number of left
rotations to perform.
Constraints
1<=n<=10^5
1<=d<=n
1<=a[i]<=10^6
Sample Input
54
12345
Sample Output
51234
Explanation
To perform d=4 left rotations, the array undergoes the following sequence of changes:
[1, 2, 3, 4, 5]->[2,3,4,5,1]->[3,4,5,1,2]->[4,5,1,2,3]->[5,1,2,3,4]
Code:-
#include <iostream>
#include <vector>
int n = arr.size();
vector<int> rotatedArray(n);
int newIndex = (i + n - d) % n;
rotatedArray[newIndex] = arr[i];
return rotatedArray;
}
int main() {
int n, d;
cout << "Enter the size of the array and the number of left rotations (n d): ";
vector<int> arr(n);
return 0;
Input:
Output:
Viva-Voce Questions
Q.1 What is an array?
Q.2 How are arrays different from other data structures?
Q.3Explain the concept of contiguous memory allocation in arrays
Q.4 How do you declare an array in C++?
Q.5 Can the size of an array be changed after declaration?
Experiment-3
Aim:- You have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You
can change the height of a stack by removing and discarding its topmost cylinder any number of times.
Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you
must remove zero or more cylinders from the top of zero or more of the three stacks until they are all the same height,
Example
h1=[1,2,1,1]
h2=[1,1,2]
h3=[1,1]
There are 4,3 and 2 cylinders in the three stacks, with their heights in the three arrays. Remove the top 2 cylinders
from h1 (heights = [1, 2]) and from h2 (heights = [1, 1]) so that the three stacks all are 2 units tall. Return 2 as the
answer.
Function Description
Returns
The first line contains three space-separated integers, n1, n2, and n3, the numbers of cylinders in stacks , , and . The
subsequent lines describe the respective heights of each cylinder in a stack from top to bottom:
The second line contains n1 space-separated integers, the cylinder heights in stack 1. The first element is the top
The third line contains n2 space-separated integers, the cylinder heights in stack 2. The first element is the top cylinder
of the stack.
The fourth line contains n3 space-separated integers, the cylinder heights in stack 3. The first element is the top cylinder
of the stack.
Constraints
0<n1,n2,n3<=10^5
Sample Input
STDIN Function
----- --------
534 h1[] size n1 = 5, h2[] size n2 = 3, h3[] size n3 = 4
3 2 1 1 1 h1 = [3, 2, 1, 1, 1]
432 h2 = [4, 3, 2]
1 1 4 1 h3 = [1, 1, 4, 1]
Sample Output
5
Explanation
1. 8-3=5
2. 9-4=5
3. 7-1-1=5
Code:-
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
// Find the common height by iterating until a common height is found or one of the stacks is empty
while (!h1.isEmpty() && !h2.isEmpty() && !h3.isEmpty() &&
!(h1.get(h1.size() - 1).equals(h2.get(h2.size() - 1)) && h2.get(h2.size() - 1).equals(h3.get(h3.size() - 1)))) {
if (h1.get(h1.size() - 1) >= h2.get(h2.size() - 1) && h1.get(h1.size() - 1) >= h3.get(h3.size() - 1)) {
h1.remove(h1.size() - 1);
} else if (h2.get(h2.size() - 1) >= h1.get(h1.size() - 1) && h2.get(h2.size() - 1) >= h3.get(h3.size() - 1)) {
h2.remove(h2.size() - 1);
} else {
h3.remove(h3.size() - 1);
}
}
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int n3 = scanner.nextInt();
List<Integer> h1 = new ArrayList<>(n1);
List<Integer> h2 = new ArrayList<>(n2);
List<Integer> h3 = new ArrayList<>(n3);
Input:
Output:
Viva-voce Questions:
Q.1 What is a stack?
Q.2 How can you implement a stack?
Q.3 What is the difference between a stack and a queue?
Q.4 Explain the operations that can be performed on a stack.
Q.5 Can a stack be implemented using a queue?
Experiment-4
Aim:- Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node
should point to head and the data value should be replaced with a given value. Return a reference to the new head of
the list. The head pointer given may be null meaning that the initial list is empty.
Function Description
data: the value to insert in the data field of the new node
Input Format
The first line contains an integer n, the number of elements to be inserted at the head of the list.
The next n lines contain an integer each, the elements to be inserted, one per function call.
Constraints
1<=n<=1000
1<=list[i]<=1000
Sample Input
5
383
484
392
975
321
Code:-
import java.util.Scanner;
SinglyLinkedListNode(int node_data) {
this.data = node_data;
this.next = null;
}
}
if (llist == null) {
// If the list is empty, the new node becomes the head
return newNode;
} else {
// Inserting the new node before the current head
newNode.next = llist;
return newNode;
}
}
Output:-
Viva-Voce Questions
Q.1 What is Linked List?
Q.2 What is the difference between an array and a linked list?
Q.3 What is a Node in a Linked List?
Q.4 What is a Singly Linked List?
Q.5 How do you insert a node at the beginning of a linked list?
Experiment-5
Aim: Given an undirected weighted connected graph, find the Really Special SubTree in it. The Really Special
SubTree is defined as a subgraph consisting of all the nodes in the graph and:
There is only one exclusive path from a node to every other node.
The subgraph is of minimum overall weight (sum of all edges) among all such subgraphs.
To create the Really Special SubTree, always pick the edge with smallest weight. Determine if including it will create a
cycle. If so, ignore the edge. If there are edges of equal weight available:
Choose the edge that minimizes the sum u+v+wt where u and v are vertices and wt is the edge weight.
Print the overall weight of the tree formed using the rules.
u v wt
1 2 2
2 3 3
3 1 5
First choose 1->2 at weight 2. Next choose 2->3 at weight 3. All nodes are connected without cycles for a total weight
of 3+2=5 .
Function Description
Complete the kruskals function in the editor below. It should return an integer that represents the total weight of the
subtree formed.
Input Format
The first line has two space-separated integers g_nodes and g_edges, the number of nodes and edges in the graph.
The next lines each consist of three space-separated integers g_from ,g_to and g_weight, where g_ from
and g_to denote the two nodes between which the undirected edge exists and denotes the weight of that edge.
Constraints
2<=g_nodes<=3000
1<=g_edges<=N*(N-1)/2
1<=g_from,g_to<=N
0<=g_weight<=10^5
**Note: ** If there are edges between the same pair of nodes with different weights, they are to be considered as is,
Output Format
Print a single integer denoting the total weight of the Really Special SubTree.
Sample Input 1
Undirected Weighed Graph:
46
125
133
416
247
324
345
Sample Output 1
12
Explanation 1
Applying Kruskal's algorithm, all of the edges are sorted in ascending order of weight.
Code:-
#include <iostream>
#include <vector>
#include <algorithm>
struct Edge {
};
// Structure to represent a disjoint set
struct DisjointSet {
DisjointSet(int n) {
parent.resize(n + 1);
rank.resize(n + 1);
parent[i] = i;
rank[i] = 0;
int find(int u) {
if (parent[u] != u) {
parent[u] = find(parent[u]);
return parent[u];
}
void unionSets(int u, int v) {
parent[rootU] = rootV;
parent[rootV] = rootU;
} else {
parent[rootU] = rootV;
rank[rootV]++;
};
// Function to find the minimum spanning tree weight using Kruskal's algorithm
vector<Edge> edges;
for (int i = 0; i < g_edges; i++) {
});
DisjointSet ds(g_nodes);
int totalWeight = 0;
// Iterate through sorted edges and include them in the MST if they don't form a cycle
if (rootFrom != rootTo) {
ds.unionSets(rootFrom, rootTo);
totalWeight += edge.weight;
return totalWeight;
int main() {
return 0;
Input:-
Output:-
Viva-Voce Questions
Q.1 What is the Purpose of a Minimum Spanning Tree?
Q.2 Define Minimum Spanning Tree (MST)
Q.3 What is a Spanning Tree?
Q.4 Explain Kruskal's Algorithm for Finding the Minimum Spanning Tree.
Q.5 What is a Binary Search Tree (BST)?
Experiment-6
Aim:- Complete the preOrder function in the editor below, which has 1 parameter: a pointer to the root of a binary
tree. It must print the values in the tree's preorder traversal as a single line of space-separated values.
Input Format
Our test code passes the root node of a binary tree to the preOrder function.
Constraints
Output Format
Sample Input
1
\
2
\
5
/ \
3 6
\
4
Sample Output
125346
Explanation
Code:-
#include <iostream>
#include <queue>
struct Node {
int data;
Node* left;
Node* right;
};
// Function to perform preorder traversal of a binary tree
if (root == nullptr) {
return;
preOrder(root->left);
preOrder(root->right);
int main() {
preOrder(root);
return 0;
Output:-
Viva-Voce Questions
Q.1 What is a Tree in Data Structures??
Q.2 How do you perform an Inorder Traversal of a Binary Tree?
Q.3 What is a Binary Tree?
Q.4 What is the Height of a Tree?
Q.5 What is the Difference Between a Binary Tree and a Binary Search Tree (BST)?
Experiment-7
Aim:- There is a string, s, of lowercase English letters that is repeated infinitely many times. Given an integer,n , find
and print the number of letter a's in the first n letters of the infinite string.
Example
s=’abccac’
n=10
The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of a in the
substring.
Function Description
s: a string to repeat
Returns
Input Format
Sample Input 1
aba
10
Code:-
#include <iostream>
// Calculate the number of times the string s repeats completely in the first n characters
long complete_repeats = n / s.length();
return total_count_a;
}
int main() {
// Input values
string s = "aba";
long n = 10;
// Call the repeatedString function
long result = repeatedString(s, n);
return 0;
}
Output:-
Viva-Voce Questions
Q.1 What is a string?
Q.2 How are strings represented in memory?
Q.3 How do you find the length of a string?
Q.4 How do you reverse a string?
Q.5 How do you compare two strings for equality?
Experiment-8
Aim:- Given an array of integers and a target sum, determine the sum nearest to but not exceeding the target that can be
created. To create the sum, use any element of your array zero or more times.
For example, if arr=[2,3,4] and your target sum is 10 , you might select [2,2,2,2,2],[2,2,3,3] or [3,3,3,1] . In this case,
Function Description
Complete the unboundedKnapsack function in the editor below. It must return an integer that represents the sum
k: an integer
Input Format
- The first line contains two integers n and k, the length of arr and the target sum.
Output Format
Print the maximum sum for each test case which is as near as possible, but not exceeding, to the target sum on a
separate line.
Sample Input
2
3 12
169
59
34448
Code:
#include <iostream>
#include <vector>
#include <algorithm>
return dp[k];
}
int main() {
// Input values
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
return 0;
}
Output:
Viva-Voce Questions
Q.1 What are the different variations of the Knapsack Problem?
Q.2 What is dynamic programming approach?
Q.3 Explain the Unbounded Knapsack Problem.
Q.4 What is the Subset Sum Problem?
Q.5 How can the Subset Sum Problem be related to the Knapsack Problem?
Experiment-9
Aim:- To implement traveling salesman problem using naive approach.
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
return cost;
}
return minCost;
}
int main() {
// Define the graph as an adjacency matrix
vector<vector<int>> graph = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
return 0;
}
Output:
Viva-Voce Questions
Q.1 What is Traveling Salesman Problem?
Q.2 Explain the dynamic programming approach to solving TSP.
Q.3 What are the steps involved in the nearest neighbor approach?
Q.4 What is the connection between TSP and the MST?
Q.5 Discuss the branch and bound technique for solving TSP.
Experiment-10
Aim:-Given a set of non-negative integers and a value sum, the task is to check if there is a subset
of the given set whose sum is equal to the given sum.
Examples:
Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9
Output: True
Explanation: There is a subset (4, 5) with sum 9.
Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 30
Output: False
Explanation: There is no subset that add up to 3
Code:
#include <iostream>
#include <vector>
return dp[n][sum];
}
int main() {
vector<int> set = {3, 34, 4, 12, 5, 2};
int sum1 = 9;
int sum2 = 30;
return 0;
}
Output:
Viva-Voce Questions
Q.1 What is a subset?
Q.2 How would you define the concept of a power set?
Q.3 Can an empty set be considered a subset?
Q.4 What approaches can be used to solve the subset sum problem?
Q.5 Explain the concept of the intersection of two sets.