0
public int[] join(int[] arr1,int[] arr2){

    int[] joinArr=new int[arr1.length + arr2.length];
    int j=0,k=0;
    for(int i=0;i<joinArr.length;i++){
        if(j==arr1.length){
            joinArr[i]=arr2[k];
        }
        else if(k==arr2.length){
            joinArr[i]=arr1[j];
        }
        else if(arr1[j]>arr2[k]){
            joinArr[i]=arr1[j];
            j++;
        }
        else{
            joinArr[i]=arr2[k];
        k++;
        }

    }    

    return joinArr;

}

Testcase1 Parameters

{100,90,80,70,60} {105,95,85,75,65}

Testcase1 Actual Answer

{105,100,95,90,85,80,75,70,65,60}

Testcase1 Expected Answer

{105,100,95,90,85,80,75,70,65,60}

Testcase2 Parameters

{100,90,80,70,60} {105}

Testcase2 Actual Answer

{105,100,100,100,100,100}

Testcase2 Expected Answer

{105,100,90,80,70,60}

When I run my Testcase2, it doesn't give me the expected answer , how could I fix this problem?

Pallavi Singh
  • 133
  • 10
  • possible duplicate of [How to merge two sorted arrays into a sorted array?](http://stackoverflow.com/questions/5958169/how-to-merge-two-sorted-arrays-into-a-sorted-array) – Lee Sep 02 '15 at 16:51

2 Answers2

1

You have 4 possible outcomes. In the last two outcomes, you increment the index when you take a value from arr1[j] or arr2[k]

You have to do the same for all outcomes, otherwise you just the value you were up to repeatedly.

BTW I suggest you

  • use the code formatter in your IDE to make your code more readable
  • use the debugger in your IDE to step through the code so you undersand what it is doing.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • @PallaviSingh You increment `j` and `k` after copying the value from that index in two cases, but not the first two cases. You should be doing this consistently, and you should be using your debugger first to debug your code. – Peter Lawrey Sep 02 '15 at 20:53
1

try this way:

public static int[] join(int[] arr1,int[] arr2){
        int[] joinArr=new int[arr1.length + arr2.length];
        int i=0,j=0,k=0;
        while(i<arr1.length && j<arr2.length){  // coping from both the array while one of them is exhausted
            if( arr1[i]>arr2[j]){
                joinArr[k++]=arr1[i++]; // coping from arr1 and update the index i and k.
            }else if(arr1[i]<arr2[j]){
                joinArr[k++]=arr2[j++]; // coping from arr2 and update the index j and k.
            }else{
                joinArr[k++]=arr2[j++]; // coping from any of arr1  or arr2 and update the index i,j and k. 
                i++;
            }


        }  
        if(i<arr1.length){  // coping from  the array arr1 since arr2 is exhausted

             while(i<arr1.length ){
                 joinArr[k++]=arr1[i++];
             }
        }

        if(j<arr2.length){  // coping from  the array arr2 since arr1 is exhausted

             while(j<arr2.length ){
                 joinArr[k++]=arr2[j++];
             }
        }

        return Arrays.copyOf(joinArr, k);

    }
Rustam
  • 6,485
  • 1
  • 25
  • 25