📌  相关文章
📜  求最长连续子数组的长度

📅  最后修改于: 2022-05-13 01:56:09.923000             🧑  作者: Mango

求最长连续子数组的长度

给定一个包含N个整数的数组arr[] ,任务是找到最长递增子数组的长度,使得子数组中的元素是连续整数。

例子:

方法:这个想法是运行一个循环并保持计数和最大值(最初都是零)。请按照以下步骤操作:

  • 从头到尾运行一个循环
    • 如果当前元素不等于(前一个元素+1),则将计数设置为 1。
    • 否则增加计数。
    • 用 count 和 max 的最大值更新 max。

下面是上述方法的实现。

C++
// C++ program to find longest
// increasing consecutive subarray
#include 
using namespace std;
 
// Returns length of the longest
// consecutive subarray
int findLongestConseqSubarr(vector& v)
{
    int ans = 0, count = 0;
 
    // find the maximum length
    // by traversing the array
    for (int i = 0; i < v.size(); i++) {
 
        // Check if the current element
        // is equal to previous element + 1
        if (i > 0 && v[i] == v[i - 1] + 1)
            count++;
        // reset the count
        else
            count = 1;
 
        // update the maximum
        ans = max(ans, count);
    }
    return ans;
}
 
// Driver code
int main()
{
    vector arr = { 1, 9, 3, 4, 20, 2 };
    cout << findLongestConseqSubarr(arr);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Returns length of the longest
  // consecutive subarray
  static int findLongestConseqSubarr(int  arr[ ])
  {
    int ans = 0, count = 0;
 
    // find the maximum length
    // by traversing the array
    for (int i = 0; i < arr.length; i++) {
 
      // Check if the current element
      // is equal to previous element + 1
      if (i > 0 && arr[i] == arr[i - 1] + 1)
        count++;
       
      // reset the count
      else
        count = 1;
 
      // update the maximum
      ans = Math.max(ans, count);
    }
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    int arr[ ] = { 1, 9, 3, 4, 20, 2 };
    System.out.print(findLongestConseqSubarr(arr));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# python3 program to find longest
# increasing consecutive subarray
 
# Returns length of the longest
# consecutive subarray
def findLongestConseqSubarr(v):
 
    ans, count = 0, 0
 
    # find the maximum length
    # by traversing the array
    for i in range(0, len(v)):
 
        # Check if the current element
        # is equal to previous element + 1
        if (i > 0 and v[i] == v[i - 1] + 1):
            count += 1
             
        # reset the count
        else:
            count = 1
 
        # update the maximum
        ans = max(ans, count)
 
    return ans
 
# Driver code
if __name__ == "__main__":
 
    arr = [1, 9, 3, 4, 20, 2]
    print(findLongestConseqSubarr(arr))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Returns length of the longest
  // consecutive subarray
  static int findLongestConseqSubarr(int[] arr)
  {
    int ans = 0, count = 0;
 
    // find the maximum length
    // by traversing the array
    for (int i = 0; i < arr.Length; i++)
    {
 
      // Check if the current element
      // is equal to previous element + 1
      if (i > 0 && arr[i] == arr[i - 1] + 1)
        count++;
 
      // reset the count
      else
        count = 1;
 
      // update the maximum
      ans = Math.Max(ans, count);
    }
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int[] arr = { 1, 9, 3, 4, 20, 2 };
    Console.Write(findLongestConseqSubarr(arr));
  }
}
 
// This code is contributed by gfgking


Javascript



输出
2

时间复杂度: O(N)
辅助空间: O(1)