# Number of GP (Geometric Progression) subsequences of size 3

Given n elements and a ratio r, find the number of G.P. subsequences with length 3. A subsequence is considered GP with length 3 with ration r.
Examples:

```Input : arr[] = {1, 1, 2, 2, 4}
r = 2
Output : 4
Explanation: Any of the two 1s can be chosen
as the first element, the second element can
be any of the two 2s, and the third element
of the subsequence must be equal to 4.

Input : arr[] = {1, 1, 2, 2, 4}
r = 3
Output : 0
```

A naive approach is to use three nested for loops and check for every subsequence with length 3 and keep a count of the subsequences. The complexity is O(n3).

An efficient approach is to solve the problem for fixed middle element of progression. This means that if we fix element a[i] as middle, then it must be multiple of r, and a[i]/r and a[i]*r must be present. We count number of occurrences of a[i]/r and a[i]*r and then multiply the counts. To do this, we can use concept of hashing where we store the count of all possible elements in two hash maps, one indicating the number of elements in the left and the other indicating the number of elements to the right.

Below is the implementation of the above approach

## C++

 `// C++ program to count GP subsequences of size 3. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns count of G.P. subseqeunces ` `// with length 3 and common ratio r ` `long` `long` `subsequences(``int` `a[], ``int` `n, ``int` `r) ` `{ ` `    ``// hashing to maintain left and right array ` `    ``// elements to the main count ` `    ``unordered_map<``int``, ``int``> left, right; ` ` `  `    ``// stores the answer ` `    ``long` `long` `ans = 0; ` ` `  `    ``// traverse through the elements ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``right[a[i]]++; ``// keep the count in the hash ` ` `  `    ``// traverse through all elements ` `    ``// and find out the number of elements as k1*k2 ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// keep the count of left and right elements ` `        ``// left is a[i]/r and right a[i]*r ` `        ``long` `long` `c1 = 0, c2 = 0; ` ` `  `        ``// if the current element is divisible by k, ` `        ``// count elements in left hash. ` `        ``if` `(a[i] % r == 0) ` `            ``c1 = left[a[i] / r]; ` ` `  `        ``// decrease the count in right hash ` `        ``right[a[i]]--; ` ` `  `        ``// number of right elements  ` `        ``c2 = right[a[i] * r]; ` ` `  `        ``// calculate the answer ` `        ``ans += c1 * c2; ` ` `  `        ``left[a[i]]++; ``// left count of a[i] ` `    ``} ` ` `  `    ``// returns answer ` `    ``return` `ans; ` `} ` ` `  `// driver program  ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 6, 2, 3, 6, 9, 18, 3, 9 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` `    ``int` `r = 3; ` `    ``cout << subsequences(a, n, r); ` `    ``return` `0; ` `} `

