Given a sorted array of distinct positive integers, print all triplets that forms Geometric Progression with integral common ratio.
A geometric progression is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54,… is a geometric progression with common ratio 3.
Examples:
Input:
arr = [1, 2, 6, 10, 18, 54]
Output:
2 6 18
6 18 54
Input:
arr = [2, 8, 10, 15, 16, 30, 32, 64]
Output:
2 8 32
8 16 32
16 32 64
Input:
arr = [ 1, 2, 6, 18, 36, 54]
Output:
2 6 18
1 6 36
6 18 54
The idea is to start from the second element and fix every element as middle element and search for the other two elements in a triplet (one smaller and one greater). For an element arr[j] to be middle of geometric progression, there must exist elements arr[i] and arr[k] such that –
arr[j] / arr[i] = r and arr[k] / arr[j] = r
where r is an positive integer and 0 <= i < j and j < k <= n - 1
Below is the implementation of above idea
C++
#include <iostream>
using namespace std;
void findGeometricTriplets( int arr[], int n)
{
for ( int j = 1; j < n - 1; j++)
{
int i = j - 1, k = j + 1;
while (i >= 0 && k <= n - 1)
{
while (arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
cout << arr[i] << " " << arr[j]
<< " " << arr[k] << endl;
k++ , i--;
}
if (arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0)
{
if (arr[j] / arr[i] < arr[k] / arr[j])
i--;
else k++;
}
else if (arr[j] % arr[i] == 0)
k++;
else i--;
}
}
}
int main()
{
int arr[] = {1, 2, 4, 16};
int n = sizeof (arr) / sizeof (arr[0]);
findGeometricTriplets(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findGeometricTriplets( int arr[], int n)
{
for ( int j = 1 ; j < n - 1 ; j++)
{
int i = j - 1 , k = j + 1 ;
while (i >= 0 && k <= n - 1 )
{
while (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
System.out.println(arr[i] + " " + arr[j]
+ " " + arr[k]);
k++ ; i--;
}
if (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 )
{
if (i >= 0 && arr[j] / arr[i] < arr[k] / arr[j])
i--;
else k++;
}
else if (i >= 0 && arr[j] % arr[i] == 0 )
k++;
else i--;
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 16 };
int n = arr.length;
findGeometricTriplets(arr, n);
}
}
|
Python 3
def findGeometricTriplets(arr, n):
for j in range ( 1 , n - 1 ):
i = j - 1
k = j + 1
while (i > = 0 and k < = n - 1 ):
while (arr[j] % arr[i] = = 0 and
arr[k] % arr[j] = = 0 and
arr[j] / / arr[i] = = arr[k] / / arr[j]):
print ( arr[i] , " " , arr[j],
" " , arr[k])
k + = 1
i - = 1
if (arr[j] % arr[i] = = 0 and
arr[k] % arr[j] = = 0 ):
if (arr[j] / / arr[i] < arr[k] / / arr[j]):
i - = 1
else :
k + = 1
elif (arr[j] % arr[i] = = 0 ):
k + = 1
else :
i - = 1
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 16 ]
n = len (arr)
findGeometricTriplets(arr, n)
|
C#
using System;
class GFG
{
static void findGeometricTriplets( int []arr, int n)
{
for ( int j = 1; j < n - 1; j++)
{
int i = j - 1, k = j + 1;
while (i >= 0 && k <= n - 1)
{
while (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
Console.WriteLine(arr[i] + " " +
arr[j] + " " + arr[k]);
k++ ; i--;
}
if (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0)
{
if (i >= 0 && arr[j] / arr[i] <
arr[k] / arr[j])
i--;
else k++;
}
else if (i >= 0 && arr[j] % arr[i] == 0)
k++;
else i--;
}
}
}
static public void Main ()
{
int []arr = {1, 2, 4, 16};
int n = arr.Length;
findGeometricTriplets(arr, n);
}
}
|
Javascript
<script>
function findGeometricTriplets(arr,n)
{
for (let j = 1; j < n - 1; j++)
{
let i = j - 1, k = j + 1;
while (i >= 0 && k <= n - 1)
{
while (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
document.write(arr[i] + " " + arr[j]
+ " " + arr[k]+ "<br>" );
k++ ; i--;
}
if (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0)
{
if (i >= 0 && arr[j] / arr[i] < arr[k] / arr[j])
i--;
else k++;
}
else if (i >= 0 && arr[j] % arr[i] == 0)
k++;
else i--;
}
}
}
let arr = [1, 2, 4, 16];
let n = arr.length;
findGeometricTriplets(arr, n);
</script>
|
Time complexity of above solution is O(n2) as for every j, we are finding i and k in linear time.
Auxiliary Space: O(1), since we not used any extra space.
This article is contributed by Aditya Goel. If you like GeeksforGeeks 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 GeeksforGeeks main page and help other Geeks.