Doubt in this question

package arrays;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class targetsumtriplets {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	ArrayList<Integer> list = new ArrayList<Integer>();
	for (int i = 0; i < n; i++) {
		int num = scn.nextInt();
		list.add(num);
	}
	int target = scn.nextInt();
	Collections.sort(list);

	int i = 0;
	int lo = 1;
	int hi = n - 1;

	while (lo < hi) {
		if (list.get(i) + list.get(lo) + list.get(hi) == target) {
			System.out.println(list.get(i)+ list.get(lo) + "and" + list.get(hi));
			lo++;
			hi--;

		}
		if (list.get(i) + list.get(lo) + list.get(hi) > target) {
			hi = hi - 1;

		} else if (list.get(i) + list.get(lo) + list.get(hi) < target) {
			lo = lo + 1;

		}
		i++;

	}

}

}
WHAT IS WRONG IN THIS CODE???

@harsh.hj You are incrementing i every single time! You have to use a nested loop, because you will be checking possibilities i have corrected ya code.

why would we write low=i+1 ???

@harsh.hj bro dry run the code for sample input you will get it! You were almost correct but you were incrementing i, every time. We want triplets so basically we are taking three pointers, i, low , hi!

Try to dry run as much as possible on pen and paper you will learn a lot!