Given an array of n positive integers. The task is to count the number of Arithmetic Progression subsequence in the array. Note: Empty sequence or single element sequence is Arithmetic Progression. 1 <= arr[i] <= 1000000.
Examples: 
 
Input : arr[] = { 1, 2, 3 }
Output : 8
Arithmetic Progression subsequence from the 
given array are: {}, { 1 }, { 2 }, { 3 }, { 1, 2 },
{ 2, 3 }, { 1, 3 }, { 1, 2, 3 }.
Input : arr[] = { 10, 20, 30, 45 }
Output : 12
Input : arr[] = { 1, 2, 3, 4, 5 }
Output : 23
Since empty sequence and single element sequence is also arithmetic progression, so we initialize the answer with n(number of element in the array) + 1. 
Now, we need to find the arithmetic progression subsequence of length greater than or equal to 2. Let minimum and maximum of the array be minarr and maxarr respectively. Observe, in all the arithmetic progression subsequences, the range of common difference will be from (minarr – maxarr) to (maxarr – minarr). Now, for each common difference, say d, calculate the subsequence of length greater than or equal to 2 using dynamic programming. 
Let dp[i] be the number of subsequence that end with arr[i] and have common difference of d. So, 
 
The number of subsequence of length greater than or equal to 2 with common difference d is sum of dp[i] – 1, 0 <= i = 2 with difference d. To speed up, store the sum of dp[j] with arr[j] + d = arr[i] and j < i.
Below is implementation of above idea :
 
C++
| // C++ program to find number of AP // subsequences in the given array #include<bits/stdc++.h> #define MAX 1000001 usingnamespacestd;  intnumofAP(inta[], intn) {     // initializing the minimum value and     // maximum value of the array.     intminarr = INT_MAX, maxarr = INT_MIN;      // Finding the minimum and maximum     // value of the array.     for(inti = 0; i < n; i++)     {         minarr = min(minarr, a[i]);         maxarr = max(maxarr, a[i]);     }      // dp[i] is going to store count of APs ending     // with arr[i].     // sum[j] is going to store sum of all dp[]'s     // with j as an AP element.     intdp[n], sum[MAX];      // Initialize answer with n + 1 as single elements     // and empty array are also DP.     intans = n + 1;      // Traversing with all common difference.     for(intd=(minarr-maxarr); d<=(maxarr-minarr); d++)     {         memset(sum, 0, sizeofsum);          // Traversing all the element of the array.         for(inti = 0; i < n; i++)         {             // Initialize dp[i] = 1.             dp[i] = 1;              // Adding counts of APs with given differences             // and a[i] is last element.               // We consider all APs where an array element             // is previous element of AP with a particular              // difference             if(a[i] - d >= 1 && a[i] - d <= 1000000)                 dp[i] += sum[a[i] - d];              ans += dp[i] - 1;             sum[a[i]] += dp[i];         }     }      returnans; }  // Driver code intmain() {     intarr[] = { 1, 2, 3 };     intn = sizeof(arr)/sizeof(arr[0]);     cout << numofAP(arr, n) << endl;     return0; }  | 
Java
| // Java program to find number of AP // subsequences in the given array importjava.util.Arrays;  classGFG {          staticfinalintMAX = 1000001;      staticintnumofAP(inta[], intn)     {                  // initializing the minimum value and         // maximum value of the array.         intminarr = +2147483647;         intmaxarr = -2147483648;          // Finding the minimum and maximum         // value of the array.         for(inti = 0; i < n; i++) {             minarr = Math.min(minarr, a[i]);             maxarr = Math.max(maxarr, a[i]);         }          // dp[i] is going to store count of          // APs ending with arr[i].         // sum[j] is going to store sum of          // all dp[]'s with j as an AP element.         intdp[] = newint[n];         intsum[] = newint[MAX];          // Initialize answer with n + 1 as          // single elements and empty array          // are also DP.         intans = n + 1;          // Traversing with all common          // difference.         for(intd = (minarr - maxarr);                  d <= (maxarr - minarr); d++)          {             Arrays.fill(sum, 0);              // Traversing all the element              // of the array.             for(inti = 0; i < n; i++) {                                  // Initialize dp[i] = 1.                 dp[i] = 1;                  // Adding counts of APs with                 // given differences and a[i]                  // is last element.                 // We consider all APs where                  // an array element is previous                  // element of AP with a particular                 // difference                 if(a[i] - d >= 1&&                               a[i] - d <= 1000000)                     dp[i] += sum[a[i] - d];                  ans += dp[i] - 1;                 sum[a[i]] += dp[i];             }         }          returnans;     }          // Driver code     publicstaticvoidmain(String[] args)     {         intarr[] = { 1, 2, 3};         intn = arr.length;                  System.out.println(numofAP(arr, n));     } }  // This code is contributed by Anant Agarwal.  | 
Python3
| # Python program to find number of AP # subsequences in the given array  MAX=1000001 defnumofAP(a, n):      # initializing the minimum value and     # maximum value of the array.     minarr =+2147483647    maxarr =-2147483648     # Finding the minimum and      # maximum value of the array.     fori inrange(n):         minarr =min(minarr, a[i])         maxarr =max(maxarr, a[i])           # dp[i] is going to store count of APs ending     # with arr[i].     # sum[j] is going to store sum of all dp[]'s     # with j as an AP element.     dp =[0fori inrange(n +1)]           # Initialize answer with n + 1 as single      # elements and empty array are also DP.     ans =n +1     # Traversing with all common difference.     ford inrange((minarr -maxarr), (maxarr -minarr) +1):         sum=[0fori inrange(MAX+1)]                  # Traversing all the element of the array.         fori inrange(n):                      # Initialize dp[i] = 1.             dp[i] =1             # Adding counts of APs with given differences             # and a[i] is last element.              # We consider all APs where an array element             # is previous element of AP with a particular              # difference             if(a[i] -d >=1anda[i] -d <=1000000):                 dp[i] +=sum[a[i] -d]              ans +=dp[i] -1            sum[a[i]] +=dp[i]      returnans  # Driver code arr =[ 1, 2, 3] n =len(arr)  print(numofAP(arr, n))  # This code is contributed by Anant Agarwal.  | 
C#
| // C# program to find number of AP // subsequences in the given array usingSystem;  classGFG {          staticintMAX = 1000001;      // Function to find number of AP     // subsequences in the given array     staticintnumofAP(int[]a, intn)     {                  // initializing the minimum value and         // maximum value of the array.         intminarr = +2147483647;         intmaxarr = -2147483648;         inti;                  // Finding the minimum and maximum         // value of the array.         for(i = 0; i < n; i++)          {             minarr = Math.Min(minarr, a[i]);             maxarr = Math.Max(maxarr, a[i]);         }          // dp[i] is going to store count of          // APs ending with arr[i].         // sum[j] is going to store sum of          // all dp[]'s with j as an AP element.         int[]dp = newint[n];         int[]sum = newint[MAX];          // Initialize answer with n + 1 as          // single elements and empty array          // are also DP.         intans = n + 1;          // Traversing with all common          // difference.         for(intd = (minarr - maxarr);                   d <= (maxarr - minarr); d++)          {                          for(i = 0; i < MAX; i++)             sum[i]= 0;                      // Traversing all the element              // of the array.             for( i = 0; i < n; i++)             {                                  // Initialize dp[i] = 1.                 dp[i] = 1;                  // Adding counts of APs with                 // given differences and a[i]                  // is last element.                 // We consider all APs where                  // an array element is previous                  // element of AP with a particular                 // difference                 if(a[i] - d >= 1 &&                      a[i] - d <= 1000000)                     dp[i] += sum[a[i] - d];                  ans += dp[i] - 1;                 sum[a[i]] += dp[i];             }         }          returnans;     }          // Driver code     publicstaticvoidMain()     {         int[]arr = {1, 2, 3};         intn = arr.Length;                  Console.WriteLine(numofAP(arr, n));     } }  // This code is contributed by vt_m.  | 
Javascript
| <script>     // Javascript program to find number of AP     // subsequences in the given array          let MAX = 1000001;        functionnumofAP(a, n)     {                    // initializing the minimum value and         // maximum value of the array.         let minarr = +2147483647;         let maxarr = -2147483648;            // Finding the minimum and maximum         // value of the array.         for(let i = 0; i < n; i++) {             minarr = Math.min(minarr, a[i]);             maxarr = Math.max(maxarr, a[i]);         }            // dp[i] is going to store count of          // APs ending with arr[i].         // sum[j] is going to store sum of          // all dp[]'s with j as an AP element.         let dp = newArray(n);         let sum = newArray(MAX);            // Initialize answer with n + 1 as          // single elements and empty array          // are also DP.         let ans = n + 1;            // Traversing with all common          // difference.         for(let d = (minarr - maxarr);                  d <= (maxarr - minarr); d++)          {             sum.fill(0);                // Traversing all the element              // of the array.             for(let i = 0; i < n; i++) {                                    // Initialize dp[i] = 1.                 dp[i] = 1;                    // Adding counts of APs with                 // given differences and a[i]                  // is last element.                 // We consider all APs where                  // an array element is previous                  // element of AP with a particular                 // difference                 if(a[i] - d >= 1 &&                               a[i] - d <= 1000000)                     dp[i] += sum[a[i] - d];                    ans += dp[i] - 1;                 sum[a[i]] += dp[i];             }         }            returnans;     }          let arr = [ 1, 2, 3 ];     let n = arr.length;      document.write(numofAP(arr, n));      </script> | 
Output : 
 
8
Time complexity: O(n*d) given an array of n positive integers and d is the difference between the maximum and minimum value in the array.
Auxiliary Space: O(MAX)
This article is contributed by
Anuj Chauhan. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    








