Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

SCHOOL OF COMPUTING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


ASSIGNMENT I - QUESTIONS
---------------------------------------------------------------------------------------------------------------------
Course Code/ Course Name : 1151CS120 / Data Structures and Algorithms
Year : Summer 2021-2022
Course Handling Faculty :Mrs.K.VijayaLakshmi. Slot: S6
---------------------------------------------------------------------------------------------------------------------
Level of learning
CO
Course Outcomes domain (Based on
Nos.
revised Bloom’s)
Design modular programs to find solution for computational problem with
CO1 K3
time and space complexities using suitable linear data structure.
Discuss the familiarity of Tree data structure, rule to manipulate those, and
CO2 K2
their canonical applications.

Tas
S.No VTUNo Name
k
1 VTU12021 KURAPATI RAJENDRA PREM KUMAR 1

2 VTU12057 RAVURI GANESH 1

3 VTU12238 V POOVENDHAN 1

4 VTU12325 T RAJADURAI 2

5 VTU12326 S KABILDEV 2

6 VTU12329 M SABARI 2

7 VTU12389 A JASON 3

8 VTU12391 S. MOHAMED ARSHATH 3

9 VTU12392 T ROBERT ROSARIO 3

10 VTU12394 C ANURAG 4

11 VTU12398 JAIE PRAMOD S 4

12 VTU12412 G CHANDRA SASIKANTH 4

5
13 VTU12427 SARAVANAN AKASH BALA

14 VTU13986 ERUKALA VENU GOPAL 5

1
15 VTU15977 MANISH KUMAR 5

16 VTU16698 LAMBU LAVANYA 6

17 VTU16736 GUNTAKA BHARATH KUMAR REDDY 6


AVULA NAGA SATHYA SAI KUMAR
18 VTU17000 REDDY 6

19 VTU17020 JAMPANI AKASH BABU 7

20 VTU17021 BATTULA LOKESH 7


CHALAMALASETTI JEYENDRA
21 VTU17093 SEETHARAM 7

22 VTU17096 KATAKAM SONU 8

23 VTU17100 SYED AHAMAD BASHA 8

24 VTU17210 R TRINADH CHOWDARY 8

25 VTU17257 SHIVAM KUMAR JHA 9

26 VTU17278 MALAPATI SREEKANTH 9

27 VTU17352 NARAHARISETTIMAHALAKSHMI 9

28 VTU17364 MOVVA SIDDHARDHA 10

29 VTU17406 B JAGADISH VARMA 10

30 VTU17460 G GOVARDHA SAI KUMAR 10

31 VTU17505 M PURNA CHANDRA REDDY 11

32 VTU17647 GORANTLA VENKAT 11

33 VTU17743 PARNE AJAY KUMAR REDDY 11

34 VTU17753 GONGALA REDDY VEERA VIJAYA 12

35 VTU17759 K SRIVASANTHI 12

36 VTU17772 L ACHYUTH KUMAR 12

37 VTU18126 GURIJALA PRANAVIPRIYA 13


THALLAPUREDDY SAI BHARATH
38 VTU18310 KUMAR REDDY 13

39 VTU18697 BIKI NATH NEWA 13

GROUP - 1

2
K
S.No List of Questions CO VTU Nos
Level

1 Array Manipulation 1 3
Starting with a 1-indexed array of zeros and a list of operations, for
each operation add a value to each the array element between two
given indices, inclusive. Once all operations have been performed,
return the maximum value in the array.

Example:Queries are interpreted as follows:

abk
153
487
691
Add the values of  between the indices  and  inclusive:
VTU12021

index-> 1 2 3 4 5 6 7 8 9 10 VTU12057

VTU12238
[0,0,0, 0, 0,0,0,0,0, 0]
[3,3,3, 3, 3,0,0,0,0, 0]
[3,3,3,10,10,7,7,7,0, 0]
[3,3,3,10,10,8,8,8,1, 0]
The largest value is  after all operations are performed.

Function Description

Complete the function arrayManipulation in the editor below.

arrayManipulation has the following parameters:

 int n - the number of elements in the array

 int queries[q][3] - a two dimensional array of queries where


each queries[i] contains three integers, a, b, and k.

Returns

3
 int - the maximum value in the resultant array
Input Format
The first line contains two space-separated integers  and , the size of
the array and the number of operations.
Each of the next  lines contains three space-separated
integers ,  and , the left index, right index and summand.
Constraints

