Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Longest Increasing Subsequence

Medium Acceptance 20.00% Points 30.00

Given an array arr[] of size n, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in which the elements of the subsequence are sorted in increasing order.

Examples
Example 1

Example 1

Input: arr[] = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]

Output: 6

Explanation: One of the possible longest strictly increasing subsequences is [0, 2, 6, 9, 13, 15], which has a length of 6.

Example 2

Example 2

Input: arr[] = [3, 10, 2, 1, 20]

Output: 3

Explanation: The longest strictly increasing subsequence could be [3, 10, 20], which has a length of 3.

Hints
Hint 1
Expected Time Complexity: O(n * n)
Hint 2
Expected Auxiliary Space: O(n)
Constraints
  • 1 = arr.size() = 10^3
  • 0 = arr[i] = 10^6
Companies
Amazon Microsoft Samsung Paytm
Topics
Array Dynamic Programming
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions