Solved

# mergeTwo  challenge

Posted on 2016-08-30
92 Views
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.

how below results are 40 each? please advise
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"]
0
Question by:gudii9

LVL 28

Expert Comment

ID: 41778340
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

LVL 7

Author Comment

ID: 41778905
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

LVL 84

Assisted Solution

ozo earned 250 total points
ID: 41779279
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

LVL 37

Accepted Solution

zzynx earned 250 total points
ID: 41781201
@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

LVL 7

Author Comment

ID: 41781695
this is quite a bit of code to digest
0

LVL 7

Author Comment

ID: 41788967
any other simple approach?
0

LVL 37

Expert Comment

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

LVL 7

Author Comment

ID: 41791606
``````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

LVL 7

Author Comment

ID: 41791628
``````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

LVL 7

Author Comment

ID: 41791667
``````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

LVL 7

Author Comment

ID: 41791730
``````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

LVL 7

Author Comment

ID: 41791789
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

LVL 7

Author Comment

ID: 41791791
``````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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 18
null output 3 25
jsp login check 12 32
Is Applet the way to go for my drag and drop system? 8 14
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

#### 777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.