Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

Array a = [1, 3, 2, 3, 1, 4, 5, 2]. Subsequences are chosen by index (positions may repeat values). Count the strictly increasing subsequences whose length equals the longest strictly increasing subsequence length.

Array a = [1, 3, 2, 3, 1, 4, 5, 2]. Subsequences are chosen by index (positions may repeat values). Count the strictly increasing subsequences whose length equals the longest strictly increasing subsequence length.
a=[1,3,2,3,1,4,5,2]
n=len(a); L=[1]*n; C=[1]*n
for i in range(n):
    for j in range(i):
        if a[j]<a[i]:
            if L[j]+1>L[i]: L[i]=L[j]+1; C[i]=C[j]
            elif L[j]+1==L[i]: C[i]+=C[j]
m=max(L)
print(sum(C[i] for i in range(n) if L[i]==m))
What is printed?
  1. A. 1
  2. B. 2
  3. C. 5
  4. D. 20

Correct answer: A. 1

Track for each index the length and the number of ways to reach it; equal-length predecessors add their counts. Allowing equal values merges in extra subsequences, printing the length instead of the count is a plain misreading, and letting a longer predecessor add its ways instead of replacing them overcounts.

More Coding Interview Problems questions

Exams that ask this topic