给定一个由N 个整数组成的数组arr[] ,任务是找到形成算术级数的最长子序列的长度。
例子:
Input: arr[] = {5, 10, 15, 20, 25, 30}
Output: 6
Explanation:
The whole set is in AP having common difference = 5.
Therefore, the length is 4.
Input: arr[] = { 20, 1, 15, 3, 10, 5, 8 }
Output: 4
Explanation:
The longest subsequence having the same difference is { 20, 15, 10, 5 }.
The above subsequence has same difference for every consecutive pairs i.e., (15 – 20) = (10 – 15) = (5 – 10) = -5.
Therefore, the length is 4.
朴素方法:解决问题的最简单方法是生成给定数组的所有可能子序列,并打印在相邻元素对之间具有相同差异的最长子序列的长度。时间
复杂度: O(N*2 N )
辅助空间: O(1)
高效方法:上述方法可以使用动态规划进行优化。以下是步骤:
- 初始化一个辅助矩阵dp[][] ,其中dp[i][j]表示从i开始的子序列的长度,其公共差为j 。
- 使用嵌套循环i从N – 1到0和j从i+1到N迭代数组。
- 假设arr[i]和arr[j]是序列的前两个元素,并让它们的差为d 。
- 现在,出现两种情况:
- dp[j][d] = 0 :不存在以arr[j]开头且其差为d 的序列。在这种情况下dp[i][d] = 2 ,因为我们只能有arr[i]和arr[j]在序列中。
- dp[j][d] > 0 : 存在以arr[j]开头且相邻元素之间的差为d的序列。在这种情况下,关系为dp[i][d] = 1 + dp[j] [d] .
- 最后,打印形成的所有子序列的最大长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that finds the longest
// arithmetic subsequence having the
// same absolute difference
int lenghtOfLongestAP(int A[], int n)
{
// Stores the length of sequences
// having same difference
unordered_map >
dp;
// Stores the resultant length
int res = 2;
// Iterate over the array
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int d = A[j] - A[i];
// Update length of subsequence
dp[d][j] = dp[d].count(i)
? dp[d][i] + 1
: 2;
res = max(res, dp[d][j]);
}
}
// Return res
return res;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 20, 1, 15, 3, 10, 5, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << lenghtOfLongestAP(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function that finds the longest
// arithmetic subsequence having the
// same absolute difference
static int lenghtOfLongestAP(int A[], int n)
{
// Stores the length of sequences
// having same difference
Map> dp = new HashMap>();
// Stores the resultant length
int res = 2;
// Iterate over the array
for(int i = 0; i < n; ++i)
{
for(int j = i + 1; j < n; ++j)
{
int d = A[j] - A[i];
Map temp;
// Update length of subsequence
if (dp.containsKey(d))
{
temp = dp.get(d);
if (temp.containsKey(i))
temp.put(j, temp.get(i) + 1);
else
temp.put(j, 2);
}
else
{
temp = new HashMap();
temp.put(j, 2);
}
dp.put(d, temp);
res = Math.max(res, temp.get(j));
}
}
// Return res
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 20, 1, 15, 3, 10, 5, 8 };
int N = arr.length;
// Function Call
System.out.println(lenghtOfLongestAP(arr, N));
}
}
// This code is contributed by jithin
Python3
# Python3 program for the above approach
# Function that finds the longest
# arithmetic subsequence having the
# same absolute difference
def lenghtOfLongestAP(A, n) :
# Stores the length of sequences
# having same difference
dp = {}
# Stores the resultant length
res = 2
# Iterate over the array
for i in range(n) :
for j in range(i + 1, n) :
d = A[j] - A[i]
# Update length of subsequence
if d in dp :
if i in dp[d] :
dp[d][j] = dp[d][i] + 1
else :
dp[d][j] = 2
else :
dp[d] = {}
dp[d][j] = 2
if d in dp :
if j in dp[d] :
res = max(res, dp[d][j])
# Return res
return res
# Given array arr[]
arr = [ 20, 1, 15, 3, 10, 5, 8 ]
N = len(arr)
# Function Call
print(lenghtOfLongestAP(arr, N))
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that finds the longest
// arithmetic subsequence having the
// same absolute difference
static int lenghtOfLongestAP(int []A, int n)
{
// Stores the length of sequences
// having same difference
Dictionary> dp = new Dictionary>();
// Stores the resultant length
int res = 2;
// Iterate over the array
for(int i = 0; i < n; ++i)
{
for(int j = i + 1; j < n; ++j)
{
int d = A[j] - A[i];
// Update length of subsequence
if (dp.ContainsKey(d))
{
if (dp[d].ContainsKey(i))
{
dp[d][j] = dp[d][i] + 1;
}
else
{
dp[d][j] = 2;
}
}
else
{
dp[d] = new Dictionary();
dp[d][j] = 2;
}
res = Math.Max(res, dp[d][j]);
}
}
// Return res
return res;
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
int []arr = { 20, 1, 15, 3, 10, 5, 8 };
int N = arr.Length;
// Function call
Console.Write(lenghtOfLongestAP(arr, N));
}
}
// This code is contributed by rutvik_56
Javascript
4
时间复杂度: O(N 2 )
辅助空间: O(N 2 )
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。