Prepin
Log in

Longest Ascending Subsequence

Approach / Explanation

The problem known as "Longest Increasing Subsequence" often appears as a programming challenge. Its objective is to determine the length of the longest subsequence within a given sequence, where the elements of the subsequence are sorted in ascending order and the subsequence is maximally lengthy. This subsequence does not need to be contiguous or unique. To effectively tackle this question, it's important to grasp the idea of a subsequence, recognize how to identify an increasing subsequence, and understand the logic or algorithms used to find the longest one. Here are some important aspects to keep in mind when addressing this question: Differentiate between subsequence and substring : a subsequence can be formed from another sequence by removing some or none of the elements while preserving the order of the remaining elements. This contrasts with a substring, where all elements are adjacent. Familiarize yourself with problem-solving techniques : there are various approaches to solve this problem, including dynamic programming and the patience sort technique.

Suggested Answer

For example, consider the sequence [10, 22, 9, 33, 21, 50, 41, 60, 80]; the length of the longest increasing subsequence is 6. The longest increasing subsequence itself is [10, 22, 33, 50, 60, 80], where each successive element is greater than the one before it. To tackle this problem, we can apply dynamic programming. We begin by creating a list to record the longest increasing subsequence that ends at each index. For each index, we examine all preceding indices. If the current element is greater than a previous element, and the longest increasing subsequence at the current index is shorter than the longest increasing subsequence at the previous index plus one, we update our list. Ultimately, the longest increasing subsequence's length is represented by the highest value in this list.

Alternative Answer

For another illustration, take the sequence [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]. The longest increasing subsequence has a length of 6, and one such subsequence is [0, 2, 6, 9, 11, 15]. Additionally, a dynamic programming method can be employed to tackle this problem. By using an array called tabs, where tabs[i] indicates the smallest possible tail element of all increasing subsequences of length (i+1) encountered so far, we can progressively build longer subsequences, ultimately leading us to the solution. This technique is often known as patience sorting.

Question Details

Difficulty & Category

Medium

Product

AI Candidate AgentCompaniesBrowse JobsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin | All rights reserved.