Longest Continuous Increasing Subsequence
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Given an unsorted array of integers nums, return the length of the longestcontinuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
A continuous increasing subsequence is defined by two indices l and r
(l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]
and for each l <= i < r, nums[i] < nums[i + 1].
Examples
Example 1
Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
4.
Example 2
Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
increasing.
Constraints
1 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9
Solution
Method 1 – Single Pass Linear Scan (1)
Intuition
We can find the longest continuous increasing subsequence by scanning the array once, keeping track of the current increasing segment and updating the maximum length found so far.
Approach
- Initialize two variables:
ansfor the maximum length andcurfor the current segment length. - Iterate through the array:
- If the current number is greater than the previous, increment
cur. - Otherwise, reset
curto 1. - Update
ansifcuris greater.
- If the current number is greater than the previous, increment
- Return
ansas the result.
Code
C++
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int ans = 1, cur = 1;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] > nums[i-1]) cur++;
else cur = 1;
ans = max(ans, cur);
}
return ans;
}
};
Go
func findLengthOfLCIS(nums []int) int {
ans, cur := 1, 1
for i := 1; i < len(nums); i++ {
if nums[i] > nums[i-1] {
cur++
} else {
cur = 1
}
if cur > ans {
ans = cur
}
}
return ans
}
Java
class Solution {
public int findLengthOfLCIS(int[] nums) {
int ans = 1, cur = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i-1]) cur++;
else cur = 1;
ans = Math.max(ans, cur);
}
return ans;
}
}
Kotlin
class Solution {
fun findLengthOfLCIS(nums: IntArray): Int {
var ans = 1
var cur = 1
for (i in 1 until nums.size) {
if (nums[i] > nums[i-1]) cur++
else cur = 1
ans = maxOf(ans, cur)
}
return ans
}
}
Python
class Solution:
def findLengthOfLCIS(self, nums: list[int]) -> int:
ans = cur = 1
for i in range(1, len(nums)):
if nums[i] > nums[i-1]:
cur += 1
else:
cur = 1
ans = max(ans, cur)
return ans
Rust
impl Solution {
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
let mut ans = 1;
let mut cur = 1;
for i in 1..nums.len() {
if nums[i] > nums[i-1] {
cur += 1;
} else {
cur = 1;
}
ans = ans.max(cur);
}
ans
}
}
TypeScript
class Solution {
findLengthOfLCIS(nums: number[]): number {
let ans = 1, cur = 1;
for (let i = 1; i < nums.length; i++) {
if (nums[i] > nums[i-1]) cur++;
else cur = 1;
ans = Math.max(ans, cur);
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n), where n is the length of the array. We scan the array once. - 🧺 Space complexity:
O(1), only a few variables are used.