## Java

 `// Java program to count GP subsequences ` `// of size 3.  ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `class` `GFG{ ` ` `  `// Returns count of G.P. subseqeunces  ` `// with length 3 and common ratio r  ` `static` `long` `subsequences(``int` `a[], ``int` `n, ``int` `r)  ` `{  ` `     `  `    ``// Hashing to maintain left and right array  ` `    ``// elements to the main count  ` `    ``Map left = ``new` `HashMap<>(), ` `                         ``right = ``new` `HashMap<>();  ` ` `  `    ``// Stores the answer  ` `    ``long` `ans = ``0``;  ` ` `  `    ``// Traverse through the elements  ` `    ``for``(``int` `i = ``0``; i < n; i++)  ` `     `  `        ``// Keep the count in the hash  ` `        ``right.put(a[i], ` `        ``right.getOrDefault(a[i], ``0``) + ``1``);  ` ` `  `    ``// Traverse through all elements  ` `    ``// and find out the number of  ` `    ``// elements as k1*k2  ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{  ` `         `  `        ``// Keep the count of left and right  ` `        ``// elements left is a[i]/r and  ` `        ``// right a[i]*r  ` `        ``long` `c1 = ``0``, c2 = ``0``;  ` ` `  `        ``// If the current element is divisible  ` `        ``// by k, count elements in left hash.  ` `        ``if` `(a[i] % r == ``0``)  ` `            ``c1 = left.getOrDefault(a[i] / r, ``0``);  ` ` `  `        ``// Decrease the count in right hash  ` `        ``right.put(a[i], ` `        ``right.getOrDefault(a[i], ``0``) - ``1``);  ` ` `  `        ``// Number of right elements  ` `        ``c2 = right.getOrDefault(a[i] * r, ``0``);  ` ` `  `        ``// Calculate the answer  ` `        ``ans += c1 * c2;  ` `         `  `        ``// left count of a[i]  ` `        ``left.put(a[i], ` `        ``left.getOrDefault(a[i], ``0``) + ``1``);  ` `    ``}  ` `     `  `    ``// Returns answer  ` `    ``return` `ans;  ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `a[] = { ``1``, ``2``, ``6``, ``2``, ``3``,  ` `                ``6``, ``9``, ``18``, ``3``, ``9` `};  ` `    ``int` `n = a.length;  ` `    ``int` `r = ``3``;  ` `     `  `    ``System.out.println(subsequences(a, n, r));  ` `} ` `} ` ` `  `// This code is contributed by offbeat`

## Python3

 `# Python3 program to count GP subsequences  ` `# of size 3.  ` `from` `collections ``import` `defaultdict ` ` `  `# Returns count of G.P. subseqeunces  ` `# with length 3 and common ratio r  ` `def` `subsequences(a, n, r):  ` ` `  `    ``# hashing to maintain left and right ` `    ``# array elements to the main count  ` `    ``left ``=` `defaultdict(``lambda``:``0``) ` `    ``right ``=` `defaultdict(``lambda``:``0``) ` ` `  `    ``# stores the answer  ` `    ``ans ``=` `0` ` `  `    ``# traverse through the elements  ` `    ``for` `i ``in` `range``(``0``, n):  ` `        ``right[a[i]] ``+``=` `1` `# keep the count in the hash  ` ` `  `    ``# traverse through all elements and  ` `    ``# find out the number of elements as k1*k2  ` `    ``for` `i ``in` `range``(``0``, n):  ` ` `  `        ``# keep the count of left and right elements  ` `        ``# left is a[i]/r and right a[i]*r  ` `        ``c1, c2 ``=` `0``, ``0` ` `  `        ``# if the current element is divisible  ` `        ``# by k, count elements in left hash.  ` `        ``if` `a[i] ``%` `r ``=``=` `0``:  ` `            ``c1 ``=` `left[a[i] ``/``/` `r]  ` ` `  `        ``# decrease the count in right hash  ` `        ``right[a[i]] ``-``=` `1` ` `  `        ``# number of right elements  ` `        ``c2 ``=` `right[a[i] ``*` `r]  ` ` `  `        ``# calculate the answer  ` `        ``ans ``+``=` `c1 ``*` `c2  ` ` `  `        ``left[a[i]] ``+``=` `1` `# left count of a[i]  ` ` `  `    ``return` `ans  ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``:  ` ` `  `    ``a ``=` `[``1``, ``2``, ``6``, ``2``, ``3``, ``6``, ``9``, ``18``, ``3``, ``9``]  ` `    ``n ``=` `len``(a)  ` `    ``r ``=` `3` `    ``print``(subsequences(a, n, r))  ` ` `  `# This code is contributed by  ` `# Rituraj Jain `

Output:

```6
```

Time complexity: O(n)

The above solution does not handle the case when r is 1 : For example, for input = {1,1,1,1,1}, there are 10 possible for G.P. subsequences of length 3, which can be calculated by using 5C3. Such a procedure should be implemented for all cases where r = 1. Below is the modified code to handle this.

## C++

 `// C++ program to count GP subsequences of size 3. ` `#include ` `using` `namespace` `std; ` ` `  `// to calculate nCr ` `// DP approach ` `int` `binomialCoeff(``int` `n, ``int` `k) { ` `  ``int` `C[k + 1]; ` `  ``memset``(C, 0, ``sizeof``(C)); ` `  ``C = 1; ``// nC0 is 1 ` `  ``for` `(``int` `i = 1; i <= n; i++) { ` ` `  `    ``// Compute next row of pascal triangle using ` `    ``// the previous row ` `    ``for` `(``int` `j = min(i, k); j > 0; j--) ` `      ``C[j] = C[j] + C[j - 1]; ` `  ``} ` `  ``return` `C[k]; ` `} ` ` `  `// Returns count of G.P. subseqeunces ` `// with length 3 and common ratio r ` `long` `long` `subsequences(``int` `a[], ``int` `n, ``int` `r) ` `{ ` `    ``// hashing to maintain left and right array ` `    ``// elements to the main count ` `    ``unordered_map<``int``, ``int``> left, right; ` ` `  `    ``// stores the answer ` `    ``long` `long` `ans = 0; ` ` `  `    ``// traverse through the elements ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``right[a[i]]++; ``// keep the count in the hash ` ` `  `    ``// IF RATIO IS ONE ` `    ``if` `(r == 1){ ` ` `  `        ``// traverse the count in hash ` `        ``for` `(``auto` `i : right) { ` ` `  `             ``// calculating nC3, where 'n' is ` `             ``// the number of times each number is ` `             ``// repeated in the input ` `             ``ans += binomialCoeff(i.second, 3); ` `        ``} ` ` `  `        ``return` `ans; ` `    ``} ` ` `  `    ``// traverse through all elements ` `    ``// and find out the number of elements as k1*k2 ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// keep the count of left and right elements ` `        ``// left is a[i]/r and right a[i]*r ` `        ``long` `long` `c1 = 0, c2 = 0; ` ` `  `        ``// if the current element is divisible by k, ` `        ``// count elements in left hash. ` `        ``if` `(a[i] % r == 0) ` `            ``c1 = left[a[i] / r]; ` ` `  `        ``// decrease the count in right hash ` `        ``right[a[i]]--; ` ` `  `        ``// number of right elements  ` `        ``c2 = right[a[i] * r]; ` ` `  `        ``// calculate the answer ` `        ``ans += c1 * c2; ` ` `  `        ``left[a[i]]++; ``// left count of a[i] ` `    ``} ` ` `  `    ``// returns answer ` `    ``return` `ans; ` `} ` ` `  `// driver program  ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 6, 2, 3, 6, 9, 18, 3, 9 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` `    ``int` `r = 3; ` `    ``cout << subsequences(a, n, r); ` `    ``return` `0; ` `} `

Output:

```6
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : rituraj_jain, redleg, offbeat

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.