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
  • 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 →

  • [deleted] 2 months ago+ 0 comments

    Hello!

    I'm learning Java, this solution was converted from @Jugad's Javascript solution, but I'm failing test cases 6 and 10. Can anyone help?

    // Complete the countTriplets function below.
        static long countTriplets(List<Long> arr, long r) {
            //0. Initializations
            Map<Long, Long> singleCounts = new HashMap<>();
            Map<Long, Long> pairCounts = new HashMap<>();
            long triplets = 0;
            
            //1. Sort array ascending order
            Collections.sort(arr, Collections.reverseOrder());
            
            //2. Iterate through sorted array
            for (Long num: arr) {
                // A. Count completed pairs
                if (pairCounts.get(num*r) != null) {
                    triplets += pairCounts.get(num*r);
                }
                // B. Increment pair counts if needed
                if (singleCounts.get(num*r) != null) {
                    pairCounts.merge(num, singleCounts.get(num*r), Long::sum);
                }
                // C. Increment single counts
                singleCounts.merge(num, Long.valueOf(1), Long::sum);
                
            }
            return triplets;
        }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy