| Topic | Difficulty | Companies |
|---|---|---|
| Dynamic Programming | MEDIUM | Google Microsoft |
Given a set of integers in an array arr[] of size n , write a program to find the length of the longest arithmetic subsequence in arr[].
Problem Note
P[0], P[1], P[2], …, P[m - 1] of length m is an Arithmetic Progression if and only if P[1] - P[0] == P[2] - P[1] == P[3] - P[2] == … == P[m - 1] - P[m - 2]Example 1
Input: arr[] = [2, 6, 10, 14]
Output: 4
Explanation: [2, 6, 10, 14] form an arithmetic progression with common difference 4.
Example 2
Input: arr[] = [2, 4, 8, 2, 12]
Output: 3
Explanation:[4, 8, 12] form an arithmetic progression.