My input is one integer suppose M and the program must print all the combination of two integer x and y where x + y = M.
Let us take our input as M = 50 and array element as 25 20 25 30 15 45 45 5
and my required output is
5 45,20 30,25 25.
But my output is coming as
5 45,5 45,20 30,25,25
How to remove the occurrence of that two times 5 45?
My code is as follows
Scanner s = new Scanner(System.in);
int m = s.nextInt();
s.nextLine();
String str = s.nextLine();
StringTokenizer st = new StringTokenizer(str);
int len = st.countTokens();
int[] a = new int[len];
String[] temp = new String[len];
for (int i = 0; i < len; i++)
{
temp[i] = st.nextToken();
a[i] = Integer.parseInt(temp[i]);
}
Arrays.sort(a);
for (int i = 0;i < len-1; i++)
{
for(int j = i + 1; j < len; j++)
{
if ((a[i] +a [j]) == m)
System.out.println(a[i] + " " + a[j]);
}
}