Assign 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Birla Institute of Technology and Science, Pilani Hyd Campus

BITS F232: Foundations of Data Structures and Algorithms


1st Semester 2022-23, Assignment-3, Max Marks: 8
Date of Submission: 18.11.2022

Q1. (Euler Tour) You are given a tree containing n nodes and the value associated with each node. You
decided to go on a Euler tour on that tree and collect all the points from a node every time you visit
it. Print the value of total points collected during the tour.

INPUT FORMAT (input arrives from the terminal/ stdin):


The first line contains n which is the number of nodes the tree contains. The next line contains n values
which represents the value associated with ith node. Then n-1 lines follows which contain 2 values say
x and y stating that x is the parent of y.

OUTPUT FORMAT (print output to the terminal / stdout):


Print the total points collected during the traversal.

SAMPLE INPUT 1:
5
2 4 8 6 10
12
13
24
35

SAMPLE OUTPUT 1:
46

Q2. (Priority Queue) You are given n lines of different lengths. You are required to convert it into a
single line in minimum time. The time it takes to join 2 lines is the sum of lengths of those 2 lines.

INPUT FORMAT (input arrives from the terminal / stdin):


Each input consists of several independent test cases, all of which need to be solved correctly to solve
the entire input case. The first line contains T (1 ≤ T ≤ 100), giving the number of test cases to be
solved. The T test cases follow, each described by a pair of lines. The first line of each pair contains
N(1 ≤ N ≤ 100000 ), and the second line contains a1, a2,a3, . . . , an . It is guaranteed that the sum of
N over all test cases is at most 105. Values of N might differ in each test case.

OUTPUT FORMAT (print output to the terminal / stdout):


Please write T lines of output, one for each test case.

SAMPLE INPUT 1:
2
5
14295
7
5362491

SAMPLE OUTPUT 1:
43
78
Explanation- In 1st case, join 1st and 3rd line (which took 1+2=3 minutes), and make the array
[4,3,9,5]. Now, join 1st and 2nd line (which took 3+4=7 minutes), and makes the arrays as [7,9,5]. Now,
join 1st and 3rd line (which took 7+5=12 minutes), and makes the array as [9,12]. Now, join them
which will take 21 minutes.
So, the total time is 43 minutes.

Q3. (Hashing) You are given an array of n integers. You are required to find the number of indices
(i,j) such that i<j and aj +2*i=ai+2*j. Mind that indexing is 1 based.

INPUT FORMAT (input arrives from the terminal / stdin):


Each input consists of several independent test cases, all of which need to be solved correctly to solve
the entire input case. The first line contains T (1 ≤ T ≤ 100 ) , giving the number of test cases to be
solved. The T test cases follow, each described by a pair of lines. The first line of each pair contains
N(1 ≤ N ≤ 100000 ), and the second line contains a1, a2,a3, . . . , an . It is guaranteed that the sum of
N over all test cases is at most 105. Values of N might differ in each test case.

OUTPUT FORMAT (print output to the terminal / stdout):


Please write T lines of output, one for each test case.

SAMPLE INPUT 1:
2
6
351466
3
123

SAMPLE OUTPUT 1:
2
0

Explanation-
For first test case:
Pairs are (1,2) and (4,5).

Q4. (Hashing) A group of friends decided to play a game. Winning a round of it, rewards some positive
points and loosing, some negative points. The winner is the one with the maximum number of points.
In case, there are more than 1 player who has the maximum number of points, winner is the one who
gained it first.

INPUT FORMAT (input arrives from the terminal / stdin):


The first line of contains n (1 ≤ n ≤ 100000), then follow n lines, containing the information about the
rounds in "name score" format in chronological order, where name is a string of lower-case Latin
letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.

OUTPUT FORMAT (print output to the terminal / stdout):


Print the name of the winner.
SAMPLE INPUT 1:
3
Khushil 3
Rahil 5
Khushil 2
SAMPLE OUTPUT 1:
Rahil
SAMPLE INPUT 2:
3
Vedant 3
Vedant 5
Mohit 2
SAMPLE OUTPUT 2:
Vedant

SAMPLE INPUT 3:
4
Jeet 12
Jeet -5
Suyash 10
Jeet 3
SAMPLE OUTPUT 3:
Jeet

Submission Instructions:

You should retain the same grouping, if any. This grouping is allowed only for allowing peer
learning. However, you need to solve yourself all the questions. There will be a demo or viva
for these assignments at a later date. You should submit your exe file, and source files putting
all of those into a tar file/ zip file, and upload it into google class before the submission
deadline i.e. midnight of 16/11/2022. Your code should also run on Ubuntu systems (like in
the lab) for evaluation purposes. You should also keep a copy of your code. Any doubts or
queries regarding the questions can be emailed to f20200065@hyderabad.bits-pilani.ac.in.

I/C
(C R Hota)

Date given: 01.11.2022

-----------------------------

You might also like