ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
def nlis(a):
    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)
    return sum(C[i] for i in range(n) if L[i] == m)
print(nlis([1, 3, 5, 4, 7]), nlis([2, 2, 2]))
  1. A. 3 3
  2. B. 2 3
  3. C. 2 2
  4. D. 1 3

Correct answer: B. 2 3

For [1, 3, 5, 4, 7] the longest increasing length is 4, reached by 1-3-4-7 and 1-3-5-7, so the count is 2. For [2, 2, 2] strictly increasing runs have length 1 and there are three of them.

More Python Programming questions

Exams that ask this topic