0

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}
Vishwas
  • 6,967
  • 5
  • 42
  • 69
Sarvaratchagan
  • 95
  • 1
  • 10
  • 2
    You'll need to create a new Array with the combined lengths and then place the values in there accordingly. On that note, what sort of monster starts all their variable names in Java with an underscore? – Dragondraikk Jun 02 '15 at 07:43
  • Could you please show me some samples and i know Java doesn't have something like C#'s ref and out keywords for passing by reference. How can i achieve it? – Sarvaratchagan Jun 02 '15 at 07:47
  • arrays are objects, and your variable is a reference. So...passing by reference is inevitable. – zubergu Jun 02 '15 at 07:50
  • @zubergu- So you are trying to convey this is possible ah? – Sarvaratchagan Jun 02 '15 at 08:03
  • What he is saying is that, essentially, it is always pass by reference. The reason for this is that the variable for your object is actually merely a reference to that object; A copy of that reference still points to the same object. This is a good read to explain it: http://stackoverflow.com/a/40499/583447 – Erik S Jun 02 '15 at 08:30
  • @Sarrrva - mergeArray(int []a, int []b, int M ) - what's significance of M – Rajesh Jun 02 '15 at 08:52
  • @Rajesh - That's actual length of array. – Sarvaratchagan Jun 02 '15 at 09:39
  • "merge" usually means to join all elements of both arrays into one array whose elements are in order. ie `[1,3,5]` merged with `[2,4,6]` would produce `[1,2,3,4,5,6]`. Is that the task? Please let me know. If so, your test data is badly chosen, because a "merge" can achieved by simply concatenating the arrays due to all elements of b being larger than a. – Bohemian Jun 02 '15 at 18:19

4 Answers4

2

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

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);        
}
Rajesh
  • 2,135
  • 1
  • 12
  • 14
  • I have already mentioned there. you should not modify main. Please read whole things carefully! – Sarvaratchagan Jun 02 '15 at 08:18
  • How does this modify main? It merely modifies the `mergeArray` method, that is *supposed* to be changed according to your code. – Erik S Jun 02 '15 at 08:27
  • 1
    This is what Sarrrva asked with question..."Here is my question how can we merge that two array and return it?" :) – Rajesh Jun 02 '15 at 08:32
  • @Erik Dolor & Rajesh : Ah! yep indeed this answer does not going to modify main function. if you are going to add this solution with above program you should modify main function. isn't it? – Sarvaratchagan Jun 02 '15 at 08:32
0

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}
Thomas
  • 498
  • 4
  • 16
0

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