# midway challege

Hi,

i am trying below challenge

http://codingbat.com/prob/p146449

i wrote as below
``````public int[] middleWay(int[] a, int[] b) {
int arrayC[]=new int[2];
arrayC[0]=a[1];

arrayC[1]=b[1];
return arrayC;
}
``````

i wrote as above and passed all tests. I wonder how can i improve my code. please advise
LVL 7
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
return new int[]{a[1],b[1]};
Commented:
Try this:
``````public static int[] getMiddleNumbers(final int[] a, final int[] b){
final int[] middleWay = new int[2];

middleWay[0] = a[a.length / 2];
middleWay[1] = b[b.length / 2];

return middleWay;

}
``````

Hopefully will work for any size array.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Hopefully will work for any size array.

i wonder how works for any size other than 3. Let us say array size is 6 then 6/2=3 so
middleWay[0] = a[3];
middleWay[1] = b[3];
which may not be what we want in this challenge? please advise
Commented:
What would be what you want when array size is 6?
Commented:
Depends what behaviour you desire. a[3] will be the 4th element in the array leaving 2 on the right hand side and 3 on the left hand side. There's no way you can split it evenly.
Commented:
maybe
return new int[]{(a[(a.length-1)/2]+a[a.length/2])/2,(b[(b.length-1)/2]+b[b.length/2])/2};
Commented:
maybe
return new int[]{(a[(a.length-1)/2]+a[a.length/2])/2,(b[(b.length-1)/2]+b[b.length/2])/2};

Still same issue. I think @gudii9 is asking how to handle arrays that are not equally splittable in half.
In short, you can't.
Author Commented:

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

say if above challenge modified to below

Given 2 int arrays, a and b, each length 3 or 9 or any odd number(i guess other than 1!!), return a new array length 2 containing their middle elements.

Commented:
return new int[]{a[a.length/2],b[b.length/2]};
works for any odd number

return new int[]{(a[(a.length-1)/2]+a[a.length/2])/2,(b[(b.length-1)/2]+b[b.length/2])/2};
also works for any odd number and extends the solution in a symmetric way for even numbers
Commented:
The solution I posted works for any length
Commented:
Guddi9 can u tell me if the solution I provided works or not? If not tell me the expected outcome versus actual outcome
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.