Longest subarray forming an Arithmetic Progression (AP) with given common difference
Given an array arr[] of N integers and an integer K, the task is to find the length of the longest subarray that forms an Arithmetic Progression having common difference K.
Examples:
Input: arr[] = {3, 4, 5}, K = 1
Output: 3
Explanation: The longest subarray forming an AP with common difference 1 is {3, 4, 5}.
Input: arr[] = {10, 7, 4, 6, 8, 10, 11}, K = 2
Output: 4
Explanation: The longest possible subarray forming an AP with common difference as 2 is {4, 6, 8, 10} .
Brute Force Approach: This approach finds the length of the longest subarray forming an arithmetic progression with the given common difference by looping through each possible subarray in the array and checking if it forms an arithmetic progression with the given common difference. It keeps track of the length of the current subarray and updates the answer if the length of the current subarray is greater than the current maximum length.
- Initialize a variable maxLen to 1.
- Loop through each element arr[i] in the array from index 0 to n-1.
- For each arr[i], initialize a variable count to 1 and a variable prev to arr[i].
- Loop through each element arr[j] in the array from index i+1 to n-1.
- Check if arr[j] – prev is equal to the given common difference. If it is, increment count and set prev to arr[j].
- If arr[j] – prev is not equal to the given common difference, break out of the inner loop.
- After the inner loop, update maxLen to be maximum of maxLen and count.
- After the outer loop, return maxLen as the length of the longest subarray forming an arithmetic progression with the given common difference.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxlenAP( int arr[], int & n, int & d)
{
int maxLen = 1;
for ( int i = 0; i < n; i++) {
int count = 1;
int prev = arr[i];
for ( int j = i + 1; j < n; j++) {
if (arr[j] - prev == d) {
count++;
prev = arr[j];
}
}
maxLen = max(maxLen, count);
}
return maxLen;
}
int main()
{
int arr[] = { 10, 7, 4, 6, 8, 10, 11 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << maxlenAP(arr, N, K);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int maxlenAP( int [] arr, int n, int d)
{
int maxLen = 1 ;
for ( int i = 0 ; i < n; i++) {
int count = 1 ;
int prev = arr[i];
for ( int j = i + 1 ; j < n; j++) {
if (arr[j] - prev == d) {
count++;
prev = arr[j];
}
}
maxLen = Math.max(maxLen, count);
}
return maxLen;
}
public static void main(String[] args)
{
int [] arr = { 10 , 7 , 4 , 6 , 8 , 10 , 11 };
int N = arr.length;
int K = 2 ;
System.out.println(maxlenAP(arr, N, K));
}
}
|
Python
def maxlenAP(arr, n, d):
maxLen = 1
for i in range (n):
count = 1
prev = arr[i]
for j in range (i + 1 , n):
if arr[j] - prev = = d:
count + = 1
prev = arr[j]
maxLen = max (maxLen, count)
return maxLen
def main():
arr = [ 10 , 7 , 4 , 6 , 8 , 10 , 11 ]
N = len (arr)
K = 2
print (maxlenAP(arr, N, K))
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG
{
public static int MaxLenAP( int [] arr, int n, int d)
{
int maxLen = 1;
for ( int i = 0; i < n; i++)
{
int count = 1;
int prev = arr[i];
for ( int j = i + 1; j < n; j++)
{
if (arr[j] - prev == d)
{
count++;
prev = arr[j];
}
}
maxLen = Math.Max(maxLen, count);
}
return maxLen;
}
public static void Main()
{
int [] arr = { 10, 7, 4, 6, 8, 10, 11 };
int N = arr.Length;
int K = 2;
Console.WriteLine(MaxLenAP(arr, N, K));
}
}
|
Javascript
function maxlenAP(arr, n, d) {
let maxLen = 1;
for (let i = 0; i < n; i++) {
let count = 1;
let prev = arr[i];
for (let j = i + 1; j < n; j++) {
if (arr[j] - prev == d) {
count++;
prev = arr[j];
}
}
maxLen = Math.max(maxLen, count);
}
return maxLen;
}
let arr = [10, 7, 4, 6, 8, 10, 11];
let N = arr.length;
let K = 2;
console.log(maxlenAP(arr, N, K));
|
Output:
4
Time Complexity: O(N2)
Auxiliary Space: O(1)
Sliding Window Approach: The given problem is an implementation-based problem that can be solved using the sliding window technique. Follow the steps mentioned below to solve the problem:
- Traverse the given array and maintain a variable that stores the number of variables in the current window.
- If the difference between the current element and the previous element in the array is K, increment the size of the current window, otherwise, reset the size of the window as 1.
- Print the maximum difference as answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int maxlenAP( int arr[], int & n, int & d)
{
int count = 1;
int maxLen = INT_MIN;
for ( int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == d)
count++;
else
count = 1;
maxLen = max(maxLen, count);
}
return maxLen;
}
int main()
{
int arr[] = { 10, 7, 4, 6, 8, 10, 11 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << maxlenAP(arr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int maxlenAP( int []arr, int n, int d)
{
int count = 1 ;
int maxLen = Integer.MIN_VALUE;
for ( int i = 1 ; i < n; i++) {
if (arr[i] - arr[i - 1 ] == d)
count++;
else
count = 1 ;
maxLen = Math.max(maxLen, count);
}
return maxLen;
}
public static void main(String args[])
{
int []arr = { 10 , 7 , 4 , 6 , 8 , 10 , 11 };
int N = arr.length;
int K = 2 ;
System.out.println(maxlenAP(arr, N, K));
}
}
|
Python3
def maxlenAP(arr, n, d):
count = 1
maxLen = 10 * * - 9
for i in range ( 1 , n):
if (arr[i] - arr[i - 1 ] = = d):
count + = 1
else :
count = 1
maxLen = max (maxLen, count)
return maxLen
arr = [ 10 , 7 , 4 , 6 , 8 , 10 , 11 ]
N = len (arr)
K = 2
print (maxlenAP(arr, N, K))
|
C#
using System;
class GFG {
static int maxlenAP( int []arr, int n, int d)
{
int count = 1;
int maxLen = Int32.MinValue;
for ( int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == d)
count++;
else
count = 1;
maxLen = Math.Max(maxLen, count);
}
return maxLen;
}
public static void Main()
{
int []arr = { 10, 7, 4, 6, 8, 10, 11 };
int N = arr.Length;
int K = 2;
Console.Write(maxlenAP(arr, N, K));
}
}
|
Javascript
<script>
function maxlenAP(arr, n, d)
{
let count = 1;
let maxLen = Number.MIN_VALUE;
for (let i = 1; i < n; i++)
{
if (arr[i] - arr[i - 1] == d)
count++;
else
count = 1;
maxLen = Math.max(maxLen, count);
}
return maxLen;
}
let arr = [10, 7, 4, 6, 8, 10, 11];
let N = arr.length;
let K = 2;
document.write(maxlenAP(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
09 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...