Count Triplets

  • + 32 comments

    Thanks, I cleared all the test cases! Takeaways from my solution:

    • I used two maps, one to keep count and one to track the number of potential triplets.
    • Keeping track of the latter makes updating the result trivial.
    • Only a single pass of the array was needed.
    • No edge case condition when R is 1.

    I highly recommend to anyone not quite understanding, think through the case

    5 2
    1 2 1 2 4
    

    The expected answer is 3. I was getting 4 with my initial approach. Good luck!