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

📅  最后修改于: 2021-09-05 11:42:31             🧑  作者: Mango

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

例子:

朴素的方法:解决问题的最简单的方法是遍历数组,对于每个索引i ,从过度索引开始遍历,并从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


Javascript


输出
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live