Data structure Assignment, Exercises of Data Structures and Algorithms

Ther aThe assignment contains multiple questions regarding basic data structure concept.re

Typology: Exercises

2018/2019

Uploaded on 12/21/2019

sallu110
sallu110 🇵🇰

3 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures
Assignment # 4
Solve the following problems using Hashing through C++ Program.
Question # 1
Alice likes festivals a lot as we all do. He also likes spending money on these festivals. He spends
money in buying various things on these festivals. But he has problem of forgetting. He only
remembers his top three maximum spendings for any festival.
For eg, on Holi he spends 25 units on colors, 50 units on water sprays, 100 units on gifts, 150 units
on sweets but he remebers only his top 3 spendings ,i.e., 50, 100 & 150.
Now as the year ends he wants to know the festival on which he spent most of his money that
he can remember.
Input Format
First line contains an integer T, denoting number of test cases.
Second line contains an integer N, denoting number of spendings in a year.
Each of the next N lines contain a string S and an integer X denoting festival name and spending on
one of the events of that festival.
Output Format
For each test case, print a single line containing the festival name and its total spending that he
remembers.
If there are more than 1 festival with the maximum spending, print the one which has lexiographically
smallest name.
Constraints
S will consist of lowercase and/or uppercase English aplhabets
pf3
pf4
pf5

Partial preview of the text

Download Data structure Assignment and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Data Structures

Assignment # 4

Solve the following problems using Hashing through C++ Program.

Question # 1

Alice likes festivals a lot as we all do. He also likes spending money on these festivals. He spends money in buying various things on these festivals. But he has problem of forgetting. He only remembers his top three maximum spendings for any festival. For eg, on Holi he spends 25 units on colors, 50 units on water sprays, 100 units on gifts, 150 units on sweets but he remebers only his top 3 spendings ,i.e., 50, 100 & 150. Now as the year ends he wants to know the festival on which he spent most of his money that he can remember. Input Format First line contains an integer T , denoting number of test cases. Second line contains an integer N , denoting number of spendings in a year. Each of the next N lines contain a string S and an integer X denoting festival name and spending on one of the events of that festival. Output Format For each test case, print a single line containing the festival name and its total spending that he remembers. If there are more than 1 festival with the maximum spending, print the one which has lexiographically smallest name. Constraints S will consist of lowercase and/or uppercase English aplhabets

Hint: Hash Tables

Explanation Test Case 1 : Festival : A , He forgets 2 as he can remember only three max spendings. So he remembers 10 + 10 + 30 = 50 Festival : B , He spent only two times i.e with 20 and 30 units. So he remembers 50. Since total spending for both fetivals is same, we have to chose the festival with lexicographically smallest name. So, our answer is 'A 50'. Test Case 2: Festival: abc - Amount spent - 10 Festival: xyz - Amount spent - 15 Festival: oop - Amount spent - 8 As spendings on xyz are highest. So answer is ' xyz 15 '.

Question # 3

Hint: Hash Tables

Adi has loved an intersting thing today which is subsequence. His friend gave him an array of N integers. Now he asks to find all the possible subsequences of 1 to K which he can form from the given array. The subsequence should contain all the elements from 1,2,3...K. Here K can be any integer. K can be different for different subsequences. Each element of the array should belong to exactly one subsequence. If it possible to do so then print the number of subsequence and in next line print all the subsequence in increasing order of their length. If not then print only the minimum numbers to add to the array such that it is possible to assign each element to a subsequence. For eg : 5 1 3 3 4 5 Now here it is not possible to assign each number to a subsequence. If we add elements 1,2,2 to the array then it is possible. 1 2 3 1 2 3 4 5 So the minimum count of numbers required is 3. INPUT First line contains an integer N denoting the size of the array. Next line contain N space separated integers dentoing the array.

OUTPUT

If possible print the number of subsequences. In next line print all the subsequences in increasing order of their length. If not possible then print only the minimum count of numbers which should be added to the array such that it is possible to assign element to a subsequence. CONSTRAINT