📌  相关文章
📜  最大化具有给定数组作为子序列的 AP 的公共差异

📅  最后修改于: 2021-09-04 08:33:05             🧑  作者: Mango

给定一个由N 个不同元素组成的排序数组arr[] ,任务是找到一个等差数列的最大可能公差,使得给定的数组是该等差数列的子序列。

例子:

朴素的方法:解决这个问题的最简单的方法是使用变量CD (公差)迭代范围[(arr[N – 1] – arr[0]), 1]并且对于范围内的每个值,检查是否给定的数组可以是等差数列的后续,其中第一个元素为arr[0] ,公共差为CD 。这可以通过简单地检查每对相邻数组元素之间的差异是否可以被CD整除来实现。如果发现为真,则打印CD的值作为最大可能答案。

时间复杂度:O(N *(MAXM – MINM)),其中MAXMMINM分别是最后和第一阵列元素。
辅助空间: O(1)

高效的方法:上述方法可以基于以下观察进行优化:

请按照以下步骤解决问题:

  • 初始化一个变量,比如maxCD ,以存储等差数列的最大可能公差,使得给定数组是该等差数列的子序列。
  • 使用变量i遍历数组并更新maxCD = GCD(maxCD, (arr[i + 1] – arr[i])) 的值
  • 最后,打印maxCD的值。

下面是上述方法的实现

C++
// C++ program to implement
// the above approach
 
#include 
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


输出:
2

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live