0% found this document useful (0 votes)
21 views

DSA Lab S2 Section Odd Sem 2024

Lab questions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

DSA Lab S2 Section Odd Sem 2024

Lab questions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DATA STRUCTURES AND ALGORITHMS

ASSIGNMENT-5
Language allowed: C
Due: 16th Oct 2024
5.1 Zero Sum Subarrays
Given an array arr[] of size n. The task is to find the total count of subarrays having their
sum equal to 0.
Input Format
• The first line contains a single integer n which represents the size of the array.

• The second line contains n space-separated integers which represent the elements of the
array arr[].

Examples

Input
n = 6
arr[] = 0 0 5 5 0 0
Output
6
Explanation: The 6 subarrays are [0], [0], [0], [0], [0, 0], and [0, 0].

Input
n = 10
arr[] = 6 -1 -3 4 -2 2 4 6 -12 -7
Output
4
Explanation: The 4 subarrays are [-1, -3, 4], [-2, 2], [2, 4, 6, -12], and [-1, -3,
4, -2, 2].

Constraints
• 1 ≤ n ≤ 103

• −103 ≤ arr[i] ≤ 103

1
5.2 Predecessor and Successor in Binary Search Tree
(BST)
Given a Binary Search Tree (BST) of integers containing N nodes, and an integer X, your
task is to find the inorder predecessor and successor of the integer X in the BST.
Formally, you need to return an array or list containing the inorder predecessor and successor
in that order.
Definitions
- The **inorder predecessor** of a node X in a BST is the largest value that is smaller than
X. - The **inorder successor** of a node X in a BST is the smallest value that is greater than
X.
Note: If there is no inorder predecessor or successor of X, return ‘-1‘ in that place.

Example
For the BST below:

5 10

2 6

The inorder traversal of the tree is: ‘2 5 6 7 8 10‘.


For this tree:
- The inorder predecessor of 6 is 5.
- The inorder successor of 6 is 7.
- The inorder predecessor of 10 is 8.
- The inorder successor of 10 is ‘-1‘ as no node exists after 10.

Sample Input
arr[] = {8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1}
X = 5
Sample Output
{2 6}

Constraints
1 ≤ N ≤ 104

2
5.3 The kth element is all you need, but what you don’t
need to sort the things out!
Problem Statement
You are given a stream of integers. Your task is to design a data structure that supports the
following operations:

• insert(num): Inserts integers into the stream.

• getKthLargest(k): Returns the k-th largest element in the current stream.

Constraints:

• First line contains the number of elements n.

• Second line contains, n unsorted elements, separated by space.

• Third line contains k in which kth largest element to be returned.

• Output the kth largest element.

Note: Do not use any sorting algorithms to find the k-th largest element. Your algorithm
must be deterministic and run in O(n) time when k is a constant.

Example
Operations:
insert(3)
insert(1)
insert(5)
insert(10)
insert(2)

getKthLargest(3) -> 3 (The third largest element is 3)


getKthLargest(1) -> 10 (The largest element is 10)
getKthLargest(4) -> 2 (The fourth largest element is 2)

Test Case 1:
5
5 1 2 7 4
2
5

3
Test Case 2:
6
9 1 3 2 5 7
4
3

4
5.4 Convert Sorted Array to Balanced BST
Problem Statement
Given a sorted array of integers in ascending order, write a program to convert it into a height-
balanced binary search tree (BST) and print all the nodes of the tree.
A height-balanced binary tree is defined as a binary tree in which the depth of the two
subtrees of every node never differs by more than 1.
You must write the program to meet the following conditions:

• The input is a sorted integer array.

• The output should print the balanced BST in level-order as an array, where empty posi-
tions are denoted as null.

Example

Sample Input

[1, 2, 3, 4, 5, 6]

Sample Output

[4, 2, 5, 1, 3, null, 6]

Tree Representation
4

2 5

1 3 6

Figure 1: Height-balanced Binary Search Tree for the Input Array

5
5.5 Median of an Unsorted Array
Problem Statement
Given an unsorted array of integers, write a program to find the median of this array. The
median of a sorted array is defined as the middle element when the number of elements is odd,
and the average of the two middle elements when the number of elements is even.
You must write the program to meet the following conditions:

• The input is an unsorted integer array.

• The output should return the median value.

• The expected time complexity should be O(N) on average, where N is the number of
elements in the array.

Example

Sample Input

[12, 3, 5, 7, 4, 19, 26]

Sample Output

Sample Input

[12, 3, 5, 7, 4, 26]

Sample Output

Explanation
For the first sample input, the sorted sequence of the array is: [3, 4, 5, 7, 12, 19, 26],
and since the number of elements is odd, the median is the 4th element, which is 7.
For the second sample input, the sorted sequence is: [3, 4, 5, 7, 12, 26], and since the
number of elements is even, the median is the average of the 3rd and 4th elements, which is (5
+ 7)/2 = 6.

You might also like