Problem sum triplet 2

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int arr[] = new int[n];
for(int i =0;i<n;i++){
arr[i] = cin.nextInt();
}
int t = cin.nextInt();
for(int count = 0; count<arr.length-1; count++)
{
for(int j = 0;j<arr.length-count-1;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

	 for(int i = 0;i<arr.length-2;i++){
	      int  l = i+1, h = arr.length-1;
	       while(l<h){
		int sum = arr[i]+arr[l]+arr[h];
		if( sum == t){
			System.out.println(i+", "+l+" and "+h);
			break;
		}else if(sum<t){
		    l++;
		}else if(sum>t){
		    h--;
		}
	    }
	 }
	
}

}====what’s the error in this approach

now this is new approach i have problem with plus i have not solved the problem till now

@AbhishekAhlawat1102
for ur code when u find 1 pair corresponding to the first element,u move to the next element. but there can be more than 1 triplets with every first element

so what should i do instead of break should i us l++ 0r h–

after printing out your found triplet move l to next pointer and h to previous instead of break.
so replace break with
l++;
h–;

@AbhishekAhlawat1102 please mark your doubt as resolved if you are satisfied with the answer :slight_smile: