removing similar element from the array from sum of any two integer

Viewed 102

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]);
    }
}
5 Answers

use break for skipping repeated number(inner loop) and check (a[i] - a[i+1]) == 0 and use continue to skip repeated number in outer loop.

include only the following changes in the for loop, works perfectly

// Init a new array.
for(int i=0;i<len-1;i++)
{        
    if((a[i] - a[i+1]) == 0)
       continue;         // for skipping repeated number in outer loop 
    for(int j=i+1;j<len;j++)
    {
        if((a[i]+a[j])==m ) {
            System.out.println(a[i]+" "+a[j]);
            break;             // for skipping repeated number in inner loop
        }
      else if(a[i] == m/2){
        System.out.println(a[i]+" "+a[i]);
        break;
      }
    } 

}

input

25 ,20, 25 ,30, 15, 45, 45, 5

output

5 45
20 30
25 25

input

5 ,5 ,45, 45, 45

output

5 45

When just printing the correct pairs, you cannot check for the former solutions. You can add a[i] and a[j] whose sum is M to an array. After that, if a new pair summary gives M, you can check the array if elements in it.

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);

// Init a new array.
for(int i=0;i<len-1;i++)
{
    for(int j=i+1;j<len;j++)
    {
        if((a[i]+a[j])==m) {
            // if(new array contains a[i]) continue;
            //array.add(a[i]);
            //array.add(a[j]);
            System.out.println(a[i]+" "+a[j]);
        }
    }
}

you can try to store in a list what you printed and not repeat a print if already printed.

    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);
    List<String> output = new ArrayList<>();
    for(int i=0;i<len-1;i++)
    {
        for(int j=i+1;j<len;j++)
        {
            if((a[i]+a[j])==m) {
                String msg = a[i]+" "+a[j];
                if(!output.contains(msg)) {
                    System.out.println(msg);
                    output.add(msg);
                }
            }
        }
    }
    s.close();

I wouldn't try to reinvent the wheel. Just put every pair in a java.util.Set so you only have distinct pairs:

Add this right before your last nested loop:

Set<String> pairs = new HashSet<>();

And change System.out.println(a[i] + " " + a[j]); to pairs.add(a[i] + " " + a[j]);

And in the end you can print the contents of the Set.

Try it online.

Related