# midThree challenge

Hi,

I am working n below challenge

http://codingbat.com/prob/p155713

``````public int[] midThree(int[] nums) {
int len=nums.length;
int[] newNums=new int[len];

newNums[(len/2)-1]=nums[(len/2)-1];
newNums[(len/2)]=nums[(len/2)];
newNums[(len/2)+1]=nums[(len/2)+1];
return newNums;

}
``````

i failed few tests

Expected      Run
midThree({1, 2, 3, 4, 5}) → {2, 3, 4}      {0, 2, 3, 4, 0}      X
midThree({8, 6, 7, 5, 3, 0, 9}) → {7, 5, 3}      {0, 0, 7, 5, 3, 0, 0}      X
midThree({1, 2, 3}) → {1, 2, 3}      {1, 2, 3}      OK

how to fix and imprve my code and alternate ways of doing it. 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:
int m=nums.length/2;
return new int[]{nums[m-1],nums[m],nums[m+1]};
Commented:
or
``````public int[] midThree(int[] nums) {
int len=nums.length;
int[] newNums=new int[3];

newNums[(1)-1]=nums[(len/2)-1];
newNums[(1)]=nums[(len/2)];
newNums[(1)+1]=nums[(len/2)+1];
return newNums;

}
``````
Commented:
``````public int[] midThree(int[] nums) {
int minIndex = (nums.length / 2) - 1;
return Arrays.copyOfRange(nums, minIndex, minIndex + 3);
}
``````
Author Commented:

newNums[(1)-1]=nums[(len/2)-1];
newNums[(1)]=nums[(len/2)];
newNums[(1)+1]=nums[(len/2)+1];

what is meaning of above statements esp. the highlighted ones?

Commented:
(1)-1 == 0
(1) == 1
(1)+1 == 2
The statements have the same structure as
newNums[(len/2)-1]=nums[(len/2)-1];
newNums[(len/2)]=nums[(len/2)];
newNums[(len/2)+1]=nums[(len/2)+1];
with the len/2 on the left replaced by 1
I could also have said
newNums[(newNums.length/2)-1]=nums[(len/2)-1];
newNums[(newNums.length/2)]=nums[(len/2)];
newNums[(newNums.length/2)+1]=nums[(len/2)+1];
Author Commented:
``````public int[] midThree(int[] nums) {
int len=nums.length;
int[] newNums=new int[3];

newNums[(len/2)-1]=nums[(len/2)-1];
newNums[(len/2)]=nums[(len/2)];
newNums[(len/2)+1]=nums[(len/2)+1];
return newNums;

}
``````

i thought i pass but failed some tests

xpected      Run
midThree({1, 2, 3, 4, 5}) → {2, 3, 4}      {xception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:9}      X
midThree({8, 6, 7, 5, 3, 0, 9}) → {7, 5, 3}      {xception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:8}      X
midThree({1, 2, 3}) → {1, 2, 3}      {1, 2, 3}

Author Commented:
return Arrays.copyOfRange(nums, minIndex, minIndex + 3);

Author Commented:
``````public int[] midThree(int[] nums) {
int len=nums.length;
int[] newNums=new int[3];

newNums[0]=nums[(len/2)-1];
newNums[1]=nums[(len/2)];
newNums[2]=nums[(len/2)+1];
return newNums;

}
``````

above passed all. I wonder what is difference compared to previous my post?
Commented:
newNums[(len/2)]
newNums[1]
what is difference?

newNums.length
nums.length
what is difference?

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:
i see my mistake.

I origniginally meant to write like below

``````public int[] midThree(int[] nums) {
int len=nums.length;
int[] newNums=new int[3];
int len2=newNums.length;

newNums[(len2/2)-1]=nums[(len/2)-1];
newNums[(len2/2)]=nums[(len/2)];
newNums[(len2/2)+1]=nums[(len/2)+1];
return newNums;

}
``````

which i passed all tests. But wrote wrongly
###### 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
Java

From novice to tech pro — start learning today.