Home > Uncategorized > Sorting of Pairwise sum

Sorting of Pairwise sum

In this problem, we have two list X & Y. we have to take any element x of X and y of Y to make a pair. The problem is to sort the sum of the pair in the ascending order.

if X = {100,110,130} Y = {50,75,125} the sorted pair sum would be

150, 160,175,180,185,205,225.

The code below does this :

class pairwisesum {
public static void main(String [] s) {

int [] X = {100,110,130};
int [] Y = {50,75,125};
int [] index = {0,0,0};
int sum ,next;
for ( int k=0; k<X.length*Y.length;  k++) {
sum = 99999;
next = 0;

for (int i=0; i<X.length; i++) {
//System.out.println(index[i]);
if ( X[i] + Y[index[i]] < sum ){
sum = X[i] + Y[index[i]];
next = i;
}

}
index[next] ++;
System.out.println( “” + sum + “::” + next );
}

}

}

 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: