# mergeTwo challenge

Hi,
i am working one below challenge
http://codingbat.com/prob/p139150

i was not clear on below description

Start with two arrays of strings, A and B, each with its elements in alphabetical order and without duplicates. Return a new array containing the first N elements from the two arrays. The result array should be in alphabetical order and without duplicates. A and B will both have a length which is N or more. The best "linear" solution makes a single pass over A and B, taking advantage of the fact that they are in alphabetical order, copying elements directly to the new array.

mergeTwo(["a", "c", "z"], ["b", "f", "z"], 3) → ["a", "b", "c"]//i expected output  ["a", "c", "z"."b", "f", "z]??
mergeTwo(["a", "c", "z"], ["c", "f", "z"], 3) → ["a", "c", "f"]
mergeTwo(["f", "g", "z"], ["c", "f", "g"], 3) → ["c", "f", "g"]
LVL 7
###### Who is Participating?

x

Software engineerCommented:
@ozo, it's probably correct working code, but way too 'obscure' for gudii9 I'm afraid.
@gudii9, here's a better human readable version:

``````public String[] mergeTwo(String[] a, String[] b, int n) {
String[] c= new String[n];
int aIndex=0, bIndex=0, cIndex=0;
for( cIndex=0; cIndex<n; cIndex++) {
int compareResult = a[aIndex].compareTo(b[bIndex]); // compare an element of array a with one of array b
if (compareResult == 0) { // The elements are equal
c[cIndex] = a[aIndex]; // add it to the result array c
aIndex++; // increment the "working index" of both arrays
bIndex++;
} else if (compareResult < 0) { // The element of array a comes alphabetically before the one in array b
c[cIndex] = a[aIndex]; // add the smallest to the result array c
aIndex++; // increment the working index of array a
} else if (compareResult > 0) {  // The element of array b comes alphabetically before the one in array a
c[cIndex] = b[bIndex]; // add the smallest to the result array c
bIndex++; // increment the working index of array b
}
}
return c;  // return the result array
}
``````
0

PresidentCommented:
You are missing this note: containing the first N elements from the two arrays.  Note that there is a third parameter to the function call, which is 3 in each case.  Therefore, you will only be returning an array of size 3, and NOT the culmination of both arrays.  This is the first reason why your "expected output" is wrong -- there should only be 3 elements returned.

The second reason your "expected output" is wrong is because you are not returning the array in alphabetical order, which was also clearly indicated in the specs.
0

Author Commented:
You are missing this note: containing the first N elements from the two arrays.  Note that there is a third parameter to the function call, which is 3 in each case.  Therefore, you will only be returning an array of size 3, and NOT the culmination of both arrays.  This is the first reason why your "expected output" is wrong -- there should only be 3 elements returned.

The second reason your "expected output" is wrong is because you are not returning the array in alphabetical order, which was also clearly indicated in the specs.

i got meaning now. let me think psuedo code and code?
0

Commented:
public String[] mergeTwo(String[] a, String[] b, int n) {
String[] c= new String[n];
int ax=0; int bx=0; int cx=0;
for( cx=0;cx<n;){
int d=a[ax].compareTo(b[bx]);
c[cx++] = d<0?a[ax++]:d>0?b[bx++]:a[bx++*0+ax++];
}
return c;
}
0

Author Commented:
this is quite a bit of code to digest
0

Author Commented:
any other simple approach?
0

Software engineerCommented:
any other simple approach?
You can't tell a story in one sentence.
0

Author Commented:
``````public String[] mergeTwo(String[] a, String[] b, int n) {
String[] c= new String[n];
int aIndex=0, bIndex=0, cIndex=0;
for( cIndex=0; cIndex<n; cIndex++) {
int compareResult = a[aIndex].compareTo(b[bIndex]); // compare an element of array a with one of array b
if (compareResult == 0) { // The elements are equal
c[cIndex] = b[bIndex]; // add it to the result array c
aIndex++; // increment the "working index" of both arrays
bIndex++;
} else if (compareResult < 0) { // The element of array a comes alphabetically before the one in array b
c[cIndex] = a[aIndex]; // add the smallest to the result array c
aIndex++; // increment the working index of array a
} else if (compareResult > 0) {  // The element of array b comes alphabetically before the one in array a
c[cIndex] = b[bIndex]; // add the smallest to the result array c
bIndex++; // increment the working index of array b
}
}
return c;  // return the result array
}
``````

if both equal i can say as below right which passes all tests

c[cIndex] = b[bIndex]; // add it to the result array c

c[cIndex] = a[aIndex]; // add it to the result array c
0

Author Commented:
``````public class Test {

public static void main(String args[]) {
String str1 = "Strings are immutable";
String str2 = "Strings are immutable";
String str3 = "Integers are not immutable";

int result = str1.compareTo( str2 );
System.out.println(result);

result = str2.compareTo( str3 );
System.out.println(result);

result = str3.compareTo( str1 );
System.out.println(result);
}
}
This produces the following result:

0
10
-10
``````
we are basically looping to fill array C by giving n as it size and going less than n

