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).
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:
- Initialize variable res to store the length of the longest subarray forming an AP.
- Iterate over remaining arrays and compare the current adjacent difference with the previous adjacent difference.
- 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.
- 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.
- Finally, return the res as the required answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int getMaxLength( int arr[], int N)
{
if (N==1)
return 1;
int res = 2;
int dist = 2;
int curradj = (arr[1] - arr[0]);
int prevadj = (arr[1] - arr[0]);
for ( int i = 2; i < N; i++) {
curradj = arr[i] - arr[i - 1];
if (curradj == prevadj) {
dist++;
}
else {
prevadj = curradj;
res = max(res, dist);
dist = 2;
}
}
res = max(res, dist);
return res;
}
int main()
{
int arr[] = {10, 7, 4, 6, 8, 10, 11};
int N = sizeof (arr) / sizeof (arr[0]);
cout << getMaxLength(arr, N);
}
|
Java
import java.util.*;
class GFG{
static int getMaxLength( int arr[], int N)
{
int res = 2 ;
int dist = 2 ;
int curradj = (arr[ 1 ] - arr[ 0 ]);
int prevadj = (arr[ 1 ] - arr[ 0 ]);
for ( int i = 2 ; i < N; i++)
{
curradj = arr[i] - arr[i - 1 ];
if (curradj == prevadj)
{
dist++;
}
else
{
prevadj = curradj;
res = Math.max(res, dist);
dist = 2 ;
}
}
res = Math.max(res, dist);
return res;
}
public static void main(String[] args)
{
int arr[] = { 10 , 7 , 4 ,
6 , 8 , 10 , 11 };
int N = arr.length;
System.out.print(getMaxLength(arr, N));
}
}
|
Python3
def getMaxLength(arr, N):
res = 2
dist = 2
curradj = (arr[ 1 ] - arr[ 0 ])
prevadj = (arr[ 1 ] - arr[ 0 ])
for i in range ( 2 , N):
curradj = arr[i] - arr[i - 1 ]
if (curradj = = prevadj):
dist + = 1
else :
prevadj = curradj
res = max (res, dist)
dist = 2
res = max (res, dist)
return res
if __name__ = = "__main__" :
arr = [ 10 , 7 , 4 , 6 , 8 , 10 , 11 ]
N = len (arr)
print (getMaxLength(arr, N))
|
C#
using System;
public class GFG{
static int getMaxLength( int []arr,
int N)
{
int res = 2;
int dist = 2;
int curradj = (arr[1] - arr[0]);
int prevadj = (arr[1] - arr[0]);
for ( int i = 2; i < N; i++)
{
curradj = arr[i] - arr[i - 1];
if (curradj == prevadj)
{
dist++;
}
else
{
prevadj = curradj;
res = Math.Max(res, dist);
dist = 2;
}
}
res = Math.Max(res, dist);
return res;
}
public static void Main(String[] args)
{
int []arr = {10, 7, 4,
6, 8, 10, 11};
int N = arr.Length;
Console.Write(getMaxLength(arr, N));
}
}
|
Javascript
<script>
function getMaxLength(arr, N)
{
let res = 2;
let dist = 2;
let curradj = (arr[1] - arr[0]);
let prevadj = (arr[1] - arr[0]);
for (let i = 2; i < N; i++) {
curradj = arr[i] - arr[i - 1];
if (curradj == prevadj) {
dist++;
}
else {
prevadj = curradj;
res = Math.max(res, dist);
dist = 2;
}
}
res = Math.max(res, dist);
return res;
}
let arr= [ 10, 7, 4, 6, 8, 10, 11 ];
let N = arr.length;
document.write(getMaxLength(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Dec, 2021
Like Article
Save Article