# Count of AP (Arithmetic Progression) Subsequences in an array in C++

Given an array arr[] containing integer elements. The goal is to count the number of Arithmetic Progression subsequences inside arr[]. The range of elements inside arr[] are [1,1000000].

Empty sequence or single element will also be counted.

## Let us understand with examples.

For Example

Input - arr[] = {1,2,3}

Output - Count of AP (Arithmetic Progression) Subsequences in an array are: 8

Explanation - The following subsequences will form AP:-

{}, {1}, {2}, {3}, {1,2}, {2,3}, {1,3}, {1,2,3}

Input - arr[] = {2,4,5,8}

Output - Count of AP (Arithmetic Progression) Subsequences in an array are: 12

Explanation - The following subsequences will form AP:-

{}, {2}, {4}, {5}, {8}, {2,4}, {2,5}, {2,8}, {4,5}, {4,8}, {5,8}, {2,5,8}

## Approach used in the below program is as follows

• An empty sequence is also an AP.
• A single value is also an AP.
• Find minimum and maximum values within the array as max_val and min_val. The common differences in all AP subsequences will be between [ min_val - max_val , max_val - min_val ]
• Now for each common difference we will find subsequences using dynamic programming and store in arr_2[size].
• No subsequences of length 2 or more than 2 will be the sum of arr_2[i]-1 where i is [0,2] and difference is D.
• arr_2[i] = 1+ sum ( arr[j] ) where i<j and arr_2[j] + D= arr_2[i]
• For faster approach add sums ( arr_2[j] with arr[j]+D = arr[i] and j<i ) in  arr_3[max_size]
• Take integer array arr[] as input.
• Function AP_subsequence(int arr[], int size) takes an input array and returns the count of AP (Arithmetic Progression) Subsequences in an array.
• Take the initial count as 0.
• Take variables  max_val, min_val, arr_2[size] for storing subsequences count and arr_3[max_size] for storing sums.
• Traverse arr[] using for loop and find maximum and minimum element and store in max_val and min_val.
• Take count = size +1 for single element APs and empty AP.
• Calculate maximum difference diff_max = max_val - min_val and diff_min = min_val - max_val as common differences possible.
• Traverse using for loop from j=0 to j<size.
• Set arr_2[j] = 1;
• For arr[j] - i >= 1  and arr[j] - i <= 1000000 set   arr_2[j] += arr_3[arr[j] - i].
• Update sum as  arr_3[arr[j]] = arr_3[arr[j]] + arr_2[j].
• At the end return count as result.

## Example

Live Demo

#include<bits/stdc++.h>

using namespace std;
#define max_size 10000

int AP_subsequence(int arr[], int size) {
int count = 0;
int max_val = INT_MAX;
int min_val = INT_MIN;
int arr_2[size];
int arr_3[max_size];

for (int i = 0; i < size; i++) {
max_val = min(max_val, arr[i]);
min_val = max(min_val, arr[i]);
}
count = size + 1;
int diff_max = max_val - min_val;
int diff_min = min_val - max_val;
for (int i = diff_max; i <= diff_min; i++) {
memset(arr_3, 0, sizeof arr_3);
for (int j = 0; j < size; j++) {
arr_2[j] = 1;
if (arr[j] - i >= 1) {
if (arr[j] - i <= 1000000) {
arr_2[j] += arr_3[arr[j] - i];
}
}
count += arr_2[j] - 1;
arr_3[arr[j]] = arr_3[arr[j]] + arr_2[j];
}
}
return count;
}
int main() {
int arr[] = {1,1,6,7,8};
int size = sizeof(arr) / sizeof(arr);
cout << "Count of AP (Arithmetic Progression) Subsequences in an array are: " << AP_subsequence(arr, size);
return 0;
}

If we run the above code it will generate the following output −

## Output

Count of AP (Arithmetic Progression) Subsequences in an array are: 17

Updated on: 29-Jan-2021

325 Views 