📌  相关文章
📜  连续子元素增加的最长子数组的长度

📅  最后修改于: 2021-05-17 04:23:48             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是从该数组中找到最长的子数组的长度,该子数组由递增的连续数字组成。

例子:

天真的方法:解决该问题的最简单方法是遍历数组,对于每个索引i ,从over-index遍历,并找到从i开始满足给定条件的最长子数组的长度。将i移至不满足条件的索引,然后从该索引进行检查。最后,打印获得的此类子数组的最大长度。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to find the longest subarray
// with increasing contiguous elements
int maxiConsecutiveSubarray(int arr[], int N)
{
 
    // Stores the length of
    // required longest subarray
    int maxi = 0;
 
    for (int i = 0; i < N - 1; i++) {
 
        // Stores the length of length of longest
        // such subarray from ith index
        int cnt = 1, j;
 
        for (j = i; j < N; j++) {
 
            // If consecutive elements are
            // increasing and differ by 1
            if (arr[j + 1] == arr[j] + 1) {
                cnt++;
            }
 
            // Otherwise
            else {
                break;
            }
        }
 
        // Update the longest subarray
        // obtained so far
        maxi = max(maxi, cnt);
        i = j;
    }
 
    // Return the length obtained
    return maxi;
}
 
// Driver Code
int main()
{
    int N = 11;
    int arr[] = { 1, 3, 4, 2, 3, 4,
                  2, 3, 5, 6, 7 };
 
    cout << maxiConsecutiveSubarray(arr, N);
    return 0;
}


Java
// Java implementation for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the longest subarray
// with increasing contiguous elements
public static int maxiConsecutiveSubarray(int arr[],
                                          int N)
{
     
    // Stores the length of
    // required longest subarray
    int maxi = 0;
 
    for(int i = 0; i < N - 1; i++)
    {
         
        // Stores the length of length of
        // longest such subarray from ith
        // index
        int cnt = 1, j;
 
        for(j = i; j < N - 1; j++)
        {
             
            // If consecutive elements are
            // increasing and differ by 1
            if (arr[j + 1] == arr[j] + 1)
            {
                cnt++;
            }
 
            // Otherwise
            else
            {
                break;
            }
        }
 
        // Update the longest subarray
        // obtained so far
        maxi = Math.max(maxi, cnt);
        i = j;
    }
 
    // Return the length obtained
    return maxi;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 11;
    int arr[] = { 1, 3, 4, 2, 3, 4,
                  2, 3, 5, 6, 7 };
 
    System.out.println(maxiConsecutiveSubarray(arr, N));
}
}
 
// This code is contributed by hemanth gadarla


Python3
# Python3 implementation for
# the above approach
 
# Function to find the longest
# subarray with increasing
# contiguous elements
def maxiConsecutiveSubarray(arr, N):
   
    # Stores the length of
    # required longest subarray
    maxi = 0;
 
    for i in range(N - 1):
        # Stores the length of
        # length of longest such
        # subarray from ith index
        cnt = 1;
 
        for j in range(i, N - 1):
 
            # If consecutive elements are
            # increasing and differ by 1
            if (arr[j + 1] == arr[j] + 1):
                cnt += 1;
 
            # Otherwise
            else:
                break;
 
        # Update the longest subarray
        # obtained so far
        maxi = max(maxi, cnt);
        i = j;
 
    # Return the length obtained
    return maxi;
 
# Driver Code
if __name__ == '__main__':
   
    N = 11;
    arr = [1, 3, 4, 2, 3,
           4, 2, 3, 5, 6, 7];
 
    print(maxiConsecutiveSubarray(arr, N));
 
# This code is contributed by Rajput-Ji


C#
// C# implementation for the
// above approach
using System;
class GFG{
     
// Function to find the longest
// subarray with increasing
// contiguous elements
public static int maxiConsecutiveSubarray(int []arr,
                                          int N)
{   
  // Stores the length of
  // required longest subarray
  int maxi = 0;
 
  for(int i = 0; i < N - 1; i++)
  {
    // Stores the length of
    // length of longest such
    // subarray from ith index
    int cnt = 1, j;
 
    for(j = i; j < N - 1; j++)
    {
      // If consecutive elements are
      // increasing and differ by 1
      if (arr[j + 1] == arr[j] + 1)
      {
        cnt++;
      }
 
      // Otherwise
      else
      {
        break;
      }
    }
 
    // Update the longest subarray
    // obtained so far
    maxi = Math.Max(maxi, cnt);
    i = j;
  }
 
  // Return the length
  // obtained
  return maxi;
}
 
// Driver Code
public static void Main(String []args)
{
  int N = 11;
  int []arr = {1, 3, 4, 2, 3, 4,
               2, 3, 5, 6, 7};
  Console.WriteLine(
          maxiConsecutiveSubarray(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


输出
3

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