Was getting TLE for my function in which i only used 1 loop and output was also the same, but when I used two loops it worked fine how ? I am pasting the function which I earlier used

void target_sum_triplet(int arr[], int n, int key)
{
int i = 0 ;
int j = n -1 ; //Sample array is : 1 2 3 4 5 6 7 8 9
int k = i + 1;

while (k < j)
{
	int sum = arr[i] + arr[k] + arr[j] ;
	if(sum == key)
	{
		cout<<arr[i]<<", "<<arr[k]<<" and "<<arr[j]<<endl;
		k++ ;
		j-- ; // new change
	}
	else if(sum > key)
	{
		j-- ;
	}
	if(k > j)
	{
		i++ ;
		k = i + 1 ;
		j = n -1 ;
	}
}

}

hi @kartikey.rajput0071,
u need to print unique triplet
refer https://ide.codingblocks.com/s/656672 ive commented the code