📜  具有给定共同差异的最长算术级数

📅  最后修改于: 2021-04-28 17:34:51             🧑  作者: Mango

给定大小为n和整数d(这是常见差异)的未排序数组,任务是找到最长AP的长度。
对于所有j,大于某些i(

if a[j] = a[i] + (j-i) * d

即a [j]在从索引i到j的a [i]的AP中。

例子:

天真的方法:为每个元素计算它可以形成的最长AP的长度,并在其中打印最大的AP。它涉及O(n 2 )时间复杂度。

一种有效的方法是使用哈希。

创建一个映射,其中键是AP的起始元素,其值是该AP中的元素数。这个想法是每当索引j(> i)的元素可能位于’a的AP中时,将键’a’(位于索引i且其起始元素尚未在地图上的位置)的值更新1。 ‘(作为起始元素)。然后,我们在地图中的所有值中打印最大值。

C++
// C++ code for finding the
// maximum length of AP
  
#include 
using namespace std;
int maxlenAP(int* a, int n, int d)
{
    // key=starting element of an AP,
    // value=length of AP
    unordered_map m;
  
    // since the length of longest AP is at least
    // one i.e. the number itself.
    int maxt = 1;
  
    // if element a[i]'s starting element(i.e., a[i]-i*d)
    // is not in map then its value is 1 else there already
    // exists a starting element of an AP of which a[i]
    // can be a part.
    for (int i = 0; i < n; i++) {
        m[a[i] - i * d]++;
    }
  
    // auto operator stores the key,
    // value pair type from the map.
    for (auto& it : m)
        if (it.second > maxt)
  
            // calculating the length of longest AP.
            maxt = it.second;
  
    return maxt;
}
  
// Driver code
int main()
{
    int n = 10, d = 3;
    int a[10] = { 1, 4, 2, 5, 20, 11, 56, 100, 20, 23 };
  
    cout << maxlenAP(a, n, d) << endl;
  
    return 0;
}


Java
// Java code for finding the
// maximum length of AP
import java.io.*;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Map;
import java.lang.*;
  
class GFG
{
static int maxlenAP(int a[], 
                    int n, int d)
{
    // key=starting element of an AP,
    // value=length of AP
    HashMap m = 
                    new HashMap();
  
    // since the length of longest 
    // AP is at least one i.e. 
    // the number itself.
    int maxt = 1;
  
    // if element a[i]'s starting
    // element(i.e., a[i]-i*d)
    // is not in map then its value
    // is 1 else there already
    // exists a starting element of
    // an AP of which a[i] can be
    // a part.
    for (int i = 0; i < n; i++) 
    {
        if(m.containsKey(a[i] - i * d))
        {
            int freq = m.get(a[i] - i * d);
            freq++;
            m.put(a[i] - i * d, freq);
        }
        else
        {
            m.put(a[i] - i * d, 1);
        }
    }
  
    // auto operator stores the key,
    // value pair type from the map.
    for(Entry val: m.entrySet())
    {
        if (maxt < val.getValue())
            maxt = val.getValue();
    }
    return maxt;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 10, d = 3;
    int a[] = new int[]{ 1, 4, 2, 5, 20, 11, 
                         56, 100, 20, 23 };
  
    System.out.print(maxlenAP(a, n, d) + "\n");
}
}


C#
// C# code for finding the
// maximum length of AP
using System;
using System.Collections.Generic;
  
class GFG
{
    static int maxlenAP(int []a, 
                    int n, int d)
    {
        // key=starting element of an AP,
        // value=length of AP
        Dictionary m = 
                    new Dictionary();
  
        // since the length of longest 
        // AP is at least one i.e. 
        // the number itself.
        int maxt = 1;
  
        // if element a[i]'s starting
        // element(i.e., a[i]-i*d)
        // is not in map then its value
        // is 1 else there already
        // exists a starting element of
        // an AP of which a[i] can be
        // a part.
        for (int i = 0; i < n; i++) 
        {
            if(m.ContainsKey(a[i] - i * d))
            {
                int freq = m[a[i] - i * d];
                freq++;
                m.Remove(a[i] - i * d);
                m.Add(a[i] - i * d, freq);
            }
            else
            {
                m.Add(a[i] - i * d, 1);
            }
        }
  
        // auto operator stores the key,
        // value pair type from the map.
        foreach(var val in m)
        {
            if (maxt < val.Value)
                maxt = val.Value;
        }
        return maxt;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 10, d = 3;
    int []a = new int[]{ 1, 4, 2, 5, 20, 11, 
                        56, 100, 20, 23 };
  
    Console.Write(maxlenAP(a, n, d) + "\n");
}
}
  
// This code is contributed by 29AjayKumar


Python 3
# Python code for finding the
# maximum length of AP
  
def maxlenAP(a, n, d):
  
    # key = starting element of an AP,
    # value = length of AP
    m = dict()
  
    # since the length of longest AP is at least
    # one i.e. the number itself.
    maxt = 1
  
    # if element a[i]'s starting element(i.e., a[i]-i*d)
    # is not in map then its value is 1 else there already
    # exists a starting element of an AP of which a[i]
    # can be a part.
    for i in range(n):
        if (a[i] - i * d) in m:
            m[a[i] - i * d] += 1
        else:
            m[a[i] - i * d] = 1
  
    # In this it variable will be
    # storing key value of dictionary.
    for it in m:
        if m[it] > maxt:
  
            # calculating the length of longest AP.
            maxt = m[it]
  
    return maxt
  
  
# Driver code
if __name__ == "__main__":
    n, d = 10, 3
    a = [1, 4, 2, 5, 20, 11, 56, 100, 20, 23]
    print(maxlenAP(a, n, d))
  
# This code is contributed by
# sanjeev2552


输出:
5