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

Open in new window


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
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:
int m=nums.length/2;
  return new int[]{nums[m-1],nums[m],nums[m+1]};
0
ozoCommented:
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;
  
  
}

Open in new window

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

Open in new window

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.

gudii9Author 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?

please advise
0
ozoCommented:
(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];
0
gudii9Author 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;
  
  
}

Open in new window


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}


please advise
0
gudii9Author Commented:
 return Arrays.copyOfRange(nums, minIndex, minIndex + 3);

what is meaning of above sentense. please advise
0
gudii9Author 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;
  
  
}

Open in new window


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

newNums.length
nums.length
what is difference?
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:
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;
  
  
}

Open in new window


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

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.