Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created November 3, 2019 18:23
Show Gist options
  • Select an option

  • Save imedadel/6e8382bcea810fb5797b26149d531b95 to your computer and use it in GitHub Desktop.

Select an option

Save imedadel/6e8382bcea810fb5797b26149d531b95 to your computer and use it in GitHub Desktop.
from collections import Counter
# Imagine this as three boxes or stages, each number "graduates" from the first stage to the second one, then, to the final stage.
# Instead of having a final stage object, having an integer that's incremented each time a number reaches the final stage, is more performant
def countTriplets(arr, r):
finalStage = 0
firstStage = Counter()
secondStage = Counter()
for i in reversed(arr):
finalStage += secondStage[i*r]
secondStage[i] += firstStage[i*r]
firstStage[i] += 1
return finalStage
n, r = [int(x) for x in input().strip().split()]
numbers = [int(x) for x in input().strip().split()]
print(countTriplets(numbers, r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment