Sarvaratchagan
Sarvaratchagan

Reputation: 95

How can you deal this merge array approach?

Hello Professionals,

I was taking a test online coding challenge in a website. They provide me 2 program. I had done a program and second one is below.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution{

/*
* Complete the function below.
*/

/* Write your custom functions here */
static void mergeArray(int []a, int []b, int M ){

}

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int _a_cnt = 0;
    int[] _a = new int[100001];
    int[] _b = new int[200002];

    try {
       _a_cnt = sc.nextInt();
    }catch (Exception e) {
         System.out.println("Here: " + e.getMessage()); 
    } 

    for( int i = 0;i < _a_cnt;i++ ){            
      _a[i] = sc.nextInt();     
    }

    for( int i = 0;i < _a_cnt;i++ ){
        _b[i] = sc.nextInt();       
    }

    mergeArray(_a ,_b,_a_cnt);

    for( int i = 0;i < 2 * _a_cnt;i++ ){
        System.out.print(_b[i] + " ");      
    }
}
 }

In my understanding we need to write a piece of code for merge two array ( that already defined there like int []a, int []b, int M) and we should return it to main program. Here is my question how can we merge that two array and return it? Is there any technique to handle memory reference ( Like C# out,ref keyword ) in java?

Rule : You should not modify main function.

Here is some output:

Sample Input:

              a = {3,5,6,9,12,14,18,20,25,28}
              b = {30,32,34,36,38,40,42,44,46,48 }

Expected output :

{3,5,6,9,12,14,18,20,25,28,30,32,34,36, 38,40,42,44,46,48}

Upvotes: 0

Views: 188

Answers (4)

Hemant Kumar
Hemant Kumar

Reputation: 47

Answer:

static void mergeArray(int []a, int []b, int M ){
    for( int i = 0;i < M;i++ ){
        b[M+i] = a[i];       
    }
}

Ofcourse didn't check indexes. may be i <= M in for loop

Upvotes: 0

Bohemian
Bohemian

Reputation: 425083

It's fairly obvious by the lengths of the arrays that the task is to merge the first array into the second (since the length of her second array is double that of the first, yet they both must contain the same number of inputted values).

It ain't that complex:

static void mergeArray(int[] a, int[] b, int n) {
    for (int i = n*2 - 1, x = n - 1, y = n - 1; i >= 0; i--)
        b[i] = x >= 0  && a[x] >= b[y] ? a[x--] : b[y--];
}

Tested and works.

Upvotes: 2

Rajesh
Rajesh

Reputation: 2155

Using foreach:

static void mergeArray(int[] a, int[] b, int M) {

    int c[] = new int[a.length + b.length];

    int k = 0;
    for (int i : a)
        c[k++] = i;
    for (int i : b)
        c[k++] = i;     
}

Using System.arraycopy:

static void mergeArrays(int[] a, int[] b) {
    int aLength = a.length;
    int bLength = b.length;

    int[] c = new int[aLength + bLength];

    System.arraycopy(a, 0, c, 0, aLength);
    System.arraycopy(b, 0, c, aLength, bLength);        
}

Upvotes: 1

Thomas
Thomas

Reputation: 508

Hope below code snippet helps :)

static void mergeArray(int []a, int []b, int m ){
    int[] c= new int[100001];
       System.arraycopy(a, 0, c, 0, m);
       System.arraycopy(b, 0, c, m, m*2);
       System.arraycopy(c, 0, b, 0, m*2);
}

Test Results -> Sample Input:

          a = {3,5,6,9,12,14,18,20,25,28}
          b = {30,32,34,36,38,40,42,44,46,48 }

output Printed:

{3,5,6,9,12,14,18,20,25,28,30,32,34,36, 38,40,42,44,46,48}

Upvotes: 0

Related Questions