for( cIndex=0; cIndex<n; cIndex++) {

then we are checking below

int compareResult = a[aIndex].compareTo(b[bIndex]); // compare an element of array a with one of array b
if (compareResult == 0) { // The elements are equal
fill c with either a or b element as both same increment a and b

else if   if (compareResult < 0) {
fill C with 'a' element as 'a' is less than 'b' and increment a as that is already filled

else  if (compareResult > 0) {
fill C with b element as 'a' is greater than 'b' and increment b as that is already filled
0

Author Commented:
``````public String[] mergeTwo(String[] a, String[] b, int n) {

String[] c=new String[n];
int aIndex=0,bIndex=0,cIndex=0;
int compareVal=a[aIndex].compareTo(b[bIndex]);
for(cIndex=0;cIndex<n;cIndex++){

if(compareVal==0){
c[cIndex]=a[aIndex];
aIndex++;
bIndex++;
}

else if(compareVal<0){
// c(cIndex[i])=a(aIndex[i]);
c[cIndex]=a[aIndex];
aIndex++;
//bIndex++;
}

else if(compareVal>0){
//c(cIndex[i])=b(bIndex[i]);
//aIndex++;
c[cIndex]=b[bIndex];
bIndex++;
}
}
return c;
}
``````

when i tried as above failing below tests
Expected      Run
mergeTwo(["a", "c", "z"], ["b", "f", "z"], 3) → ["a", "b", "c"]      ["a", "c", "z"]      X
mergeTwo(["a", "c", "z"], ["c", "f", "z"], 3) → ["a", "c", "f"]      ["a", "c", "z"]      X
mergeTwo(["f", "g", "z"], ["c", "f", "g"], 3) → ["c", "f", "g"]      ["c", "f", "g"]      OK
mergeTwo(["a", "c", "z"], ["a", "c", "z"], 3) → ["a", "c", "z"]      ["a", "c", "z"]      OK
mergeTwo(["a", "b", "c", "z"], ["a", "c", "z"], 3) → ["a", "b", "c"]      ["a", "b", "c"]      OK
mergeTwo(["a", "c", "z"], ["a", "b", "c", "z"], 3) → ["a", "b", "c"]      ["a", "c", "z"]      X
mergeTwo(["a", "c", "z"], ["a", "c", "z"], 2) → ["a", "c"]      ["a", "c"]      OK
mergeTwo(["a", "c", "z"], ["a", "c", "y", "z"], 3) → ["a", "c", "y"]      ["a", "c", "z"]      X
mergeTwo(["x", "y", "z"], ["a", "b", "z"], 3) → ["a", "b", "x"]      ["a", "b", "z"]      X
other tests
X
0

Author Commented:
``````package com.solution;

import java.util.Arrays;

public class MergeTwo {

public static void main(String[] args) {
// TODO Auto-generated method stub
String[] a={"a", "c", "z"};
String[] b={"b", "f", "z"};
System.out.println("is--->"+Arrays.toString(mergeTwo(a,b,3)));

}

public static String[] mergeTwo(String[] a, String[] b, int n) {

String[] c=new String[n];
int aIndex=0,bIndex=0,cIndex=0;
int compareVal=a[aIndex].compareTo(b[bIndex]);
for(cIndex=0;cIndex<n;cIndex++){

if(compareVal==0){
c[cIndex]=a[aIndex];
aIndex++;
bIndex++;
}

else if(compareVal<0){
// c(cIndex[i])=a(aIndex[i]);
c[cIndex]=a[aIndex];
aIndex++;
//bIndex++;
}

else if(compareVal>0){
//c(cIndex[i])=b(bIndex[i]);
//aIndex++;
c[cIndex]=b[bIndex];
bIndex++;
}
}
return c;
}

}
``````

above give wrong output as below

is--->[a, c, z]
0

Author Commented:
i see i made a mistake. i have to open beyondcompare to compare working and not working code. compareVal(compareResult) i put outside for loop which is resulting -1 all the time. i fixed and passing all tests now
``````public String[] mergeTwo(String[] a, String[] b, int n) {

String[] c=new String[n];
int aIndex=0,bIndex=0,cIndex=0;

for(cIndex=0;cIndex<n;cIndex++){
int compareVal=a[aIndex].compareTo(b[bIndex]);
if(compareVal==0){
c[cIndex]=a[aIndex];
aIndex++;
bIndex++;
}

else if(compareVal<0){
// c(cIndex[i])=a(aIndex[i]);
c[cIndex]=a[aIndex];
aIndex++;
//bIndex++;
}

else if(compareVal>0){
//c(cIndex[i])=b(bIndex[i]);
//aIndex++;
c[cIndex]=b[bIndex];
bIndex++;
}
}
return c;
}
``````
0

Author Commented:
``````package com.solution;

import java.util.Arrays;

public class MergeTwo {

public static void main(String[] args) {
// TODO Auto-generated method stub
String[] a={"a", "c", "z"};
String[] b={"b", "f", "z"};
System.out.println("is--->"+Arrays.toString(mergeTwo(a,b,3)));

}

public static String[] mergeTwo(String[] a, String[] b, int n) {

String[] c=new String[n];
int aIndex=0,bIndex=0,cIndex=0;
int compareVal=a[aIndex].compareTo(b[bIndex]);//c.compareTo(b)===>i thought 1 but coming -1

for(cIndex=0;cIndex<n;cIndex++){
System.out.println("compareVal is====>"+compareVal);
if(compareVal==0){
c[cIndex]=a[aIndex];
aIndex++;
bIndex++;
}

else if(compareVal<0){
// c(cIndex[i])=a(aIndex[i]);
c[cIndex]=a[aIndex];
aIndex++;
//bIndex++;
}

else if(compareVal>0){
//c(cIndex[i])=b(bIndex[i]);
//aIndex++;
c[cIndex]=b[bIndex];
bIndex++;
}
}
return c;
}

}
``````

above is misplaced code resulting same -1

compareVal is====>-1
compareVal is====>-1
compareVal is====>-1
is--->[a, c, z]

as index values not updating keep on printing a only
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.