Data Structures & AlgorithmsHardGATE CS · TCS NQT
The function below computes the length of a longest increasing subsequence.
The function below computes the length of a longest increasing subsequence.
import bisect
a = [4, 4, 1, 5, 5, 6, 2, 7]
t = []
for x in a:
i = bisect.bisect_left(t, x)
if i == len(t): t.append(x)
else: t[i] = x
print(len(t))What is printed?- A. 4
- B. 6
- C. 8
- D. 5
Correct answer: A. 4
bisect_left places a new value before equal entries, so repeated values replace rather than extend the tails and the length is the strict LIS. bisect_right lets equal values extend the chain. Counting distinct values is a different quantity.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A maximum subarray sum routine is written as follows. python def bestsub(a): cur = best = a[0] for x in a[1:]: cur = max(x, cur + x) best…
- A knapsack routine is written as follows (items are (weight, value)). python items = [(2, 3), (3, 4), (4, 5)] W = 6 dp = [0] (W + 1) for…
- Two DP loops count ways to pay 5 using coins 1 and 2 (the order of coins in the sum is ignored in the first version). python coins = [1…
- A monotonic deque computes the minimum of each window of size 3. python from collections import deque a = [4, 2, 12, 11, -5, 7] k = 3 dq =…
- A DP counts the minimum number of coins to pay exactly 11 with coins [2, 5], returning −1 if impossible. python INF = float('inf') coins =…
- A binary search finds the largest x with xx <= 50 using the bounds 0 and 50. python lo, hi = 0, 50 while lo < hi: mid = (lo + hi + 1) // 2…