educative.io

Similar approach why not working?

Hello I was trying to solve the problem by using a helper method that instead of returning the count, it modifies it. I would like your help with helping me notice why it does not work? I cannot see where is the problem, Thank you!

  public static int searchTriplets(int[] arr, int target) {
      int left = 0;
      int count = 0;
      Arrays.sort(arr);
      for (int i = 0; i < arr.length; i++) {
        countValidTriplets(count, arr, target - arr[i], i + 1);
      }
      return count;
  }

  private static void countValidTriplets(int count, int[] arr, int target, int left) {
    int right = arr.length - 1;
    while (left < right) {
      int currSum = arr[left] + arr[right];
      System.out.println(currSum);
      if (currSum < target) {
        count += right - left + 1;
        left++;
      } else {
        right--;
      }
    }
  }

Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/5554621957275648