Given an array arr[] of length N, the task is to find the length of the longest subarray which consists of consecutive numbers in increasing order, from the array.
Examples:
Input: arr[] = {2, 3, 4, 6, 7, 8, 9, 10}
Output: 5
Explanation: Subarray {6, 7, 8, 9, 10} is the longest subarray satisfying the given conditions. Therefore, the required output is 5.Input: arr[] = {4, 5, 1, 2, 3, 4, 9, 10, 11, 12}
Output: 4
Naive Approach: The simplest approach to solve the problem is to traverse the array and for every index i, traverse from over-index and find the length of the longest subarray satisfying the given condition starting from i. Shift i to the index which does not satisfy the condition and check from that index. Finally, print the maximum length of such subarray obtained.
Below is the implementation of the above approach:Â
C++
// C++ implementation for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the longest subarray// with increasing contiguous elementsint 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 Codeint 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 approachimport java.util.*;Â
class GFG{     // Function to find the longest subarray// with increasing contiguous elementspublic 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 Codepublic 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 elementsdef 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 Codeif __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 approachusing System;class GFG{     // Function to find the longest // subarray with increasing // contiguous elementspublic 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 Codepublic 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
<script>// Javascript program to implement// the above approachÂ
// Function to find the longest subarray// with increasing contiguous elementsfunction maxiConsecutiveSubarray(arr, N){          // Stores the length of    // required longest subarray    let maxi = 0;      for(let i = 0; i < N - 1; i++)    {                  // Stores the length of length of        // longest such subarray from ith        // index        let 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         let N = 11;    let arr = [ 1, 3, 4, 2, 3, 4,                  2, 3, 5, 6, 7 ];      document.write(maxiConsecutiveSubarray(arr, N));     </script> |
3
Time Complexity: O(N2)Â
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
