We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Count Triplets
  2. Discussions

Count Triplets

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • dainjungdev
    3 months ago+ 1 comment

    Very simple Python3 solution!

    from collections import defaultdict
    
    freq = defaultdict(int)
    pair = defaultdict(int)
    
    def countTriplets(arr, r):
        count = 0
        
        for val in arr:
            if val % (r*r) == 0:
                count += pair[val // r]
            if val % r == 0:
                pair[val] += freq[val // r] 
            freq[val] += 1
                
        return count
    
    1|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy