0% found this document useful (0 votes)
10 views3 pages

FDSA Assignment-2

Uploaded by

Chirag Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

FDSA Assignment-2

Uploaded by

Chirag Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Birla Institute of Technology and Science, Pilani Hyd Campus

BITS F232: Foundations of Data Structures and Algorithms


1st Semester 2022-23
Assignment-2 (stack, queue, deque), Max Marks: 08
Date of Submission: 15th Oct 2022
Q.1 Suppose you are given an n-element array A containing distinct integers that are listed in increasing order.
Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What
is the running time of your algorithm? Write a program in C++ to measure experimentally how much time does
your implementation take.

Q.2. There are n people standing in a row, with non-negative heights. Sam can deal with exactly k people at a
time. Starting from the first person, help Sam find the height of the tallest person amongst every k people
standing consecutively.

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, K, which represents the number of
person Sam can deal with and the second line contains n elements which represents the height of n people
standing in the line. It is guaranteed that the sum of N over all test cases is at most 10^5. 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
52
51234
73
512639

SAMPLE OUTPUT 1:
5234
5669

Q.3 Sumyash has n elements represented in the form of an array. For every element a[i] (i>=0 and i<n), you are
required to find the nearest element which appears after a[i] and is greater than it in O(n).

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, and the second line contains a1,
a2,a3, . . . , an . It is guaranteed that the sum of N over all test cases is at most 10^5. 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.
If there exists no greater element, print -1.

SAMPLE INPUT 1:
2
5
14295
7
5362591

SAMPLE OUTPUT 1:
4 9 9 -1 -1
6 6 9 5 9 -1 -1

Explanation- In 1st case, for 1 next greater element is 4 and not 9. Rest all is self-explanatory.

Q4. Sahil’s favorite data structure is queue. He has first n natural numbers in a random order in a queue. His
friend, Rohit made a bet with Sahil for a Jain pizza if he can sort the elements. In order to do it, he can use an
extra queue and stack.
Determine whether he will be able to get Jain pizza on Thursday’s night.

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, and the second line contains n
elements which are present in the queue. It is guaranteed that the sum of N over all test cases is at most 10^5.
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
51234
7
512634

SAMPLE OUTPUT 1:
YES
NO
Explanation-
For the 1st test case:
Pop the first element of the given Queue i.e 5, then push 5 into the stack. Now, pop all the elements of the given
Queue and push them to second Queue. Now, pop element 5 in the stack and push it to the second Queue.

For 2nd test case:


Push 5 to stack. Pop 1, 2 from given Queue and push it to another Queue. Pop 6 from given Queue and push to
stack. Pop 3, 4 from given Queue and push to second Queue. Now, from using any of above operation, we cannot
push 5 into the second Queue because it is below the 6 in the stack.

Submission Instructions:

You should retain the same group (of assignment1). 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
15/10/2022. Your code should also run on Ubuntu systems (that are there 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 or h20211030070@hyderabad.bits-pilani.ac.in.

I/C
(C R Hota)

Date given: 8/10/2022

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy