Can you please tell me why my code is failing at 2 Cases ,can’t figure it out
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numCases = sc.nextInt();
int arr[] = new int[numCases];
for (int i = 1; i <= numCases; i++) {
arr[i-1]=sc.nextInt();
}
int target = sc.nextInt();
printTargetPairsUsingTreeMap(arr,target);
sc.close();
}
private static void printTargetPairsUsingTreeMap(int[] arr, int target) {
TreeMap<Integer,Integer> hm = new TreeMap<Integer,Integer>();
for (int i = 0; i < arr.length; i++) {
if(!hm.containsKey(arr[i]))
{
hm.put(arr[i],1);
}
else
{
int val = hm.get(arr[i]);
hm.remove(arr[i]);
hm.put(arr[i],val+1);
}
}
for (int i = 0; i < arr.length; i++) {
if(hm.containsKey(arr[i]))
{
int num1= arr[i];
int num2 = target-num1;
int t1 = hm.get(arr[i]);
if(hm.get(num2) != null)
{
int t2 = hm.get(num2);
int max = t2 > t1 ? t2 : t1;
for(int j=0;j<max;j++)
{
System.out.println(num1+" and "+num2);
}
}
hm.remove(num1);
hm.remove(num2);
}
}
}
}
will go with it 