Sample Input
53

1 2 100

2 5 100

3 4 100
Sample Output
200
Explanation
After the first update the list is 100 100 0 0 0.
After the second update list is 100 200 100 100 100.
After the third update list is 100 200 200 200 100.

The maximum value is .200

2 Tree: Height of a Binary Tree 2 2


Input Format

The first line contains an integer , the number of nodes in the tree.

Next line contains  space separated integer where th integer denotes

node[i].data.

4
Note: Node values are inserted into a binary search tree before a

reference to the tree's root node is passed to your function. In a

binary search tree, all nodes on the left branch of a node are less

than the node value. All values on the right branch are greater than

the node value.

Constraints

Sample Output

GROUP - 2

K
S.No List of Questions CO VTU Nos
Level

Merge two sorted linked lists


1 1 3 VTU1232
Given pointers to the heads of two sorted linked lists, merge them 5
into a single, sorted linked list. Either head pointer may be null

5
meaning that the corresponding list is empty. VTU1232
6
Example VTU1232
9

 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 , the number of test cases.

The format for each test case is as follows:

The first line contains an integer , the length of the first linked list.
The next  lines contain an integer each, the elements of the linked
list.
The next line contains an integer , the length of the second linked
list.
The next  lines contain an integer each, the elements of the second
linked list.
Constraints

where  is the  element of the list.


Sample Input
1

6
3

4
Sample Output
12334

Tree: Preorder Traversal


Complete the  function in the editor below, which has  parameter: a
pointer to the root of a binary tree. It must print the values in the
tree's preorder traversal as a single line of space-separated values.
Input Format
Our test code passes the root node of a binary tree to
the preOrder function.
Constraints

Output Format
Print the tree's preorder traversal as a single line of space-separated
2 values. 2 2
Sample Input
1
\
2
\
5
/ \
3 6
\
4
Sample Output
125346

GROUP - 3

S.No List of Questions CO K VTU Nos

7
Level

1 Insert a node at a specific position in a linked list 1 3


Given the pointer to the head node of a linked list and an integer to
insert at a certain position, create a new node with the given integer
as its  data attribute, insert this node at the desired position and
return the head node.

A position of 0 indicates head, a position of 1 indicates one node


away from the head and so on. The head pointer given may be null
meaning that the initial list is empty.

Example
 head refers to the first node in the list 1->2->3

data=4

Position=2

Insert a node at position 2  with data . The new list is 1->2->4->3

Function Description Complete the


function insertNodeAtPosition in the editor below. It must return a
reference to the head node of your finished list. VTU12392

insertNodeAtPosition has the following parameters: VTU12389

VTU12391
 head: a SinglyLinkedListNode pointer to the head of the list

 data: an integer value to insert as data in your new node

 position: an integer position to insert the new node, zero


based indexing

Returns

 SinglyLinkedListNode pointer: a reference to the head of the


revised list
Constraints

where  is the  element of the linked list.

8
Sample Input
3

16

13

2
Sample Output
16 13 1 7

Explanation
The initial linked list is 16->13->7. Insert 1 at the position 2 which
currently has 7  in it. The updated linked list is 16->13->1>17.

2 Is This a Binary Search Tree? 2 2

For the purposes of this challenge, we define a binary tree to be


a binary search tree with the following ordering requirements:

 The  data value of every node in a node's left subtree is less


than the data value of that node.

 The   data value of every node in a node's right subtree


is greater than the data value of that node.

Given the root node of a binary tree, can you determine if it's also a
binary search tree?

Complete the function in your editor below, which has 1 parameter:


a pointer to the root of a binary tree. It must return
a boolean denoting whether or not the binary tree is a binary search
tree. You may have to write one or more helper functions to
complete this challenge.
Input Format
You are not responsible for reading any input from stdin. Hidden
code stubs will assemble a binary tree and pass its root node to your

9
function as an argument.
Constraints

Output Format
You are not responsible for printing any output to stdout. Your
function must return true if the tree is a binary search tree;
otherwise, it must return false. Hidden code stubs will print this
result as a Yes or No answer on a new line.

Sample Output

No

GROUP - 4

K
S.No List of Questions CO VTU Nos
Level

10
1 Queue using Two Stacks 1 3

A queue is an abstract data type that maintains the order in which


