Datastructure Questiopn-KSR-PPL-END Semester Exam
Datastructure Questiopn-KSR-PPL-END Semester Exam
QUESTION :-B
All questions satisfies All Cos-1-2-3-4
P ROBLEM 1: Remove Friends
After getting her PhD, Christie has become a celebrity at her university, and her facebook
profile is full of friend requests. Being the nice girl she is, Christie has accepted all the
requests. Now Kuldeep is jealous of all the attention she is getting from other guys, so he
asks her to delete some of the guys from her friend list. To avoid a 'scene', Christie decides
to remove some friends from her friend list, since she knows the popularity of each of the
friend she has, she uses the following algorithm to delete a friend.
Algorithm Delete(Friend):
DeleteFriend=false
for i = 1 to Friend.length-1
if (Friend[i].popularity <
Friend[i+1].popularity) delete i th friend
DeleteFriend
=true break
if(DeleteFriend ==
false) delete the
last friend
Input:
First line contains T number of test cases. First line of each test case contains N, the
number of friends Christie currently has and K ,the number of friends Christie decides to
delete. Next lines contains popularity of her friends separated by space.
Output:
For each test case print N-K numbers which represent popularity of Christie friend's
after deleting K friends.
Constraints
1<=T<=1000
1<=N<=100000
0<=K< N
0<=popularity_of_friend<=100
NOTE:
Order of friends after deleting exactly K friends should be maintained as given in input.
SAMPLE INPUT
3
31
3 100 1
52
19 12 3 4 17
53
23 45 11 77 18
SAMPLE OUTPUT
100 1
19 12 17
77 18
Davy wants to do the matrix multiplication using Linked List, so help her to write the
program
Input
1 ≤ T ≤ 10
1 ≤ m ≤ 100
1 ≤ n ≤ 100
1 ≤ k ≤ 100
a11 1, a11
2...a1m n a21
1, a21 2...a2n
k a31 1, a31
2...a3m k
Example
Input:
2
3
3
1-> 2-> 3->\n
4-> 5-> 6->\n
7-> 8-> 9->\n
3
3
10-> 11-> 12->\n
13-> 14-> 15->\n
16-> 17-> 18->\n
2
3
1-> 2-> 3->\n
4-> 5-> 6->\n
4
3
7-> 8-> 9->\n
10-> 11-> 12->\n
13-> 14-> 15->\n
16-> 17-> 18->\n
Output:
3
3
84-> 90-> 96->\n
201-> 216-> 231->\n
318-> 342-> 366->\n
The Matrix Multiplication is not possible
S TACK
Input :
The first line contains a single integer N denoting the size of array A. Each of the next N
lines contains a single integer, where the integer on the line denotes .
Output :
Print N space separated integers on a single line, where the integer denotes or 1, if does
not exist.
SAMPLE INPUT
8
3
7
1
7
8
4
5
2
SAMPLE OUTPUT
1 4 4 4 -1 2 -1 -1
PROBLEM 4: Little Shino and Paris
Given a permutation of number from 1 to N. Among all the subarrays, find the number of
unique pairs such that and a is maximum and b is second maximum in that subarray.
Input:
First line contains an integer, N . Second line contains N space separated distinct integers, ,
denoting the permutation.
Output:
Print the required answer.
SAMPLE INPUT
5
12345
SAMPLE OUTPUT
4
Q UEUE
P ROBLEM 5: MONK QUEUE
The Monk is trying to explain to its users that even a single unit of time can be extremely
important and to demonstrate this particular fact he gives them a challenging task. There
are N processes to be completed by you, the chosen one, since you're Monk's favorite
student. All the processes have a unique number assigned to them from 1 to N. Now, you
are given two things:
The calling order in which all the processes are called.
The ideal order in which all the processes should have been executed.
Now, let us demonstrate this by an example. Let's say that there are 3 processes, the calling
order of the processes is: 3 - 2 - 1. The ideal order is: 1 - 3 - 2, i.e., process number 3 will
only be executed after process number 1 has been completed; process number 2 will only
be executed after process number 3 has been executed.
Iteration #1: Since the ideal order has process #1 to be executed firstly, the calling ordered
is changed, i.e., the first element has to be pushed to the last place. Changing the position
of the element takes 1 unit of time. The new calling order is: 2 - 1 - 3. Time taken in step
#1: 1.
Iteration #2: Since the ideal order has process #1 to be executed firstly, the calling ordered
has to be changed again, i.e., the first element has to be pushed to the last place. The new
calling order is: 1 - 3 - 2. Time taken in step #2: 1.
Iteration #3: Since the first element of the calling order is same as the ideal order, that
process will be executed. And it will be thus popped out. Time taken in step #3: 1.
Iteration #4: Since the new first element of the calling order is same as the ideal order, that
process will be executed. Time taken in step #4: 1.
Iteration #5: Since the last element of the calling order is same as the ideal order, that
process will be executed. Time taken in step #5: 1.
Total time taken: 5 units.
PS: Executing a process takes 1 unit of time. Changing the position takes 1 unit of time.
Input format:
The first line a number N, denoting the number of processes. The second line contains the
calling order of the processes. The third line contains the ideal order of the processes.
Output format:
Print the total time taken for the entire queue of processes to be executed.
Constraints:
1<=N<=100
SAMPLE INPUT
3
321
132
SAMPLE OUTPUT
You own a club on eerie planet. The day on this planet comprises of H hours. You
appointed C crew members to handle the huge crowd that you get, being the best club on
the planet. Each member of the crew has fixed number of duty hours to work. There can be
multiple or no crew members at work at any given hour of the day.
Being on weird planet, the rules of this club cannot be normal. Each member of the crew
only allows people who are taller than him to enter the club when he is at
work. Given the schedule of work and heights of the crew members, you have to answer
Q queries. Each query specifies the time of entry and height of a person who is visiting the
club. You have to answer if the person will be allowed to enter the club or not.
Input:
First line of the input contains 3 integers, H,C,Q. Representing number of hours in
a day, number of crew members and number of queries respectively.
Next C lines follow, where each line contains 3 integers, hi,Si,Ei, representing
height of the crew member and start and end hour of his/her work schedule. He/she
works for hours [Si,Ei], both inclusive.
Next Q lines follow, each containing 2 integers, hi,ti, representing height and time
(in hour) of the person trying to enter the club.
Output:
Q lines, each line containing "YES" or "NO", without the quotes, answering if the person
will be allowed to enter the club or not.
Constraints:
1≤H≤109
1≤C≤105
1≤Q≤105
1≤Si
≤Ei≤
H
1≤ti≤
H
1≤hi
≤107
SAMPLE INPUT
10 1 5
50 2 6
10 1
10 2
50 5
51 6
100 10
SAMPLE OUTPUT
YES
NO
NO
YES
YES
B INARY TREE
You are given a binary tree rooted at 1. You have to find the mirror image of any node qi
about node 1. If it doesn't exist then print -1.
Input:
SAMPLE INPUT
10 8
12R
13L
24R
25L
36R
37L
58R
59L
7 10 R
2
5
3
6
1
10
9
4
SAMPLE OUTPUT
3
6
2
5
1
-1
-1
7
P ROBLEM 8: BINARY TREE PATH SUM
A BinaryTree is an abstract data type that can contain either 0,1 or 2 children. BinaryTree
constructs the tree by inserting the nodes in the left right order. A basic BinaryTree has the
following operations: P – Prints the tree H x– prints the height of the node x D x– prints
the depth of the node x PRE – prints the pre-order traversal of the tree POS- prints the
post-order traversal of the tree IN – prints the in-order traversal of the tree EXT -
prints all the external
nodes. PS x- prints a path whose element sum is greater than x (Hint: If there is only one
node in the tree, then check the root node element if the value of root node is greater than
x, then print the root node, else print "No Path". If there is multiple paths which is greater
than x, then print the first leftmost path in the tree.(i.e, you have to begin the traversal from
root and move towards left child, if it doesn't satisfy the condition take the next existing
path from root) Prints the path in reverse order, i.e, from leaf node to root node)
Input:
The first line contains the tree size. Second line contains the tree nodes seperated
by spaces, if a node doesn't have left child or right child specify it as 0. Third line
reads the number of operations.
Each line of the subsequent lines contains a single query in the form described in
the problem statement above.
Output:
Should show the resulting Tree after the above mentioned operations are performed on
the queue.
Sample Input:
15
1 2 3 0 5 0 7 0 0 10 11 0 0 14 15
10
H1
D1
D 15
H
1
5
P
R
E
P
O
S
I
N
P
S
1
8
E
X
T
Sample Output:
Height 3
Depth 0
Depth 3
Heig
ht 0
Elt-
1;
Pos1
Elt-
2;
Pos2
Elt-
3;
Pos3
Elt-
5;
Pos5
Elt-
7;
Pos7
Elt-10; Pos10
Elt-11; Pos11 Elt-14; Pos14 Elt-15; Pos15
Preorder Traversal
1->2->5->10->11->3->7->14->15->
Postorder Traversal
10->11->5->2->14->15->7->3->1->
Inorder Traversal
2->10->5->11->1->3->14->7->15->
Path is
10->5->2->1
External
nodes are
10->11->14-
>15->
Input format:
First line consists of a single integer T denoting the number of test cases.
First line of each test case consists of two space separated integers denoting N and X.
Second line of each test case consists of N space separated integers denoting the array
elements.
Output format:
Print the required answer for each test case on a new line.
Constraints:
1≤T≤50
1≤X,N≤13000
1≤A[i]≤109
SAMPLE INPUT
4
41
1425
42
4215
43
5241
44
1245
SAMPLE OUTPUT
Average
Average
Average
Good
Monk is standing at the door of his classroom. There are currently N students in the class,
i'th student got Ai candies.
There are still M more students to come. At every instant, a student enters the class and
wishes to be seated with a student who has exactly the same number of candies. For each
student, Monk shouts YES if such a student is found, NO otherwise.
Input:
For each test case, output M new line, Monk's answer to the M students.
Print "YES" (without the quotes) or "NO" (without the quotes) pertaining to the Monk's
answer.
Constraints:
1 ≤ T ≤ 10
1 ≤ N, M ≤ 105
0 ≤ Ai ≤ 1012
SAMPLE INPUT
1
23
3 2 9 11 2
SAMPLE OUTPUT
NO
NO
YES
SEARCHING
A team has to perform binary search on a three-dimensional sorted integer array. Instead
on working directly at the content level, the team decided to start with the dimensions. Let
A be an X×Y×Z array of integers and the value to be searched is num. The process starts
by applying binary search only on the first dimension. Once the index, say midx, for the
first dimension is identified, that implies the value num may exist somewhere in the set of
elements represented by A[midx][0..Y−1][0..Z−1]. Now the process of binary search is
repeated again for the second
dimension to identify midy. After this, the range to be searched for gets reduced to a set of
elements represented by A[midx][midy][0..Z−1]. Lastly, the simple binary search is
applied on the third dimension to search for num.
In case of a successful search, one has to output both the ranges obtained after fixing first
and second dimension. Also the three indices corresponding to the searched element has to
be given in the output. In case of an unsuccessful search, there can be three possibilities.
num is out of all the ranges available for the first dimension. Output should be zero
only.
num is lying in one of the ranges available for the first dimension, but is out of all
the ranges available for the second dimension. Output should have the range
obtained after fixing first dimension and then zero for unsuccessful search.
num is lying in one of the ranges available for first as well as second dimension.
Output should have both the ranges obtained after fixing first and second
dimension followed by a zero for unsuccessful search.
Note: Binary search algorithm computes mid=(beg+end)/2 within the while loop only.
Input:
Line 1 contains four integers X, Y, Z, and N. X is the first, Y is the second, and Z is the
third dimension of an array. N is the value to be searched.
Line 2 contains X×Y×Z integers separated by space. These are the contents of a three-
dimensional array in row-major order.
Output:
Successful search:
Line 1 has two integers giving range obtained for the first dimension, i.e. minimum
then maximum.
Line 2 has two integers giving range obtained for the second dimension, i.e. minimum
then maximum.
Line 3 has three integers giving indices corresponding to the searched element N.
Line 4 is an integer giving total number of times while loop of a binary search gets
executed for all the three dimensions.
Unsuccessful search:
May have a single line as specified in possibility 1 of an unsuccessful search. May have two
lines as specified in possibility 2 of an unsuccessful search.
May have three lines as specified in possibility 3 of an unsuccessful search.
In addition, the last line has an integer giving total number of times while loop of a binary
search gets executed for all the three dimensions.
Sample Input I:
3 2 2 28
2 11 12 19 21 22 28 37 39 69 75 92
Sample Output I:
21 37
28 37
110
4
Sample Input II:
3 2 2 80
2 11 12 19 21 22 28 37 39 69 75 92
Sample Output II:
39 92
75 92
0
6
Sample Input III:
3 2 2 72
2 11 12 19 21 22 28 37 39 69 75 92
Sample Output III:
39 92
0
4
Input
Input will begin with an integer N, number of strings in data base. Each of the following N
lines contain a single string from database. Strings are given in the order the algorithm
compare them to a query string. All strings in database will be distinct. The following line
contains an integer Q, the number of queries. Each of the following Q lines contain a
single query string. All strings in the input will be strings of less than 30 lowercase letters
of the English alphabet.
Output
Print one integer per line for each query string, the number of steps algorithm uses
while searching for the string in the same order as given in the input.
Constraints
1 <= N <= 30,000
1 <= Q <= 30,000
Example Input:
5
avsvinaystp bvksgptk vkrjgr robot habbit
3
habbit vkrjgr bvkija Output:
11
9
8
S ORTING
Divide the given array of size equal to the sum of first n natural numbers into subarrays of
size 1,2,3, ,n. For the xth subarray of size x – if x is odd then sort it in descending
order, otherwise
sort it in ascending order. Output the sum of 1st element of each subarray.
Input
1≤t≤4
1 ≤ n ≤ 75
0 ≤ A[i] ≤ 1000000
Example
Input:
1
3
123456
Output:
9
PROBLEM 14: DILEMMA SORT
Sometimes, arrays may be too large for us to wait around for insertion sort to finish. Is
there some other way we can calculate the number of times Insertion Sort shifts each
elements when sorting an array?
If ki is the number of elements over which the ith element of the array has to shift, then the
total number of shifts will be k1+k2+………kn.
Input Format
The first line contains a single integer, T, denoting the number of queries to perform. The
2T subsequent lines describe each query over two lines:
1. The first line contains an integer N , denoting the number of elements to be sorted.
2. The second line contains N space-separated integers describing the respective values of
a[1],a[2],…..a[n].
Constraints
• 1<=T<=15
• 1<=N<=105
• 1<=a[i]<=107
Output Format
Print T lines containing the required answer for each query.
Sample Input
2
5
11122
5
31312
Sample Output
0
4
Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked
list. Either head pointer may be null meaning that the corresponding list is empty.
Example
HeadA refers to 1→3→7→NULL
HeadB refers to 1→2→ NULL
The new list is 1→1→2→3→7→NULL
Function Description
Complete the mergeLists function in the editor below.
mergeLists has the following parameters:
SinglyLinkedListNode pointer headA: a reference to the head of a list
SinglyLinkedListNode pointer headB: a reference to the head of a list
Returns
SinglyLinkedListNode pointer: a reference to the head of the merged list
Input Format
The first line contains an integer t, the number of test cases.
The format for each test case is as follows:
The first line contains an integer n, the length of the first linked list.
The next n lines contain an integer each, the elements of the linked list.
The next line contains an integer m, the length of the second linked list.
The next m lines contain an integer each, the elements of the second linked list.
Constraints
1≤t≤10
1≤n,m≤1000
1≤list[i], where list[i] is the i th element of the list.
Sample Input
1
3
1
2
3
2
3
4
Sample Output
12334