Created
November 3, 2019 18:23
-
-
Save imedadel/6e8382bcea810fb5797b26149d531b95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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