data-structures · intermediate · ~15 min

Longest increasing subsequence

Find the LIS length of an array.

Challenge

Implement:

int lis(const int *a, int n);

Return the length of the longest strictly increasing subsequence of a[0..n-1].

Input format

Array a, count n.

Output format

LIS length.

Constraints

O(n^2) DP is fine for the tested sizes.

Starter code

#include <stddef.h>
/* Length of the Longest strictly Increasing Subsequence of a[0..n-1]. */
int lis(const int *a,int n){ (void)a;(void)n; return 0; }

Common mistakes

Requiring contiguity — a subsequence may skip elements.

Edge cases to handle

n==0 -> 0; a flat array -> 1.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.