What i tried is in the below code.. I want to write the better way which gives the less time complexity. The below code complexity i am seeing is o(n^3).. I am looking for the sum exactly equal to target sum.. I know there is a question which is similar question but i am looking for better approach. Please suggest better approach do it using data structures
public class SumofThreeNumbers {
private String findTriplet(int[] inputArray, int targetSum) {
for (int i = 0; i < inputArray.length; i++) {
for (int j = i + 1; j < inputArray.length; j++) {
for (int k = j + 1; k < inputArray.length; k++) {
if (inputArray[i] + inputArray[j] + inputArray[k] == targetSum) {
return "FIRST VALUE IS===" + inputArray[i] + "\nSECOND VALUE IS===" + inputArray[j]
+ "\nTHIRD VALUE IS===" + inputArray[k];
}
}
}
}
return "Not FOUND";
}
public static void main(String[] args) {
SumofThreeNumbers sumOfThreeObj = new SumofThreeNumbers();
int[] inputArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
String result = sumOfThreeObj.findTriplet(inputArray, 20);
System.out.println(result);
}
}