Java > Array-1 > make2 (CodingBat Solution)

Problem:

Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.

make2({4, 5}, {1, 2, 3}) → {4, 5}
make2({4}, {1, 2, 3}) → {4, 1}
make2({}, {1, 2}) → {1, 2}


Solution:

public int[] make2(int[] a, int[] b) {
  int[] myArray = new int[2];
  int aLen = a.length;
  int bLen = b.length;
  
  if (aLen == 0) {
    myArray[0] = b[0];
    myArray[1] = b[1];
    return myArray;
  } else if (aLen == 1 && bLen == 1) {
    myArray[0] = a[0];
    myArray[1] = b[0];
    return myArray;
  } else if (aLen == 1) {
      myArray[0] = a[0];
      myArray[1] = b[0];
      return myArray;  
  } else {
      myArray[0] = a[0];
      myArray[1] = a[1];
      return myArray;
  }
}


18 comments :

  1. public int[] make2(int[] a, int[] b) {

    if (a.length == 1) return new int[]{ a[0], b[0] };
    if (a.length == 0) return new int[]{ b[0], b[1] };

    return new int[]{ a[0], a[1] };

    }

    ReplyDelete
    Replies
    1. public int[] make2(int[] a, int[] b) {
      int lena=a.length;
      int lenb=b.length;
      int index=0;
      int[] arr=new int[lena+lenb];

      for(int i=0;i<lena;i++)

      { arr[index]= a[i];
      index++;

      }
      for(int i=0;i<lenb;i++)

      { arr[index]= b[i];
      index++;
      }

      int[] int2=new int[2];
      int2[0]=arr[0];
      int2[1]=arr[1];

      return int2;

      }

      Delete
  2. public int[] make2(int[] a, int[] b) {
    int[]ab = new int [a.length + b.length];
    for(int i = 0 ; i < a.length ; i++ ){
    ab[i] = a[i];
    }
    for( int i=0; i<b.length; i++){
    ab[a.length + i] = b[i];
    }
    int[]m = {ab[0], ab[1]};
    return m;
    }

    ReplyDelete
  3. public int[] make2(int[] a, int[] b) {
    int sum = a.length + b.length;
    int[] concat = new int[sum];
    for(int x = 0; x<a.length; x++)
    {
    concat[x] = a[x];
    }
    for(int x = 0; x<b.length; x++)
    {
    concat[a.length + x] = b[x];
    }

    int[] two = new int[2];
    two[0] = concat[0];
    two[1] = concat[1];

    return two;
    }







    public int[] make2(int[] a, int[] b) {
    int sum = a.length + b.length;
    int[] concat = new int[sum];
    for(int x = 0; x<a.length; x++)
    {
    concat[x] = a[x];
    }
    for(int x = 0; x<b.length; x++)
    {
    concat[a.length + x] = b[x];
    }

    int[] two = new int[2];
    two[0] = concat[0];
    two[1] = concat[1];

    return two;
    }

    ReplyDelete
  4. public int[] make2(int[] a, int[] b) {
    int[] my_arr = new int[2];
    if (a.length >= 2) {
    my_arr[0] = a[0];
    my_arr[1] = a[1];
    }
    if (a.length ==1){
    my_arr[0] = a[0];
    my_arr[1] = b[0];
    }
    if (a.length == 0) {
    my_arr[0] = b[0];
    my_arr[1] = b[1];
    }
    return my_arr;
    }

    ReplyDelete
  5. //here is how i did it (more easy)
    public int[] make2(int[] a, int[] b) {
    int [] c = new int[2];
    int x = 0;
    int i = 0;
    for(i = 0; i < c.length; i++){
    if(a.length > i){
    c[i] = a[i];
    }
    else if(b.length > x){
    c[i] = b[x];
    x++;
    }
    }
    return c;
    }

    ReplyDelete
  6. public int[] make2(int[] a, int[] b) {

    //initialize array
    int newArray[] = new int[2];

    //'a' array is empty
    if (a.length == 0){
    newArray[0] = b[0];
    newArray[1] = b[1];
    }
    //'a' array has one element
    else if (a.length == 1){
    newArray[0] = a[0];
    newArray[1] = b[0];
    }
    //'a' array has enough elements to fill new array
    else if (a.length >= 2){
    newArray[0] = a[0];
    newArray[1] = a[1];
    }
    //returning new array
    return newArray;
    }

    ReplyDelete
  7. Of course it's easier with for-loops, but the Array-1 exercises should be solved without any loops....
    Start reading guys!!

    ReplyDelete
  8. public int[] make2(int[] a, int[] b) {
    int[] c = new int[2];
    for (int i = 0; i < a.length; i++) {
    if (i < 2) {
    c[i] = a[i]; }
    }
    for (int i = a.length; i < 2; i++) {
    c[i] = b[i - a.length];
    }
    return c;
    }

    ReplyDelete
  9. public int[] make2(int[] a, int[] b) {
    int[]myArr = new int[2];
    if(a.length==1){
    myArr[0]=a[0];
    myArr[1]=b[0];
    return myArr;
    }else if(a.length==0){
    myArr[0]=b[0];
    myArr[1]=b[1];
    return myArr;
    }
    return new int[]{a[0],a[1]};
    }

    ReplyDelete
  10. public int[] make2(int[] a, int[] b) {
    if(a.length >= 2){
    return new int[] {a[0], a[1]};
    }if(a.length == 1){
    return new int [] {a[0], b[0]};
    }else if(a.length == 0){
    return new int [] {b[0], b[1]};
    }
    return new int[] {1};
    }

    ReplyDelete
  11. public int[] make2(int[] a, int[] b) {
    int arr[]=new int[2];
    if(a.length==0 && b.length>1){
    arr[0]=b[0];
    arr[1]=b[1];
    return arr;
    }
    if(a.length>2 && b.length>=0){
    arr[0]=a[0];
    arr[1]=a[1];
    return arr;
    }
    if((a.length==1 && b.length==0)||(a.length==1 && b.length==1)){
    arr[0]=a[0];
    arr[1]=b[0];
    return arr;
    }
    if(a.length==1 && b.length>1){
    arr[0]=a[0];
    arr[1]=b[0];
    return arr;
    }
    else return a;

    }

    ReplyDelete
  12. public int[] make2(int[] a, int[] b) {
    int arr[]=new int[2];
    if(a.length==0){
    arr[0]=b[0];
    arr[1]=b[1];
    }
    if(b.length==0){
    arr[0]=a[0];
    arr[1]=a[1];
    }
    if(a.length==1){
    arr[0]=a[0];
    arr[1]=b[0];
    }
    if(b.length==1){
    arr[0]=a[0];
    arr[1]=b[0];
    }
    if(a.length>1){
    arr[0]=a[0];
    arr[1]=a[1];
    }
    return arr;
    }

    ReplyDelete
  13. public int[] make2(int[] a, int[] b) {
    if(a.length==0) return b;
    if(a.length==1)
    return new int[]{a[0],b[0]};
    else if(a.length>1)
    return new int[]{a[0],a[1]};
    return a;
    }

    ReplyDelete
  14. if(a.length==0) return b;
    if(a.length==1) return new int[] {a[0],b[0]};
    return new int[] {a[0],a[1]};

    ReplyDelete
  15. public int[] make2(int[] a, int[] b) {

    if(a.length==0)
    {
    int t[]={b[0],b[1]};
    return t;
    }
    else if(a.length==1)
    {
    int t[]={a[0],b[0]};
    return t;
    }
    else if(a.length==2)
    {
    int t[]={a[0],a[1]};
    return t;
    }
    int t[]={a[0],a[1]};
    return t;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact