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;
}

Open in new window


i wrote as above and passed all tests. I wonder how can i improve my code. please advise
LVL 7
gudii9Asked:
Who is Participating?
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.

ozoCommented:
return new int[]{a[1],b[1]};
0
gurpsbassiCommented:
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;

    }

Open in new window


Hopefully will work for any size array.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
gudii9Author 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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ozoCommented:
What would be what you want when array size is 6?
0
gurpsbassiCommented:
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.
0
ozoCommented:
maybe
  return new int[]{(a[(a.length-1)/2]+a[a.length/2])/2,(b[(b.length-1)/2]+b[b.length/2])/2};
0
gurpsbassiCommented:
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.
0
gudii9Author 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.

Then how to extend my solution to accomodate it. please advise
0
ozoCommented:
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
0
gurpsbassiCommented:
The solution I posted works for any length
0
gurpsbassiCommented:
Guddi9 can u tell me if the solution I provided works or not? If not tell me the expected outcome versus actual outcome
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.