Given an unsorted array of size n and an integer d which is the common difference, the task is to find the length of the longest AP.

For all j, greater than some i(<n),

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

i.e. a[j] is in the AP of a[i] from index i to j.

**Examples:**

Input:n = 6, d = 2

arr[] = {1, 2, 5, 7, 9, 85}Output:4

The longest AP, taking indices into consideration, is [1, 5, 7, 9]

since 5 is 2 indices ahead of 1 and would fit in the AP if started

from index 0. as 5 = 1 + (2 – 0) * 2. So the output is 4.

Input:n = 10, d = 3

arr[] = {1, 4, 2, 5, 20, 11, 56, 100, 20, 23}Output:5

The longest AP, taking indices into consideration, is [2, 5, 11, 20, 23]

since 11 is 2 indices ahead of 5 and 20 is 3 indices ahead of 11. So they

would fit in the AP if started from index 2.

**Naive Approach:** For each element calculate the length of the longest AP it could form and print the maximum among them. It involves **O(n ^{2})** time complexity.

An **efficient** approach is to use Hashing.

Create a map where the key is the starting element of an AP and its value is the number of elements in that AP. The idea is to update the value at key ‘a'(which is at index i and whose starting element is not yet there in the map) by 1 whenever the element at index j(>i) could be in the AP of ‘a'(as the starting element). Then we print the maximum value among all values in the map.

## C++

`// C++ code for finding the` `// maximum length of AP` ` ` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `maxlenAP(` `int` `* a, ` `int` `n, ` `int` `d)` `{` ` ` `// key=starting element of an AP,` ` ` `// value=length of AP` ` ` `unordered_map<` `int` `, ` `int` `> 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<Integer, Integer> m = ` ` ` `new` `HashMap<Integer, Integer>();` ` ` ` ` `// 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<Integer, Integer> 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<` `int` `,` `int` `> m = ` ` ` `new` `Dictionary<` `int` `,` `int` `>();` ` ` ` ` `// 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` |

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**