Given an array arr[] of size, N, the task is to find the length of the longest subarray that forms an Arithmetic Progression.

Examples:

_Input: _arr[] = {3, 4, 5}

Output:_ 3_

Explanation:The longest subarray forming an AP is {3, 4, 5} with common difference 1.

_Input: _{10, 7, 4, 6, 8, 10, 11}

Output:_ 4_

Explanation:The longest possible subarray forming an AP is {4, 6, 8, 10} with common difference(= 2).

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays and for each subarray, check if the difference between adjacent elements remains the same throughout or not. Among all such subarrays satisfying the condition, store the length of the longest subarray and print it as the result.

_Time Complexity: _O(N3)

Auxiliary Space:_ O(1)_

Efficient Approach: To optimize the above approach, the idea here is to observe that whenever the difference between the current pair of adjacent elements is not equal to the difference between the previous pair of adjacent elements, compare the length of the previous subarray with the maximum obtained so far and start a new subarray and repeat accordingly. Follow the below steps to solve the problem:

  1. Initialize a variable** res **to store the length of the longest subarray forming an AP.
  2. Iterate over remaining arrays and compare the current adjacent difference with the previous adjacent difference.
  3. Iterate over the array, and for each element, calculate the difference between the current pair of adjacent elements and check if it is equal to the previous pair of adjacent elements. If found to be true, continue the ongoing subarray by incrementing res by 1.
  4. Otherwise, consider a new subarray. Update the maximum length obtained so far, i.e. res by comparing it with the length of the previous subarray.
  5. Finally, return the** res** as the required answer.

Below is the implementation of the above approach:

C++

// C++ Program to implement

// the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to return the length

// of longest subarray forming an AP

**int** getMaxLength(``**int** arr[], **int** N)

{

// Minimum possible length of

// required subarray is 2

**int** res = 2;

// Stores the length of the

// current subarray

**int** dist = 2;

// Stores the common difference

// of the current AP

**int** curradj = (arr[1] - arr[0]);

// Stores the common difference

// of the previous AP

**int** prevadj = (arr[1] - arr[0]);

**for** (``**int** i = 2; i < N; i++) {

curradj = arr[i] - arr[i - 1];

// If the common differences

// are found to be equal

**if** (curradj == prevadj) {

// Continue the previous subarray

dist++;

}

// Start a new subarray

**else** {

prevadj = curradj;

// Update the length to

// store maximum length

res = max(res, dist);

dist = 2;

}

}

// Update the length to

// store maximum length

res = max(res, dist);

// Return the length of

// the longest subarray

**return** res;

}

// Driver Code

**int** main()

{

**int** arr[] = { 10, 7, 4, 6, 8, 10, 11 };

**int** N = **sizeof**``(arr) / **sizeof**``(arr[0]);

cout << getMaxLength(arr, N);

}

Output:

4

_Time Complexity: _O(N)

_Auxiliary Space: _O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #mathematical #searching #arithmetic progression #subarray

Longest subarray forming an Arithmetic Progression (AP)
4.05 GEEK