Java > Array-1 > middleWay (CodingBat Solution)

Problem:

Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.

middleWay({1, 2, 3}, {4, 5, 6}) → {2, 5}
middleWay({7, 7, 7}, {3, 8, 0}) → {7, 8}
middleWay({5, 2, 9}, {1, 4, 5}) → {2, 4}


Solution:

public int[] middleWay(int[] a, int[] b) {
  int[] myArray = new int[] {a[1], b[1]};
  return myArray;
}


6 comments :

  1. public int[] middleWay(int[] a, int[] b)
    {
    int c[] = new int[2];
    c[0] = a[1];
    c[1] = b[1];
    return c;

    }

    ReplyDelete
  2. public int[] middleWay(int[] a, int[] b) {
    return new int[] {a[1], b[1]};
    }

    ReplyDelete
  3. public int[] middleWay(int[] a, int[] b) {
    int midA= a.length/2;
    int midB =b.length/2;
    return new int[] {a[midA],b[midB]};
    }

    ReplyDelete
  4. public int[] middleWay(int[] a, int[] b) {

    int [] arr = new int [] {a[a.length-2], b[b.length-2]};

    return arr;
    }

    ReplyDelete
  5. public int[] middleWay(int[] a, int[] b) {
    return (new int[] {a[1],b[1]});
    }

    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