How to find the length of the longest continuous increasing subsequence from an array of numbers using C#?


LongestIncreaingSubsequence returns integer of the continuous subsequence from the array. The method has a for loop, which iterates and keeps the track of the numbers. the final result will have the Max calculated. Time complexity is O(N) because every element is visited once and the space complexity is O(1) because we are not making use of any storage space.

Time complexity − O(N)

Space complexity − O(1)

Example   − {2,4,6,5,8}

Output − 3

Example

public class Arrays{
   public int longestIncreaingSubsequence(int[] nums){
      if (nums == null || nums.Length == 0){
         return -1;
      }
      int res = 0, count = 0;
      for (int i = 0; i < nums.Count(); i++){
         if (i == 0 || nums[i] > nums[i - 1]){
            count++;
            res = Math.Max(res, count);
         }
         else{
            count = 1;
         }
      }
      return res;
   }
}

static void Main(string[] args){
   int[] nums = { 1, 3, 5, 4, 7 };
   Console.WriteLine(s.longestIncreaingSubsequence(nums));
}

Output

3

Updated on: 17-Aug-2021

572 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements