Skip to content
Related Articles

Related Articles

Improve Article
Maximize the common difference of an AP having the given array as a subsequence
  • Last Updated : 04 May, 2021

Given a sorted array arr[] consisting of N distinct elements, the task is to find the maximum possible common difference of an arithmetic progression such that the given array is a subsequence of that arithmetic progression.

Examples:

Input: arr[] = { 2, 4, 6, 8 } 
Output:
Explanation: 
Since arr[] is a subsequence of the arithmetic progression { 2, 4, 6, 8, 10, …}, the common difference of the arithmetic progression is 2.

Input: arr[] = { 2, 5, 11, 23 } 
Output:
Explanation: 
Since arr[] is a subsequence of the arithmetic progression { 2, 5, 8, 11, 14, …, 23, …}, the common difference of the arithmetic progression is 2.

Naive Approach: The simplest approach to solve this problem is to iterate over the range [(arr[N – 1] – arr[0]), 1] using variable CD(common difference) and for every value in the range, check if the given array can be a subsequent of an arithmetic progressions with the first element as arr[0] and the common difference as CD. This is possible by simply checking if the difference between every pair of adjacent array elements is divisible by CD or not. If found to be true, then print the value of CD as the maximum possible answer. 



Time Complexity: O(N * (Maxm – Minm)), where Maxm and Minm are the last and first array elements respectively. 
Auxiliary Space: O(1) 

Efficient Approach: The above approach can be optimized based on the following observations:

If arr[i] is Xth term and arr[0] is the first term of an arithmetic progression with common difference CD, then: 
 

arr[i] = arr[0] + (X – 1) * CD 
=> (arr[i] – arr[0]) = (X – 1) * CD

Therefore, the maximum possible common difference of the AP is the GCD of the absolute difference of each pair of adjacent array lements. 
 

Follow the steps below to solve the problem:

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 find the maximum common
// difference of an AP such that arr[]
// is a subsequence of that AP
int MaxComDiffSubAP(int arr[], int N)
{
    // Stores maximum common difference
    // of an AP with given conditions
    int maxCD = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
 
        // Update maxCD
        maxCD = __gcd(maxCD,
                      arr[i + 1] - arr[i]);
    }
 
    return maxCD;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 7, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << MaxComDiffSubAP(arr, N);
 
    return 0;
}

Java




// Java program to implement
// the above approach 
class GFG
{
 
  // Recursive function to return gcd of a and b
  static int gcd(int a, int b)
  {
 
    // Everything divides 0
    if (a == 0)
      return b;
    if (b == 0)
      return a;
 
    // base case
    if (a == b)
      return a;
 
    // a is greater
    if (a > b)
      return gcd(a - b, b);
 
    return gcd(a, b - a);
  }
 
  // Function to find the maximum common
  // difference of an AP such that arr[]
  // is a subsequence of that AP
  static int MaxComDiffSubAP(int arr[], int N)
  {
     
    // Stores maximum common difference
    // of an AP with given conditions
    int maxCD = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++)
    {
 
      // Update maxCD
      maxCD = gcd(maxCD,
                  arr[i + 1] - arr[i]);
    }
 
    return maxCD;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int arr[] = { 1, 3, 7, 9 };
    int N = arr.length;
 
    System.out.print(MaxComDiffSubAP(arr, N));
  }
}
 
// This code is contributed by AnkThon

Python3




# Python3 program to implement
# the above approach
from math import gcd
 
# Function to find the maximum common
# difference of an AP such that arr[]
# is a subsequence of that AP
def MaxComDiffSubAP(arr, N):
     
    # Stores maximum common difference
    # of an AP with given conditions
    maxCD = 0
 
    # Traverse the array
    for i in range(N - 1):
         
        # Update maxCD
        maxCD = gcd(maxCD, arr[i + 1] - arr[i])
 
    return maxCD
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 1, 3, 7, 9 ]
    N = len(arr)
     
    print(MaxComDiffSubAP(arr, N))
 
# This code is contributed by mohit kumar 29

C#




// C# program to implement
// the above approach 
using System;
 
class GFG{
  
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
 
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return gcd(a - b, b);
     
    return gcd(a, b - a);
}
 
// Function to find the maximum common
// difference of an AP such that arr[]
// is a subsequence of that AP
static int MaxComDiffSubAP(int[] arr, int N)
{
 
    // Stores maximum common difference
    // of an AP with given conditions
    int maxCD = 0;
     
    // Traverse the array
    for(int i = 0; i < N - 1; i++)
    {
         
        // Update maxCD
        maxCD = gcd(maxCD,
                    arr[i + 1] - arr[i]);
    }
    return maxCD;
}
 
// Driver Code
public static void Main ()
{
    int[] arr = { 1, 3, 7, 9 };
    int N = arr.Length;
     
    Console.WriteLine(MaxComDiffSubAP(arr, N));
}
}
 
// This code is contributed by susmitakundugoaldanga

Javascript




<script>
 
// Javascript program for the above approach
 
// Recursive function to return gcd of a and b
function gcd(a, b)
{
     
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return gcd(a - b, b);
     
    return gcd(a, b - a);
}
     
// Function to find the maximum common
// difference of an AP such that arr[]
// is a subsequence of that AP
function MaxComDiffSubAP(arr, N)
{
 
    // Stores maximum common difference
    // of an AP with given conditions
    let maxCD = 0;
     
    // Traverse the array
    for(let i = 0; i < N - 1; i++)
    {
         
        // Update maxCD
        maxCD = gcd(maxCD,
                    arr[i + 1] - arr[i]);
    }
    return maxCD;
}
 
// Driver Code
let arr = [ 1, 3, 7, 9 ];
let N = arr.length;
 
document.write(MaxComDiffSubAP(arr, N));
 
// This code is contributed by splevel62
 
</script>
Output: 
2

 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :