Let's say the given common ratio is 3 and the given list is:
For the given list, the possible geometric progression of size 3 with an index such that A*1 < A*2 < A*3 are:
( (index , number) , (index , number) , (index , number) )
So, the answer is 5.
1) mainl
and r
variables contains the list and the common ratio.
2) left
and right
dictionary are initialized to keeps track of how many times each number occurs to the left and right of each index in mainl
.
3) While iterating through mainl,
we initiate each element in left
and right
. Values in left
elements are initially 0 and right
elements are total number of occurrence of each element in mainl
.
4) Count variable keep track of number of geometric progressions found.
5) While iterating through mainl
again , for each element num
:
high
and low
with 0.right[num]
as the element cannot occur to the right of itself.high
and low
with right[num*r]
and left[num/r]
as GP can be written as a/n , a , a*n.high*low
to count
as high*low is equal to number of geometric progression with that index as middle of the 3 element.left[num]
as the current element will occur to left of every next element.6) We print count
.
RELATED TAGS
CONTRIBUTOR
View all Courses