DSA Assignment2
DSA Assignment2
• The subsequent lines will contain the strings. There are no spaces inside
the strings, therefore you can use normal scanf() to read the input.
1
Example 1
Input
2
[{abcde}]
{(abcba)}
Output
Balanced
Balanced
Example 2
Input
3
[{abab}]
{(absdfsdcba)]}
abcdcba
Output
Balanced
-1
Palindromic
1.3 Explanation
The first example has two strings as input. The first string has brackets in an
nested order and is not a palindrome, hence the corresponding output is ”Bal-
anced”. Similarly for the second input the string has brackets in a correct
order as well as it is not a palindrome, hence the output is ”Balanced”
For the second example, the first input is balanced, the second input is
not balanced nor Palindromic hence the corresponding output is ”-1”. The
third input doesn’t have any brackets and is palindromic, hence the output is
”Palindromic”.
1.4 Constraints
0 ≤ T ≤ 100
0 ≤ L ≤ 10000 where L is length of input string.
2
2 Circular Deque (100 Points)
In this question, you have to implement an ADT called Circular Deque using
queue data structure . A deque is known as double-ended queue. You can check
more about Deque here. Now You have to implement a Cicrular Deque which
performs the required Operations.
• void Print(Queue head): Prints the Circular Deque from the first ele-
ment. If deque is empty, Print -1.
• void PrintReverse(Queue head): Prints the Circular Deque in reverse
manner. If deque is empty, Print -1.
• int findElem(Queue head,int pos): Find the element in the given po-
sition and return it. If there is no element present in given position, return
-1. Consider the Circular Deque as 1-indexed.
• void removeKElems(Queue head,int k): Remove the front k elements
in the Circular Deque. If k is greater than the size of deque, remove all
the elements.
3
2.2 Input and Output
The first line contains T , the number of operations that need to be performed.
1 ≤ T ≤ 10000.
The next line consists of a string indicating the OPERATION to be executed.
The next few lines give the data needed for the operation i.e.
4
2.3 Example Test Cases
Input Output
15
OPER1
4
OPER3
5
OPER1
2
OPER1
6
OPER2
5
OPER4
6
OPER1
1
OPER5
421
OPER3
7
OPER6
1247
OPER3
3
OPER7
4 2
OPER8
3
OPER5
21
OPER7
3 -1
2.4 Explanation
We have:
1. Push 4 : Inserts 4 at end. The current state of the circular Deque will be:
2. Inject 5 : Inserts 5 at front. The current state of the circular lDeque will
be:
5−4
5
3. Push 2 : Inserts 2 at end. The current state of the circular Deque will
be:
5−4−2
4. Push 6 : Inserts 6 at end. The current state of the circular Deque will
be:
5−4−2−6
5. Pop : Remove the front element and return it. The current state of the
circular Deque will be:
4−2−6
6. popRear : Remove the rear element and return it. The current state of
the circular Deque will be:
4−2
7. Push 1 : Inserts 1 at end. The current state of the circular Deque will
be:
4−2−1
9. Inject 7 : Inserts 7 at front. The current state of the circular Deque will
be:
7−4−2−1
6
3 Trees - The Story of Binary Trees(100 points)
The objective of this question is to practice traversals in trees.
There are 2 easy, basic subproblems to be solved, which will get you
familiar with the basic strategies to tackle most tree questions moving
forward
Input
• The First Line consists of a number of test cases - an integer variable
-T
• The Second Line consists of N - the number of Nodes in the tree
• The Third Line consists of N space separated integer numbers, The
Elements of the tree, n i.
Output
• The First Line of the output contains N space-separated integer num-
bers which represent the ”Beautiful”-traversal(Zig-zag Level order
Traversal) of the tree
7
Constraints
• 1 ≤ T ≤ 10
• 1 ≤ N ≤ 105
• 1 ≤ ni ≤ 109
3. Example TestCase
Input output
1 20 25 10 5 15 23 30
7
20 10 25 5 15 23 30
Input
• The First Line consists of a number of test cases - an integer variable
-T
8
• The Second Line consists of N - the number of Nodes in the tree
• The Third Line consists of N space separated integer numbers, The
Elements of the tree in the Inorder Sequence, n i
• The Fourth Line consists of N space-separated integer numbers, The
Elements of the tree in the Preorder Sequence. m i
Output
Constraints
• 1 ≤ T ≤ 101
• 1 ≤ N ≤ 103
• 1 ≤ ni ≤ 109
• 1 ≤ mi ≤ 109
3. Example TestCase
Input output
1 123456
6
425163
124536
9
4 King and the Crisis (100 Points)
Once upon a time, there was a kingdom ruled by a wise King who loved na-
ture and often went on forest walks. During one of his walks, he came across
a beautiful Tree with several branches and leaves. The king, being a nature
lover, decided to keep the tree as it was and even gave orders to his workers to
take care of it.
Years passed by, and the tree grew taller, stronger, and even bore fruits. How-
ever, the kingdom was facing a financial crisis, and the king decided to take
the tree’s fruits and sell them so as a measure to overcome the crisis. But the
workers, who had grown attached to the tree, requested the king to spare it.
The king thought for a while and came up with an idea. He decided to make
use of the tree’s branches to create a Binary Search Tree with every fruit
hanging from those branches (each Node of the BST) having different Values
V, hoping to use it as a source of income for the kingdom.
However, the king soon realized that the tree was not ordinary, and he didn’t
want to harm it in any way. He asked to workers to modify the tree such that
each fruit’s price (Node’s Value) gets updated to the sum of all the values
smaller or equal to the current fruit. The workers spent days trying to
figure out a way to fulfil their Kings order but couldn’t find a way out.
You are a good friend of one of the workers got a call to assist. Help the
workers to find the Maximum Sale possible out of this tree. Maximum sale is
the sum of all values in the updated Binary Search Tree.
Output
10
4.2 Example 1
Input:
7
20 10 25 5 15 23 30
Output:
50 15 98 5 30 73 128
399
2. For Node 10 : Updated Value will be some of all values smaller or equal
than 10.
5 + 10 => 15
3. For Node 25 : Updated Value will be some of all values smaller or equal
than 25.
5 + 10 + 15 + 20 + 23 + 25 => 98
4. For Node 5 : Updated Value will be some of all values smaller or equal
than 5.
5 => 5
11
5. For Node 15 : Updated Value will be some of all values smaller or equal
than 15.
5 + 10 + 15 => 30
6. For Node 23 : Updated Value will be some of all values smaller or equal
than 23.
5 + 10 + 15 + 20 + 23 => 73
7. For Node 30 : Updated Value will be some of all values smaller or equal
than 30.
5 + 10 + 15 + 20 + 23 + 25 + 30 => 128
4.3 Example 2
Input:
11
8 3 10 1 6 14 4 7 13 11 12
Output:
29 4 39 1 14 89 8 21 75 50 62
392
12
Figure 6: Updated Binary Search Tree
13