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

Lab Questions

The document provides instructions for two coding problems - Sliding Window Maximum and Longest Subarray With Sum K. For each problem, it describes the input format, output format, constraints, expected time complexity, and provides sample test cases with explanations. Useful syntax for a max heap storing pairs is also included.

Uploaded by

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

Lab Questions

The document provides instructions for two coding problems - Sliding Window Maximum and Longest Subarray With Sum K. For each problem, it describes the input format, output format, constraints, expected time complexity, and provides sample test cases with explanations. Useful syntax for a max heap storing pairs is also included.

Uploaded by

Prateek Talwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DSA CS F211 Lab 7 March 6, 2024

General instructions: After reading the questions given below, follow the
steps described in Instructions.pdf to see the input and the expected
output for each test case.

1 Sliding Window Maximum


You are given an array of integers of size N , there is a sliding window of
size K which is moving from the very left of the array to the very right. You
have to find the maximum of each window.
Input Format:
• The first line of input contains two space separated integers N and
K. N represents the size of the array and K represents the size of the
window
• The next line contains N integers of the array.
Output Format:
A vector of size N − K + 1 whose ith element contains maximum element in
the ith window
Constraints:
1 ≤ N ≤ 106
1≤K≤N
0 ≤ a[i] ≤ 109

Expected Time Complexity:


O(N log(N ))

1
DSA CS F211 Lab 7 March 6, 2024

TEST CASE 1
N: 8
M: 3
A: [1, 3, −1, −3, 5, 3, 6, 7]
Output: [3, 3, 5, 5, 6, 7]
Explanation:
[1, 3, −1] -maximum is 3
[3, −1, −3] -maximum is 3
[−1, −3, 5] -maximum is 5
[−3, 5, 3] -maximum is 5
[5, 3, 6] -maximum is 6
[3, 6, 7] -maximum is 7

Useful syntax:
Max heap storing pairs
priority_queue<pair<int,int>> pq;
Min heap storing pairs
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;

2 Longest Subarray With Sum K


You are given an array A of size N and an integer K.
Find the length of the longest subarray of A whose sum is equal to K.

Input Format:
• The first line contains two integers, N and K, denoting the size of the
array and the integer K.
• The next line contains N integers of the array.
Output Format:
Print the length of the longest subarray whose sum is equal to K.
Constraints:
1 ≤ N ≤ 106

2
DSA CS F211 Lab 7 March 6, 2024

1 ≤ K ≤ 108
0 ≤ A[i] ≤ 106
Expected Time Complexity:
O(N ), N is the size of the array.

TEST CASE 1
N: 7
K: 3
A: [1, 2, 3, 1, 1, 1, 1]
Output: 3
Explanation:
Subarrays whose sum = 3 are:
[1, 2] -length is 2
[3] -length is 1
[1, 1, 1] -length is 3
[1, 1, 1] -length is 3
Here, the length of the longest subarray is 3, which is our final answer.

TEST CASE 2
N: 6
K: 15
A: [10, 5, 2, 7, 1, 9]
Output: 4
Explanation:
Subarrays whose sum = 15 are:
[10, 5] -length is 2
[5, 2, 7, 1] -length is 4
Here, the length of the longest subarray is 4, which is our final answer.

You might also like