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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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

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

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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];
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
gudii9Author Commented:
 return Arrays.copyOfRange(nums, minIndex, minIndex + 3);

what is meaning of above sentense. please advise
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?
ozoCommented:
newNums[(len/2)]
newNums[1]
what is difference?

newNums.length
nums.length
what is difference?

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
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.