elements were added to it, allowing the oldest elements to be
removed from the front and new elements to be added to the rear.
This is called a First-In-First-Out (FIFO) data structure because the
first element added to the queue (i.e., the one that has been waiting
the longest) is always the first one to be removed.

A basic queue has the following operations:

 Enqueue: add a new element to the end of the queue.

 Dequeue: remove the element from the front of the queue


and return it.

In this challenge, you must first implement a queue using two


stacks. Then process q queries, where each query is one of the
following 3  types:
VTU12394
1. 1 x: Enqueue element x into the end of the queue.
VTU12398
2. 2: Dequeue the element at the front of the queue. VTU12412

3. 3: Print the element at the front of the queue.


Input Format
The first line contains a single integer, q, denoting the number of
queries.
Each line i of the  q subsequent lines contains a single query in the
form described in the problem statement above. All three queries
start with an integer denoting the query type , but only query   1 is
followed by an additional space-separated value,x , denoting the
value to be enqueued.
Constraints

It is guaranteed that a valid answer always exists for each query of


type .
Output Format

11
For each query of type , print the value of the element at the front of
the queue on a new line.

Sample Input
STDIN Function

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

10 q = 10 (number of queries)

1 42 1st query, enqueue 42

2 dequeue front element

1 14 enqueue 42

3 print the front element

1 28 enqueue 28

3 print the front element

1 60 enqueue 60

1 78 enqueue 78

2 dequeue front element

2 dequeue front element


Sample Output
14

14

2 HEAP 2 2
This question is designed to help you get a better understanding
of basic heap operations.

There are  types of query:

 " " - Add an element  to the heap.

 " " - Delete the element  from the heap.

12
 "" - Print the minimum of all the elements in the heap.

NOTE: It is guaranteed that the element to be deleted will be there


in the heap. Also, at any instant, only distinct elements will be in the
heap.
Input Format
The first line contains the number of queries, .
Each of the next  lines contains one of the  types of query.

Constraints

Output Format
For each query of type , print the minimum value on a single line.
Sample Input
STDIN Function

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

5 Q=5

14 insert 4

19 insert 9

3 print minimum

24 delete 4

3 print minimum
Sample Output
4

GROUP - 5

S. List of Questions C K VTU Nos

13
Lev
No O
el

1 Game of Two Stacks 1 3


Alexa has two stacks of non-negative integers, stack a[n] and stack 
b[m] where index 0  denotes the top of the stack. Alexa challenges Nick to
play the following game:

 In each move, Nick can remove one integer from the top of either
stack a or stack b.

 Nick keeps a running sum of the integers he removes from the two
stacks.

 Nick is disqualified from the game if, at any point, his running sum
becomes greater than some integer maxsum   given at the beginning of the
game.

 Nick's final score is the total number of integers he has removed from


the two stacks.

Given ,a ,b and   maxsum for g  games, find the maximum possible score
Nick can achieve. VTU12427

VTU13986
Example
VTU15977
a=[1,2,3,4,5]

b=[6,7,8,9]
The maximum number of values Nick can remove is 4. There are two sets of
choices with this result.

1. Remove 1,2,3,4  from  with a sum of .

2. Remove 1,2,3  from  a and  6  from b  with a sum of 12

Function Description
Complete the twoStacks function in the editor below.

twoStacks has the following parameters: - int maxSum: the maximum allowed


sum
- int a[n]: the first stack
- int b[m]: the second stack

Returns
- int: the maximum number of selections Nick can make
Input Format

14
The first line contains an integer,g  (the number of games). The 3 .g
subsequent lines describe each game in the following format:

1. The first line contains three space-separated integers describing the


respective values of  n(the number of integers in stack a ),m  (the number of
integers in stack b ), and maxsum  (the number that the sum of the integers
removed from the two stacks cannot exceed).

2. The second line contains  n space-separated integers, the respective


values of .

3. The third line contains  m space-separated integers, the respective


values of .
Constraints

Subtasks

Sample Input 0
1

5 4 10

42461

2185
Sample Output 0
4

2 Balanced Brackets 2 2
A bracket is considered to be any one of the following characters: (, ), {, }, [,
or ].

Two brackets are considered to be a matched pair if the an opening bracket


