Akshat Daa Assignment
Akshat Daa Assignment
Aim: Given the pointer to the head node of a linked list, change the next pointers of the
nodes so that their order is reversed. The head pointer given may be null meaning that the
initial list is empty.
Objective: Given the head of a singly linked list, reverse the pointers between nodes so that
the list is reversed.
Algorithm:
1. Initialize three pointers: prev (initially NULL), current (points to the head), and next
(initially NULL).
2. Traverse the list. For each node:
Implementation/Code:
#include <iostream>
using namespace std;
class Node {
public:
int value;
Node* link;
Node(int val) {
value = val;
link = nullptr;
}
};
// Main function
int main() {
Node* start = new Node(1);
start->link = new Node(2);
start->link->link = new Node(3);
start->link->link->link = new Node(4);
start = reverseList(start);
return 0;
}
Output:
Objective: Given the root of a binary tree, print the node values in postorder traversal (left,
right, root).
Algorithm:
Implementation/Code:
#include <iostream>
using namespace std;
class TreeNode {
public:
int value;
TreeNode* leftChild;
TreeNode* rightChild;
// Constructor
TreeNode(int data) {
value = data;
leftChild = rightChild = nullptr;
}
};
return 0;
}
Output:
Objective: Given the root of a binary search tree and a value, insert the value into the
appropriate position in the BST.
Algorithm:
1. If the root is NULL, create a new node with the given value and return it.
2. Compare the value to be inserted with the current node's value:
Implementation/Code:
#include <iostream>
using namespace std;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
class TreeNode {
public:
int value;
TreeNode* leftChild;
TreeNode* rightChild;
return 0;
}
Output:
Objective: Given the root of a Huffman tree and a binary-encoded string, decode the string
and return the original message.
Algorithm:
Implementation/Code:
#include <iostream>
#include <string>
using namespace std;
class TreeNode {
public:
char character;
int frequency;
TreeNode* leftChild;
TreeNode* rightChild;
return 0;
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Aim: To partition an array around a pivot element as part of the Quicksort algorithm.
Objective: Given an unsorted array and a pivot element (the first element), partition the
array into three parts:
left (elements smaller than the pivot)
equal (elements equal to the pivot)
right (elements greater than the pivot).
Algorithm:
Implementation/Code:
#include <iostream>
#include <vector>
using namespace std;
return finalArray;
}
vector<int> inputArray(arraySize);
cout << "Enter " << arraySize << " space-separated integers: ";
for (int i = 0; i < arraySize; i++) {
cin >> inputArray[i];
}
return 0;
}
Output:
Aim: Given the pointer to the head node of a linked list, change the next pointers of the
nodes so that their order is reversed. The head pointer given may be null meaning that the
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
initial list is empty.
Objective: Determine the number of ways to make a given amount using an unlimited
supply of different denominations of coins.
Algorithm:
1. Initialize a dp array of size amount + 1 with dp[0] = 1 (there is one way to make zero
amount).
2. Iterate over each coin in the given list of coins:
For each coin, update the dp array for amounts from the coin value up to the target
amount.
Add the number of ways to form the remaining amount (dp[i - coin]) to the current
amount dp[i].
3. Return dp[amount], which contains the total number of ways to form the given amount.
Implementation/Code:#include <iostream>
#include <vector>
using namespace std;
// Function to calculate the number of ways to make change for a given amount
long countChangeWays(int amount, const vector<int>& coinTypes) {
vector<long> ways(amount + 1, 0);
ways[0] = 1; // There's exactly one way to make change for 0 (using no coins)
int main() {
int targetAmount, numOfCoins;
// Input the target amount and the number of different coin types
cout << "Enter the target amount and the number of coin denominations: ";
cin >> targetAmount >> numOfCoins;
vector<int> denominations(numOfCoins);
// Output the number of ways to make change for the given amount
cout << "Number of ways to make change: " << countChangeWays(targetAmount,
denominations) << endl;
return 0;
}
Output:
Aim: To compute the sum of all possible substrings of a given integer, represented as a
string.
Objective: Given a number as a string, find the sum of all substrings, modulo 109+710^9 +
7109+7.
Algorithm:
return totalSum;
}
int main() {
string number;
return 0;
}
Output:
Aim: To calculate the minimum number of miles Marc needs to walk after eating cupcakes,
minimizing his calorie intake.
Objective: Given a list of cupcake calories, minimize the walking distance by eating higher-
calorie cupcakes first.
Algorithm:
Implementation/Code:
#include <iostream>
#include <vector>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
#include <algorithm>
using namespace std;
return totalMiles;
}
int main() {
int cupcakeCount;
vector<int> calorieValues(cupcakeCount);
return 0;
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Time Complexity: O(n+m)
Space Complexity: O(1)
Aim: To determine the minimum number of containers required to ship items based on their
weights.
Objective: Group items such that each container can hold items whose weights differ by no
more than 4 units.
return containerCount;
}
int main() {
int itemCount;
vector<int> weights(itemCount);
return 0;
}
Output:
Aim: To find the longest common substring of two strings that differ by no more than k
characters.
Objective: Given two strings, find the longest common substring that has at most k differing
characters.
Algorithm:
1. Use a sliding window approach to compare substrings.
2. For each possible substring pair, count the number of differing characters.
3. Maintain a record of the longest substring that satisfies the k-difference constraint.
Implementation/Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
return maxLen;
}
int main() {
int t, k;
string s1, s2;
cout << "Enter the number of test cases: ";
cin >> t;
while (t--) {
cout << "Enter k and two strings: ";
cin >> k >> s1 >> s2;
cout << "Longest common substring length: " << substringDiff(k, s1, s2) << endl;
}
return 0;
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Time Complexity: O(n)
Space Complexity: O(1)
Learning Outcomes:
i. Reverse a Linked List: Understand how to manipulate pointers in a linked list to reverse its
order effectively.
ii. Postorder Traversal of a Binary Tree: Learn to perform recursive traversals of binary trees
using postorder traversal technique.
iii. Insert a Node into a Binary Search Tree (BST): Gain insights into the properties of binary
search trees and how to maintain their structure during node insertion.
iv. Huffman Decoding: Understand how to decode a binary string using a Huffman tree by
traversing left for '0' and right for '1'.
vi. Count Change Ways: Master dynamic programming to calculate the number of ways to make
change for a specific amount using various coin denominations.
vii. Sum of Substrings: Explore techniques to compute the total sum of all possible substrings of a
number represented as a string efficiently.
viii. Marc's Cupcake Walk: Understand the strategy of sorting items to minimize costs in resource
allocation problems.
ix. Shipping Containers: Learn to group items based on weight constraints to optimize the use of
shipping containers.
x. Longest Common Substring with k Differences: Develop skills in string manipulation and
comparison to find the longest common substring under character difference constraints.