Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the length of the longest continuous increasing subsequence from an array of numbers using C#?
The Longest Continuous Increasing Subsequence problem finds the length of the longest subarray where elements are in strictly increasing order. This algorithm uses a single pass through the array to track the current sequence length and maintains the maximum length found so far.
The approach uses two variables: count to track the current increasing sequence length and res to store the maximum length encountered. When an element breaks the increasing pattern, the count resets to 1.
Algorithm Explanation
The algorithm works as follows −
-
Initialize
count = 1andres = 1to handle single-element arrays -
For each element starting from index 1, compare it with the previous element
-
If current element is greater than previous, increment
countand updateres -
If current element is not greater, reset
count = 1
Complexity Analysis
Time Complexity − O(N) where N is the length of the array, as each element is visited exactly once.
Space Complexity − O(1) as only a constant amount of extra space is used regardless of input size.
Example
using System;
public class Solution {
public int FindLengthOfLCIS(int[] nums) {
if (nums == null || nums.Length == 0) {
return 0;
}
int res = 1, count = 1;
for (int i = 1; i < nums.Length; i++) {
if (nums[i] > nums[i - 1]) {
count++;
res = Math.Max(res, count);
} else {
count = 1;
}
}
return res;
}
}
class Program {
static void Main(string[] args) {
Solution solution = new Solution();
int[] nums1 = { 1, 3, 5, 4, 7 };
Console.WriteLine("Array: [1, 3, 5, 4, 7]");
Console.WriteLine("Length of LCIS: " + solution.FindLengthOfLCIS(nums1));
int[] nums2 = { 2, 2, 2, 2, 2 };
Console.WriteLine("\nArray: [2, 2, 2, 2, 2]");
Console.WriteLine("Length of LCIS: " + solution.FindLengthOfLCIS(nums2));
int[] nums3 = { 1, 2, 3, 4, 5 };
Console.WriteLine("\nArray: [1, 2, 3, 4, 5]");
Console.WriteLine("Length of LCIS: " + solution.FindLengthOfLCIS(nums3));
}
}
The output of the above code is −
Array: [1, 3, 5, 4, 7] Length of LCIS: 3 Array: [2, 2, 2, 2, 2] Length of LCIS: 1 Array: [2, 2, 2, 2, 2] Length of LCIS: 1 Array: [1, 2, 3, 4, 5] Length of LCIS: 5
Edge Cases Handling
using System;
class EdgeCasesExample {
public static int FindLengthOfLCIS(int[] nums) {
if (nums == null || nums.Length == 0) {
return 0;
}
int res = 1, count = 1;
for (int i = 1; i < nums.Length; i++) {
if (nums[i] > nums[i - 1]) {
count++;
res = Math.Max(res, count);
} else {
count = 1;
}
}
return res;
}
static void Main(string[] args) {
// Empty array
int[] empty = { };
Console.WriteLine("Empty array: " + FindLengthOfLCIS(empty));
// Single element
int[] single = { 5 };
Console.WriteLine("Single element [5]: " + FindLengthOfLCIS(single));
// Decreasing sequence
int[] decreasing = { 5, 4, 3, 2, 1 };
Console.WriteLine("Decreasing [5,4,3,2,1]: " + FindLengthOfLCIS(decreasing));
}
}
The output of the above code is −
Empty array: 0 Single element [5]: 1 Decreasing [5,4,3,2,1]: 1
Conclusion
The Longest Continuous Increasing Subsequence algorithm efficiently finds the maximum length of strictly increasing subarrays in O(N) time using a simple two-pointer approach. This solution handles all edge cases and provides optimal performance for arrays of any size.
