Code puzzle : find out median in a combined array

Problem Statement :

techChallenge

Solution I proposes :

image

public int getMedia(int[] input1, int[] input2) {
    int[] merged = new int[input1.length + input2.length];
    System.arraycopy(input1, 0, merged, 0, input1.length);
    System.arraycopy(input2, 0, merged, input1.length, input2.length);
    Arrays.sort(merged);
    int mergedSize = merged.length;
    int pos = mergedSize / 2;
    return mergedSize % 2 == 0 ? (merged[pos] + merged[pos + 1]) / 2 : merged[pos] / 2;
}

You can put your solution as comment Winking smile

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