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:
#include <bits/stdc++.h> using namespace std;
// Function to find longest subarray // forming an Arithmetic Progression // with the given common difference int maxlenAP( int arr[], int & n, int & d)
{ // Stores final answer
int maxLen = 1;
// Loop to traverse array
for ( int i = 0; i < n; i++) {
// Stores the length of the
// current window
int count = 1;
int prev = arr[i];
// Loop to find the longest subarray
// starting at i
for ( int j = i + 1; j < n; j++) {
if (arr[j] - prev == d) {
count++;
prev = arr[j];
}
}
// Update answer
maxLen = max(maxLen, count);
}
// Return Answer
return maxLen;
} // Driver Code 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 program for // the above approach import java.util.Arrays;
public class GFG {
// Function to find longest subarray
// forming an Arithmetic Progression
// with the given common difference
static int maxlenAP( int [] arr, int n, int d)
{
// Stores final answer
int maxLen = 1 ;
// Loop to traverse array
for ( int i = 0 ; i < n; i++) {
// Stores the length of the
// current window
int count = 1 ;
int prev = arr[i];
// Loop to find the longest subarray
// starting at i
for ( int j = i + 1 ; j < n; j++) {
if (arr[j] - prev == d) {
count++;
prev = arr[j];
}
}
// Update answer
maxLen = Math.max(maxLen, count);
}
// Return Answer
return maxLen;
}
// Driver Code
public static void main(String[] args)
{
// Given array
int [] arr = { 10 , 7 , 4 , 6 , 8 , 10 , 11 };
int N = arr.length;
// Given subarray size K
int K = 2 ;
// Function Call
System.out.println(maxlenAP(arr, N, K));
}
} |
# Function to find longest subarray # forming an Arithmetic Progression # with the given common difference def maxlenAP(arr, n, d):
# Stores the final answer
maxLen = 1
# Loop to traverse the array
for i in range (n):
# Stores the length of the current window
count = 1
prev = arr[i]
# Loop to find the longest subarray starting at i
for j in range (i + 1 , n):
if arr[j] - prev = = d:
count + = 1
prev = arr[j]
# Update the answer
maxLen = max (maxLen, count)
# Return the answer
return maxLen
# Driver Code def main():
arr = [ 10 , 7 , 4 , 6 , 8 , 10 , 11 ]
N = len (arr)
K = 2
# Function call
print (maxlenAP(arr, N, K))
if __name__ = = "__main__" :
main()
|
// C# program for // the above approach using System;
public class GFG
{ // Function to find longest subarray
// forming an Arithmetic Progression
// with the given common difference
public static int MaxLenAP( int [] arr, int n, int d)
{
// Stores the final answer
int maxLen = 1;
// Loop to traverse the array
for ( int i = 0; i < n; i++)
{
// Stores the length of the current window
int count = 1;
int prev = arr[i];
// Loop to find the longest subarray starting at i
for ( int j = i + 1; j < n; j++)
{
if (arr[j] - prev == d)
{
count++;
prev = arr[j];
}
}
// Update the answer
maxLen = Math.Max(maxLen, count);
}
// Return the answer
return maxLen;
}
//Driver code public static void Main()
{
int [] arr = { 10, 7, 4, 6, 8, 10, 11 };
int N = arr.Length;
int K = 2;
// Function call
Console.WriteLine(MaxLenAP(arr, N, K));
}
} |
// Function to find longest subarray // forming an Arithmetic Progression // with the given common difference function maxlenAP(arr, n, d) {
// Stores final answer
let maxLen = 1;
// Loop to traverse array
for (let i = 0; i < n; i++) {
// Stores the length of the
// current window
let count = 1;
let prev = arr[i];
// Loop to find the longest subarray
// starting at i
for (let j = i + 1; j < n; j++) {
if (arr[j] - prev == d) {
count++;
prev = arr[j];
}
}
// Update answer
maxLen = Math.max(maxLen, count);
}
// Return Answer
return maxLen;
} // Driver Code 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++ program of the above approach #include <bits/stdc++.h> using namespace std;
// Function to find longest subarray // forming an Arithmetic Progression // with the given common difference int maxlenAP( int arr[], int & n, int & d)
{ // Stores the length of
// the current window
int count = 1;
// Stores final answer
int maxLen = INT_MIN;
// Loop to traverse array
for ( int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == d)
// Increment window size
count++;
else
// Reset window size
count = 1;
// Update answer
maxLen = max(maxLen, count);
}
// Return Answer
return maxLen;
} // Driver Code 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 program for the above approach import java.util.*;
public class GFG {
// Function to find longest subarray // forming an Arithmetic Progression // with the given common difference static int maxlenAP( int []arr, int n, int d)
{ // Stores the length of
// the current window
int count = 1 ;
// Stores final answer
int maxLen = Integer.MIN_VALUE;
// Loop to traverse array
for ( int i = 1 ; i < n; i++) {
if (arr[i] - arr[i - 1 ] == d)
// Increment window size
count++;
else
// Reset window size
count = 1 ;
// Update answer
maxLen = Math.max(maxLen, count);
}
// Return Answer
return maxLen;
} // Driver Code 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));
} } // This code is contributed by Samim Hossain Mondal. |
# Python code for the above approach # Function to find longest subarray # forming an Arithmetic Progression # with the given common difference def maxlenAP(arr, n, d):
# Stores the length of
# the current window
count = 1
# Stores final answer
maxLen = 10 * * - 9
# Loop to traverse array
for i in range ( 1 , n):
if (arr[i] - arr[i - 1 ] = = d):
# Increment window size
count + = 1
else :
# Reset window size
count = 1
# Update answer
maxLen = max (maxLen, count)
# Return Answer
return maxLen
# Driver Code arr = [ 10 , 7 , 4 , 6 , 8 , 10 , 11 ]
N = len (arr)
K = 2
print (maxlenAP(arr, N, K))
# This code is contributed by gfgking |
// C# program for the above approach using System;
class GFG {
// Function to find longest subarray // forming an Arithmetic Progression // with the given common difference static int maxlenAP( int []arr, int n, int d)
{ // Stores the length of
// the current window
int count = 1;
// Stores final answer
int maxLen = Int32.MinValue;
// Loop to traverse array
for ( int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == d)
// Increment window size
count++;
else
// Reset window size
count = 1;
// Update answer
maxLen = Math.Max(maxLen, count);
}
// Return Answer
return maxLen;
} // Driver Code 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));
} } // This code is contributed by Samim Hossain Mondal. |
<script> // JavaScript code for the above approach
// Function to find longest subarray
// forming an Arithmetic Progression
// with the given common difference
function maxlenAP(arr, n, d)
{
// Stores the length of
// the current window
let count = 1;
// Stores final answer
let maxLen = Number.MIN_VALUE;
// Loop to traverse array
for (let i = 1; i < n; i++)
{
if (arr[i] - arr[i - 1] == d)
// Increment window size
count++;
else
// Reset window size
count = 1;
// Update answer
maxLen = Math.max(maxLen, count);
}
// Return Answer
return maxLen;
}
// Driver Code
let arr = [10, 7, 4, 6, 8, 10, 11];
let N = arr.length;
let K = 2;
document.write(maxlenAP(arr, N, K));
// This code is contributed by Potta Lokesh </script>
|
4
Time Complexity: O(N)
Auxiliary Space: O(1)