Find all triplets in a sorted array that forms Geometric Progression
Last Updated :
20 Mar, 2023
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++
// C++ program to find if there exist three elements in
// Geometric Progression or not
#include <iostream>
using namespace std;
// The function prints three elements in GP if exists
// Assumption: arr[0..n-1] is sorted.
void findGeometricTriplets(int arr[], int n)
{
// One by fix every element as middle element
for (int j = 1; j < n - 1; j++)
{
// Initialize i and k for the current j
int i = j - 1, k = j + 1;
// Find all i and k such that (i, j, k)
// forms a triplet of GP
while (i >= 0 && k <= n - 1)
{
// if arr[j]/arr[i] = r and arr[k]/arr[j] = r
// and r is an integer (i, j, k) forms Geometric
// Progression
while (arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
// print the triplet
cout << arr[i] << " " << arr[j]
<< " " << arr[k] << endl;
// Since the array is sorted and elements
// are distinct.
k++ , i--;
}
// if arr[j] is multiple of arr[i] and arr[k] is
// multiple of arr[j], then arr[j] / arr[i] !=
// arr[k] / arr[j]. We compare their values to
// move to next k or previous 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] is multiple of arr[i], then
// try next k. Else, try previous i.
else if (arr[j] % arr[i] == 0)
k++;
else i--;
}
}
}
// Driver code
int main()
{
// int arr[] = {1, 2, 6, 10, 18, 54};
// int arr[] = {2, 8, 10, 15, 16, 30, 32, 64};
// int arr[] = {1, 2, 6, 18, 36, 54};
int arr[] = {1, 2, 4, 16};
// int arr[] = {1, 2, 3, 6, 18, 22};
int n = sizeof(arr) / sizeof(arr[0]);
findGeometricTriplets(arr, n);
return 0;
}
Java
// Java program to find if there exist three elements in
// Geometric Progression or not
import java.util.*;
class GFG
{
// The function prints three elements in GP if exists
// Assumption: arr[0..n-1] is sorted.
static void findGeometricTriplets(int arr[], int n)
{
// One by fix every element as middle element
for (int j = 1; j < n - 1; j++)
{
// Initialize i and k for the current j
int i = j - 1, k = j + 1;
// Find all i and k such that (i, j, k)
// forms a triplet of GP
while (i >= 0 && k <= n - 1)
{
// if arr[j]/arr[i] = r and arr[k]/arr[j] = r
// and r is an integer (i, j, k) forms Geometric
// Progression
while (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
// print the triplet
System.out.println(arr[i] +" " + arr[j]
+ " " + arr[k]);
// Since the array is sorted and elements
// are distinct.
k++ ; i--;
}
// if arr[j] is multiple of arr[i] and arr[k] is
// multiple of arr[j], then arr[j] / arr[i] !=
// arr[k] / arr[j]. We compare their values to
// move to next k or previous 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 arr[j] is multiple of arr[i], then
// try next k. Else, try previous i.
else if (i >= 0 && arr[j] % arr[i] == 0)
k++;
else i--;
}
}
}
// Driver code
public static void main(String[] args)
{
// int arr[] = {1, 2, 6, 10, 18, 54};
// int arr[] = {2, 8, 10, 15, 16, 30, 32, 64};
// int arr[] = {1, 2, 6, 18, 36, 54};
int arr[] = {1, 2, 4, 16};
// int arr[] = {1, 2, 3, 6, 18, 22};
int n = arr.length;
findGeometricTriplets(arr, n);
}
}
// This code is contributed by Rajput-Ji
Python 3
# Python 3 program to find if
# there exist three elements in
# Geometric Progression or not
# The function prints three elements
# in GP if exists.
# Assumption: arr[0..n-1] is sorted.
def findGeometricTriplets(arr, n):
# One by fix every element
# as middle element
for j in range(1, n - 1):
# Initialize i and k for
# the current j
i = j - 1
k = j + 1
# Find all i and k such that
# (i, j, k) forms a triplet of GP
while (i >= 0 and k <= n - 1):
# if arr[j]/arr[i] = r and
# arr[k]/arr[j] = r and r
# is an integer (i, j, k) forms
# Geometric Progression
while (arr[j] % arr[i] == 0 and
arr[k] % arr[j] == 0 and
arr[j] // arr[i] == arr[k] // arr[j]):
# print the triplet
print( arr[i] , " " , arr[j],
" " , arr[k])
# Since the array is sorted and
# elements are distinct.
k += 1
i -= 1
# if arr[j] is multiple of arr[i]
# and arr[k] is multiple of arr[j],
# then arr[j] / arr[i] != arr[k] / arr[j].
# We compare their values to
# move to next k or previous i.
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
# else if arr[j] is multiple of
# arr[i], then try next k. Else,
# try previous i.
elif (arr[j] % arr[i] == 0):
k += 1
else:
i -= 1
# Driver code
if __name__ =="__main__":
arr = [1, 2, 4, 16]
n = len(arr)
findGeometricTriplets(arr, n)
# This code is contributed
# by ChitraNayal
C#
// C# program to find if there exist three elements
// in Geometric Progression or not
using System;
class GFG
{
// The function prints three elements in GP if exists
// Assumption: arr[0..n-1] is sorted.
static void findGeometricTriplets(int []arr, int n)
{
// One by fix every element as middle element
for (int j = 1; j < n - 1; j++)
{
// Initialize i and k for the current j
int i = j - 1, k = j + 1;
// Find all i and k such that (i, j, k)
// forms a triplet of GP
while (i >= 0 && k <= n - 1)
{
// if arr[j]/arr[i] = r and arr[k]/arr[j] = r
// and r is an integer (i, j, k) forms Geometric
// Progression
while (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
// print the triplet
Console.WriteLine(arr[i] +" " +
arr[j] + " " + arr[k]);
// Since the array is sorted and elements
// are distinct.
k++ ; i--;
}
// if arr[j] is multiple of arr[i] and arr[k] is
// multiple of arr[j], then arr[j] / arr[i] !=
// arr[k] / arr[j]. We compare their values to
// move to next k or previous 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 arr[j] is multiple of arr[i], then
// try next k. Else, try previous i.
else if (i >= 0 && arr[j] % arr[i] == 0)
k++;
else i--;
}
}
}
// Driver code
static public void Main ()
{
// int arr[] = {1, 2, 6, 10, 18, 54};
// int arr[] = {2, 8, 10, 15, 16, 30, 32, 64};
// int arr[] = {1, 2, 6, 18, 36, 54};
int []arr = {1, 2, 4, 16};
// int arr[] = {1, 2, 3, 6, 18, 22};
int n = arr.Length;
findGeometricTriplets(arr, n);
}
}
// This code is contributed by ajit.
JavaScript
<script>
// Javascript program to find if there exist three elements in
// Geometric Progression or not
// The function prints three elements in GP if exists
// Assumption: arr[0..n-1] is sorted.
function findGeometricTriplets(arr,n)
{
// One by fix every element as middle element
for (let j = 1; j < n - 1; j++)
{
// Initialize i and k for the current j
let i = j - 1, k = j + 1;
// Find all i and k such that (i, j, k)
// forms a triplet of GP
while (i >= 0 && k <= n - 1)
{
// if arr[j]/arr[i] = r and arr[k]/arr[j] = r
// and r is an integer (i, j, k) forms Geometric
// Progression
while (i >= 0 && arr[j] % arr[i] == 0 &&
arr[k] % arr[j] == 0 &&
arr[j] / arr[i] == arr[k] / arr[j])
{
// print the triplet
document.write(arr[i] +" " + arr[j]
+ " " + arr[k]+"<br>");
// Since the array is sorted and elements
// are distinct.
k++ ; i--;
}
// if arr[j] is multiple of arr[i] and arr[k] is
// multiple of arr[j], then arr[j] / arr[i] !=
// arr[k] / arr[j]. We compare their values to
// move to next k or previous 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 arr[j] is multiple of arr[i], then
// try next k. Else, try previous i.
else if (i >= 0 && arr[j] % arr[i] == 0)
k++;
else i--;
}
}
}
// Driver code
// int arr[] = {1, 2, 6, 10, 18, 54};
// int arr[] = {2, 8, 10, 15, 16, 30, 32, 64};
// int arr[] = {1, 2, 6, 18, 36, 54};
let arr = [1, 2, 4, 16];
// int arr[] = {1, 2, 3, 6, 18, 22};
let n = arr.length;
findGeometricTriplets(arr, n);
// This code is contributed by avanitrachhadiya2155
</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.
Similar Reads
Print all triplets in sorted array that form AP
Given a sorted array of distinct positive integers, print all triplets that form AP (or Arithmetic Progression) Examples : Input : arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 }; Output : 6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42 Input : arr[] = { 3, 5, 6, 7, 8, 10, 12}; Out
12 min read
Javascript Program for Print all triplets in sorted array that form AP
Given a sorted array of distinct positive integers, print all triplets that form AP (or Arithmetic Progression)Examples : Input : arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 };Output :6 9 122 12 2212 17 222 17 3212 22 329 22 352 22 4222 32 42Input : arr[] = { 3, 5, 6, 7, 8, 10, 12};Output :3 5 75
3 min read
Removing a number from array to make it Geometric Progression
Given an array arr[] of N positive elements, the task is to find whether it is possible to convert this array into Geometric Progression (GP) by removing at-most one element. If yes, then find index of the number removing which converts the array into a geometric progression. Special Cases : 1) If w
11 min read
Count subarrays of atleast size 3 forming a Geometric Progression (GP)
Given an array arr[] of N integers, the task is to find the count of all subarrays from the given array of at least size 3 forming a Geometric Progression. Examples: Input: arr[] = {1, 2, 4, 8}Output: 3Explanation: The required subarrays forming geometric progression are: {1, 2, 4}{2, 4, 8}{1, 2, 4,
6 min read
Largest lexicographic triplet from a given Array that forms a triangle
Given an array arr[], the task is to find the lexicographically largest triplet in that array that can form a triangle. If no such triplet exists, print -1.Example: Input: arr[] = { 4, 2, 10, 3, 5 } Output: 3 4 5 Explanation: The lexicographically largest triplet is (4, 5, 10). But it does not form
5 min read
Find bitwise XOR of all triplets formed from given three Arrays
Given three arrays arr1[], arr2[], and arr3[] consisting of non-negative integers. The task is to find the bitwise XOR of the XOR of all possible triplets that are formed by taking one element from each array. Examples: Input: arr1[] = {2, 3, 1}, arr2[] = {2, 4}, arr3[] = {3, 5}Output: 0Explanation:
11 min read
Find all triplets that sum to a given value or less
Given an array, arr[] and integer X. Find all the possible triplets from an arr[] whose sum is either equal to less than X. Example: Input : arr[] = {-1, 1, 3, 2}, X = 3Output: (-1, 1, 3), (-1, 1, 2)Explanation: If checked manually, the above two are the only triplets from possible 4 triplets whose
7 min read
Count triplets having product 0 from a given array
Given an array arr[] of size N, the task is to count the number of triplets (arr[i], arr[j], arr[k]) such that arr[i] * arr[j] = arr[j] * arr[k] = 0 (i < j < k). Examples: Input: arr[] = {0, 8, 12, 0} Output: 2 Explanation: Triplets satisfying the given conditions are (0, 8, 0) and (0, 12, 0).
8 min read
Count of triplets in an array that satisfy the given conditions
Given an array arr[] of N elements, the task is to find the count of triplets (arr[i], arr[j], arr[k]) such that (arr[i] + arr[j] + arr[k] = L) and (L % arr[i] = L % arr[j] = L % arr[k] = 0.Examples: Input: arr[] = {2, 4, 5, 6, 7} Output: 1 Only possible triplet is {2, 4, 6}Input: arr[] = {4, 4, 4,
13 min read
Find the Array which when sorted forms an AP and has minimum maximum
Given three positive integers N, X, and Y(X<Y). The task is to find an array of length N containing both X and Y, and when sorted in increasing order, the array must form an arithmetic progression Examples: Input: N = 5, X = 20, Y = 50Output: 20 30 40 50 10 Explanation: The array when sorted in i
12 min read