[JAVA] clean, short and simple solution using a HashSet (helper code with Array instead of List)


#1

There is no need of a HashMap here. It is totally redundant since the index of any 2 elements will always be different. This fact will never change if u use a Map or not. So comparing 2 elements means comparing 2 separate indices only where i is automatically not equal to j.

public int diffPossible(int[]a,int k)
{
	Set<Integer> st=new HashSet<Integer>();
	for(int i=0;i<a.length;i++)
	{
		int b=a[i];
		if(!st.isEmpty())
		{
			if(st.contains(k+b) || st.contains(b-k))
			{
				return 1;
			}
			st.add(b);
		}
		else {
			st.add(b);
		}
	}
	return 0;
}