Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Longest subarray forming an Arithmetic Progression (AP) with given common difference

  • Difficulty Level : Basic
  • Last Updated : 07 Jan, 2022

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} .

 

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




// 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




// 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.

Python3




# 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#




// 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.

Javascript




<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>

 
 

Output
4

 Time Complexity: O(N)
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!