Hi,
this is my first post and I'm doing an exercise on HackerRank called "Count Triplets".
This is the text:
and this is my code: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.
When I compile this solution , the method returns 0 for each test cases.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; }
I don't understand where the error is.
Thanks for help.