Count Triplets

  • + 66 comments

    Couple of hints:

    • Can be done in O(n) -> single pass through data
    • No division necessary and single multiplications by R are all that's needed
    • Using map(C++) or dict(Java, Python) is a must -> can be unordered map (saves O(logN))
    • Try to think forward when reading a value -> will this value form part of a triplet later?
    • No need to consider (R == 1) as a corner case

    Very interesting problem and it took a while to think it through properly. If anyone is desperate for the code, drop me a message.