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
-
Arithmetic Progression is a sequence in which all the differences between consecutive pairs are the same, i.e sequence
P[0]
,P[1]
,P[2]
, …,P[m - 1]
of length m is an Arithmetic Progression if and only ifP[1]
-P[0]
==P[2]
-P[1]
==P[3]
-P[2]
== … ==P[m - 1]
-P[m - 2]
- The common difference can be positive, negative or 0.
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.