Find Triplets of Elements in Array Whose Sum is Equal to Required Number

package org.wesome.dsalgo;

import java.util.HashSet;

class TripletPairSum {
    static boolean tripletPairSum(int arr[], int sumRequired) {
        int size = arr.length;
        for (int i = 0; i < size - 2; i++) {
            HashSet<Integer> s = new HashSet<>();
            int currentSum = sumRequired - arr[i];
            for (int j = i + 1; j < size; j++) {
                if (s.contains(currentSum - arr[j])) {
                    System.out.printf("Triplet is " + arr[i] + ", " + arr[j] + " and " + (currentSum - arr[j]) + " equals to " + sumRequired);
                    return true;
                }
                s.add(arr[j]);
            }
        }
        System.out.println("Array doesn't contains any triplet whose sum is equals to " + sumRequired);
        return false;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.TripletPairSum.tripletPairSum;

class TripletPairSumTest {
    @Test
    void PairSumTest1() {
        int arr[] = {-8, 1, 4, 6, 10, 45};
        int sumRequired = 41;
        Assertions.assertTrue(tripletPairSum(arr, sumRequired));
    }

    @Test
    void PairSumTest2() {
        int arr[] = {-5, -1, 2, 3, 4};
        int sumRequired = -4;
        Assertions.assertTrue(tripletPairSum(arr, sumRequired));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

follow us on