(i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact
same type. There are three types of matched pairs of brackets: [], {}, and ().

15
A matching pair of brackets is not balanced if the set of brackets it encloses
are not matched. For example, {[(])} is not balanced because the contents in
between { and } are not balanced. The pair of square brackets encloses a
single, unbalanced opening bracket, (, and the pair of parentheses encloses a
single, unbalanced closing square bracket, ].

By this logic, we say a sequence of brackets is balanced if the following


conditions are met:

 It contains no unmatched brackets.

 The subset of brackets enclosed within the confines of a matched pair


of brackets is also a matched pair of brackets.

Given  n  strings of brackets, determine whether each sequence of brackets is


balanced. If a string is balanced, return YES. Otherwise, return NO.

Function Description

Complete the function isBalanced in the editor below.

isBalanced has the following parameter(s):

 string s: a string of brackets

Returns

 string: either YES or NO
Input Format
The first line contains a single integer n, the number of strings.
Each of the next n lines contains a single string , a sequence of brackets.

Output Format
For each string, return YES or NO.

Sample Input
STDIN Function ----- -------- 3 n = 3 {[()]} first s = '{[()]}' {[(])} second s =
'{[(])}' {{[[(())]]}} third s ='{{[[(())]]}}'

Sample Output:YES NO YES

16
GROUP - 6

K
S. C
List of Questions Lev VTU Nos
No O
el

1 Castle on the Grid 1 3 Vtu16698,


You are given a square grid with some cells open (.) and some blocked (X). Vty16736,
Your playing piece can move along any row or column until it reaches the
edge of the grid or a blocked cell. Given a grid, a start and a goal, determine Vtu17000
the minmum number of moves to get to the goal.

Example.

Function Description
Complete the minimumMoves function in the editor.

minimumMoves has the following parameter(s):

 string grid[n]: an array of strings that represent the rows of the grid

 int startX: starting X coordinate

 int startY: starting Y coordinate

 int goalX: ending X coordinate

 int goalY: ending Y coordinate

Returns

 int: the minimum moves to reach the goal

17
Sample Input
STDIN FUNCTION

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

3 grid[] size n = 3

.X. grid = ['.X.','.X.', '...']

.X.

...

0002 startX = 0, startY = 0, goalX = 0, goalY = 2


Sample Output
3

2 Find the Running Median 2 2


The median of a set of integers is the midpoint value of the data set for which
an equal number of integers are less than and greater than the value. To find
the median, you must first sort your set of integers in non-decreasing order,
then:

 If your set contains an odd number of elements, the median is the


middle element of the sorted sample. In the sorted set

Each of the median values is stored in an array and the array is returned for the
main function to print.
Note: Add formatting to the print statement.
Function Description
Complete the runningMedian function in the editor below.
runningMedian has the following parameters:
- int a[n]: an array of integers
Returns
- float[n]: the median of the array after each insertion, modify the print
statement in main to get proper formatting.

18

Sample Input
STDIN Function
6 a[] size n = 6
12 a = [12, 4, 5, 3, 8, 7]
4
5
3
8
7
Sample Output
12.0
8.0
5.0
19
4.5
5.0
6.0

GROUP - 7

K
S. C
List of Questions Lev VTU Nos
No O
el

1 Compare two linked lists 1 3 Vtu17020,


You’re given the pointer to the head nodes of two linked lists. Compare the
Vtu17021,
data in the nodes of the linked lists to check if they are equal. If all data
attributes are equal and the lists are the same length, return 1. Otherwise, 0 Vtu17093
return .

Example

The two lists have equal data attributes for the first  3 nodes. List 2 is longer,
though, so the lists are not equal. Return 0

Function Description

Complete the compare_lists function in the editor below.

compare_lists has the following parameters:

 SinglyLinkedListNode llist1: a reference to the head of a list

 SinglyLinkedListNode llist2: a reference to the head of a list

Returns

 int: return 1 if the lists are equal, or 0 otherwise


Input Format
The first line contains an integer , the number of test cases.

Each of the test cases has the following format:


The first line contains an integer , the number of nodes in the first linked list.
Each of the next  lines contains an integer, each a value for a data attribute.
The next line contains an integer , the number of nodes in the second linked
list.

20
Each of the next  lines contains an integer, each a value for a data attribute.

Output Format
Compare the two linked lists and return 1 if the lists are equal.
Otherwise, return 0. Do NOT print anything to stdout/console.

The output is handled by the code in the editor and it is as follows:

For each test case, in a new line, print  if the two lists are equal, else print .
Sample Input
2

2
Sample Output
0

2 Same Tree 2 2

21
Given the roots of two binary trees p and q, write a function to check if they
are the same or not.

Two binary trees are considered the same if they are structurally identical, and
the nodes have the same value.

GROUP - 8

K
S.No List of Questions CO VTU Nos
Level

1 Queue Reconstruction by Height: 1 3 Vtu17096,

22
You are given an array of people, people, which are the attributes of Vtu17100,
some people in a queue (not necessarily in order). Each people[i] =
[hi, ki] represents the ith person of height hi with exactly ki other Vtu17210
people in front who have a height greater than or equal to hi.

Reconstruct and return the queue that is represented by the input


array people. The returned queue should be formatted as an
array queue, where queue[j] = [hj, kj] is the attributes of
the jth person in the queue (queue[0] is the person at the front of the
queue).

 Example 1:

Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]

Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]

