Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Count Triplets Hackerrank

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Count Triplets Hackerrank

    Hi,
    this is my first post and I'm doing an exercise on HackerRank called "Count Triplets".
    This is the text:
    You are given an array and you need to find number of triplets of indices (i, j, k) such that the elements at those indices are in geometric progression for a given common ratio r and i < j < k.
    and this is my code:
    public class Solution {
     
        // Complete the countTriplets function below.
        static long countTriplets(List<Long> arr, long r) {
     
            // Long[] a = arr.toArray();
            int counter = 0;
     
            for (int i = 0; i < arr.size() - 2; i++) {
                for (int j = i + 1; j < arr.size() - 1; j++) {
                    if (arr.get(j) - arr.get(i) == r) {
                        for (int k = j + 1; k < arr.size(); k++) {
                            if (arr.get(k) - arr.get(j) == r) {
                                System.out.println("(" + arr.get(i) + "," + arr.get(j) + "," + arr.get(k) + ")");
                                counter++;
                                break;
                            }
     
                        }
                    }
                }
            }
            return counter;
        }
    When I compile this solution , the method returns 0 for each test cases.
    I don't understand where the error is.
    Thanks for help.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Count Triplets Hackerrank

    Can you post a complete program with test data that can be compiled and executed for testing?
    Also post the correct output for the test data.

    How are you trying to debug the code to see where it is going wrong?

    The code needs some comments to explain its logic.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how can count
    By noura in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 6th, 2017, 09:43 AM
  2. Replies: 10
    Last Post: July 1st, 2014, 02:41 PM
  3. The count for each value in array
    By samar in forum Loops & Control Statements
    Replies: 4
    Last Post: January 4th, 2013, 02:38 PM
  4. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 01:31 AM
  5. select count(*)
    By jacinto in forum JDBC & Databases
    Replies: 4
    Last Post: March 2nd, 2010, 10:30 PM