here is the link to code
Why it is giving tle error i think code is optimised
@yusuf bro check on this small test case, your code is not correct, optimization is not a problem in this question.
test case -
9
9
8
7
6
5
4
3
2
1
12
dry run your logic
have corrected the code but still showing tle for 4 test cases
@yusuf,
https://ide.codingblocks.com/s/317190 corrected code.
In the code you attached add another condition:
while (j < k) {
if (arr[i] + arr[j] + arr[k] == target) {
System.out.println(arr[i] + ", " + arr[j] + " and " + arr[k]);
j++;
k--;
} else if (arr[i] + arr[j] + arr[k] > target)
k--;
else
j++;
}
Add else j++ as well. And for the sake of simplicity I have used a for loop for i instead of the while loop.
Also, use the inbuilt sort method (it uses Merge Sort) instead of insertion sort. Insertion sort has a time complexity of O(n^2) hence maybe the TLE.