Explanation:

Person 0 has height 5 with no other people taller or the same height
in front.

Person 1 has height 7 with no other people taller or the same height
in front.

Person 2 has height 5 with two persons taller or the same height in
front, which is person 0 and 1.

Person 3 has height 6 with one person taller or the same height in
front, which is person 1.

Person 4 has height 4 with four people taller or the same height in
front, which are people 0, 1, 2, and 3.

Person 5 has height 7 with one person taller or the same height in
front, which is person 1.

Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.

Constraints:

 1 <= people.length <= 2000


 0 <= hi <= 106
 0 <= ki < people.length
 It is guaranteed that the queue can be reconstructed.

23
All Elements in Two Binary Search Trees
Given two binary search trees root1 and root2.

Return a list containing all the integers from both trees sorted


in ascending order.

 Example 1:

Input: root1 = [2,1,4], root2 = [1,0,3]

Output: [0,1,1,2,3,4]

Example 2:
2 2 2
Input: root1 = [0,-10,10], root2 = [5,1,7,0,2]

Output: [-10,0,0,1,2,5,7,10]

GROUP - 9

K
S.No List of Questions CO VTU Nos
Level

1 Design Front Middle Back Queue: 1 3 Vtu17257,

Design a queue that supports push and pop operations in the front, Vtu17278,


middle, and back. Vtu17352
Implement the FrontMiddleBack class:

 FrontMiddleBack() Initializes the queue.


 void pushFront(int val) Adds val to the front of the queue.
 void pushMiddle(int val) Adds val to the middle of the
queue.
24
 void pushBack(int val) Adds val to the back of the queue.
 int popFront() Removes the front element of the queue and
returns it. If the queue is empty, return -1.
 int popMiddle() Removes the middle element of the queue
and returns it. If the queue is empty, return -1.
 int popBack() Removes the back element of the queue and
returns it. If the queue is empty, return -1.

Notice that when there are two middle position choices, the


operation is performed on the frontmost middle position choice.
For example:

 Pushing 6 into the middle of [1, 2, 3, 4, 5] results in [1, 2, 6,


3, 4, 5].
 Popping the middle from [1, 2, 3, 4, 5, 6] returns 3 and
results in [1, 2, 4, 5, 6].

Example 1:

Input:

["FrontMiddleBackQueue", "pushFront", "pushBack",


"pushMiddle", "pushMiddle", "popFront", "popMiddle",
"popMiddle", "popBack", "popFront"]

[[], [1], [2], [3], [4], [], [], [], [], []]

Output:

[null, null, null, null, null, 1, 3, 4, 2, -1]

Explanation:

FrontMiddleBackQueue q = new FrontMiddleBackQueue();

q.pushFront(1); // [1]

q.pushBack(2); // [1, 2]

q.pushMiddle(3); // [1, 3, 2]

25
q.pushMiddle(4); // [1, 4, 3, 2]

q.popFront(); // return 1 -> [4, 3, 2]

q.popMiddle(); // return 3 -> [4, 2]

q.popMiddle(); // return 4 -> [2]

q.popBack(); // return 2 -> []

q.popFront(); // return -1 -> [] (The queue is empty)

 Constraints:

 1 <= val <= 109


 At most 1000 calls will be made
to pushFront, pushMiddle, pushBack, popFront, popMiddle,
and popBack.

2 2 2
Check If a String Contains All Binary Codes of Size K
Given a binary string s and an integer k.

