Count Triplets

Sort by

recency

|

820 Discussions

|

  • + 0 comments

    I was trying to do it efficiently, just counting, but the index per se is important, also a lot of edge cases when r=1. Really fun.

  • + 0 comments

    This problem explanation is clear and helpful for understanding triplets in geometric progression, and it’s interesting how learning resources like Recipe Lookbook also make complex topics easier to follow in a simple and engaging way.

  • + 0 comments

    JS

    function countTriplets(array, ratio) {
        const numbers = new Map();
        const couplets = new Map();
        let count = 0;
        array.forEach(number => {
            count += couplets.get(number / ratio) ?? 0;
            if (numbers.has(number / ratio)) {
                couplets.set(number, (couplets.get(number) ?? 0) + numbers.get(number / ratio));
            }
            numbers.set(number, (numbers.get(number) ?? 0) + 1);
        });
        return count;
    }
    
  • + 0 comments

    First we import defaultdictionary from collections module. Then, use this code for better Understanding: def countTriplets(arr, r): total_pairs = 0 count2={} count3={} for num in arr: if num in count3: total_pairs+=count3[num] if num in count2: count3[num*r]=count3.get(num*r, 0) + count2[num] count2[num*r]=count2.get(num*r, 0) + 1 return total_pairs

  • + 0 comments

    Easy to understand Python code

    from collections import Counter, defaultdict
    
    def countTriplets(arr, r):
        left = defaultdict(int)
        right = Counter(arr)
        
        res = 0
        for a in arr:
            right[a] -= 1
            cl = left[a/r]
            cr = right[a * r]
            res += cl * cr
            left[a] += 1
            
        return res