Return true if every binary code of length k is a substring of s.


Otherwise, return false.

 Example 1:

Input: s = "00110110", k = 2

Output: true

Explanation: The binary codes of length 2 are "00", "01", "10" and
"11". They can be all found as substrings at indicies 0, 1, 3 and 2
respectively.

Example 2:

Input: s = "00110", k = 2

Output: true

Constraints:

 1 <= s.length <= 5 * 105


 s[i] is either '0' or '1'.

26
 1 <= k <= 20

GROUP - 10

K
S.No List of Questions CO VTU Nos
Level

1 Minimum Absolute Difference Queries 1 3 Vtu17364,

Vtu17406,
The minimum absolute difference of an array a is defined as
the minimum value of |a[i] - a[j]|, where 0 <= i < j < Vtu17460
a.length and a[i] != a[j]. If all elements of a are the same, the
minimum absolute difference is -1.

 For example, the minimum absolute difference of the


array [5,2,3,7,2] is |2 - 3| = 1. Note that it is
not 0 because a[i] and a[j] must be different.

You are given an integer array nums and the


array queries where queries[i] = [li, ri]. For each query i, compute
the minimum absolute difference of
the subarray nums[li...ri] containing the elements of nums between
the 0-based indices li and ri (inclusive).

Return an array  ans where ans[i] is the answer to the ith query.

A subarray is a contiguous sequence of elements in an array.

The value of |x| is defined as:

 x if x >= 0.
 -x if x < 0.

 Example 1: Input: nums = [1,3,4,8], queries = [[0,1],[1,2],[2,3],


[0,3]]

Output: [2,1,4,1]

Explanation: The queries are processed as follows:

- queries[0] = [0,1]: The subarray is [1,3] and the minimum absolute

27
difference is |1-3| = 2.

- queries[1] = [1,2]: The subarray is [3,4] and the minimum absolute


difference is |3-4| = 1.

- queries[2] = [2,3]: The subarray is [4,8] and the minimum absolute


difference is |4-8| = 4.

- queries[3] = [0,3]: The subarray is [1,3,4,8] and the minimum


absolute difference is |3-4| = 1.

Example 2:Input: nums = [4,5,2,2,7,10], queries = [[2,3],[0,2],


[0,5],[3,5]]

Output: [-1,1,1,3]

Explanation: The queries are processed as follows:

- queries[0] = [2,3]: The subarray is [2,2] and the minimum absolute


difference is -1 because all the

elements are the same.

- queries[1] = [0,2]: The subarray is [4,5,2] and the minimum


absolute difference is |4-5| = 1.

- queries[2] = [0,5]: The subarray is [4,5,2,2,7,10] and the minimum


absolute difference is |4-5| = 1.

- queries[3] = [3,5]: The subarray is [2,7,10] and the minimum


absolute difference is |7-10| = 3.

 Constraints:

 2 <= nums.length <= 105


 1 <= nums[i] <= 100
 1 <= queries.length <= 2 * 104
 0 <= li < ri < nums.length

2 2 2
  Binary Search Tree Iterator
Implement the BSTIterator class that represents an iterator over
the in-order traversal of a binary search tree (BST):

28
 BSTIterator(TreeNode root) Initializes an object of
the BSTIterator class. The root of the BST is given as part of
the constructor. The pointer should be initialized to a non-
existent number smaller than any element in the BST.
 boolean hasNext() Returns true if there exists a number in
the traversal to the right of the pointer, otherwise
returns false.
 int next() Moves the pointer to the right, then returns the
number at the pointer.

Notice that by initializing the pointer to a non-existent smallest


number, the first call to next() will return the smallest element in the
BST.

You may assume that next() calls will always be valid. That is, there
will be at least a next number in the in-order traversal when next() is
called.

Input

["BSTIterator", "next", "next", "hasNext", "next", "hasNext",


"next", "hasNext", "next", "hasNext"]

[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]

Output

[null, 3, 7, true, 9, true, 15, true, 20, false]

Explanation

BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9,

29
20]);

bSTIterator.next(); // return 3

bSTIterator.next(); // return 7

bSTIterator.hasNext(); // return True

bSTIterator.next(); // return 9

bSTIterator.hasNext(); // return True

bSTIterator.next(); // return 15

bSTIterator.hasNext(); // return True

bSTIterator.next(); // return 20

bSTIterator.hasNext(); // return False

Constraints:

 The number of nodes in the tree is in the range [1, 105].


 0 <= Node.val <= 106
 At most 105 calls will be made to hasNext, and next.

GROUP - 11

K
S.No List of Questions CO VTU Nos
Level

1 Merge In Between Linked Lists: 1 3 Vtu17505,

Vtu17647,
You are given two linked lists: list1 and list2 of
sizes n and m respectively. Vtu17743

Remove list1's nodes from the ath node to the bth node, and


put list2 in their place.

The blue edges and nodes in the following figure incidate the result:

Input: list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 =

30
[1000000,1000001,1000002]

Output: [0,1,2,1000000,1000001,1000002,5]

Explanation: We remove the nodes 3 and 4 and put the entire list2
in their place. The blue edges and nodes in the above figure indicate
the result.

2 2 2
Binary Search Tree to Greater Sum Tree
Given the root of a Binary Search Tree (BST), convert it to a
Greater Tree such that every key of the original BST is changed to
the original key plus sum of all keys greater than the original key in
BST.

As a reminder, a binary search tree is a tree that satisfies these


constraints:

 The left subtree of a node contains only nodes with keys less


than the node's key.
 The right subtree of a node contains only nodes with
keys greater than the node's key.
 Both the left and right subtrees must also be binary search
trees.

Example 1:

31
 

Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]

Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Constraints:

 The number of nodes in the tree is in the range [1, 100].


 0 <= Node.val <= 100
 All the values in the tree are unique.
 root is guaranteed to be a valid binary search tree.

GROUP - 12

K
S.No List of Questions CO VTU Nos
Level

1 Validate Stack Sequences 1 3 Vtu17753,


Given two integer arrays pushed and popped each with distinct values, Vtu17759,
return true if this could have been the result of a sequence of push and
pop operations on an initially empty stack, or false otherwise. Vtu17772

 Example 1:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]

Output: true

32
Explanation: We might do the following sequence:

push(1), push(2), push(3), push(4),

pop() -> 4,

push(5),

pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

Example 2:

Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]

Output: false

Explanation: 1 cannot be popped before 2.

 Constraints:

 1 <= pushed.length <= 1000


 0 <= pushed[i] <= 1000
 All the elements of pushed are unique.
 popped.length == pushed.length
 popped is a permutation of pushed.

2 2 2
Verify Preorder Serialization of a Binary Tree:
One way to serialize a binary tree is to use preorder traversal. When we encounter a
non-null node, we record the node's value. If it is a null node, we record using a sentinel
value such as '#'.

or example, the above binary tree can be serialized to the


string "9,3,4,#,#,1,#,#,2,#,6,#,#", where '#' represents
a null node.

Given a string of comma-separated values preorder,


return true if it is a correct preorder traversal serialization of a
binary tree.

33
It is guaranteed that each comma-separated value in the string
must be either an integer or a character '#' representing null
pointer.

You may assume that the input format is always valid.

 For example, it could never contain two consecutive


commas, such as "1,,3".

Note: You are not allowed to reconstruct the tree.

GROUP - 13

K
S.No List of Questions CO VTU Nos
Level

1  Longest Substring Without Repeating Characters 1 3 Vtu18126,


vtu18310,
Given a string s, find the length of the longest substring without
repeating characters. Vtu18697

 Example 1:

Input: s = "abcabcbb"

Output: 3

Explanation: The answer is "abc", with the length of 3.

Example 2:

34
Input: s = "bbbbb"

Output: 1

Explanation: The answer is "b", with the length of 1.

 Constraints:

 0 <= s.length <= 5 * 104


 s consists of English letters, digits, symbols and spaces.

Divide Apples

Problem

N boys are sitting in a circle. Each of them have some apples in their
hand. You find that the total number of the apples can be divided by N. So
you want to divide the apples equally among all the boys. But they are so
lazy that each one of them only wants to give one apple to one of the
neighbors at one step. Calculate the minimal number of steps to make
each boy have the same number of apples.

Input

The first line of input is an integer N. 2 <= N <= 10000 The second line is
N integers indicates the number of apples of the ith boy. Each integer is
2 positive and no more than 10^9. 2 2

Output

A single line contains the minimal number of steps to make each boy have
the same number of apples.

Sample Input

1397

Sample Output

Course Handling Faculty Course Coordinator H.O.D/CSE

35

You might also like