double23 challenge

Hi,

I am trying below challenge

http://codingbat.com/prob/p145365

public boolean double23(int[] nums) {
  int len=nums.length;

 if(len==2&(nums[0]==nums[1]))
 return true;
 else return false;
 
 }

Open in new window


i wrote as above and failing test cases as below

Expected      Run            
double23({2, 2}) → true      true      OK         
double23({3, 3}) → true      true      OK         
double23({2, 3}) → false      false      OK         
double23({3, 2}) → false      false      OK         
double23({4, 5}) → false      false      OK         
double23({2}) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:4)      X         
double23({3}) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:4)      X         
double23({}) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:4)      X         
double23({3, 4}) → false      false      OK         
other tests
X      

how to fix and 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:
//You may want to be sure you understand the difference between & and &&
return nums.length==2&&nums[0]==nums[1]&&(nums[0]|1)==3;
0
krakatoaCommented:
public boolean double23(int[] nums) {
  
  if(nums.length==1){return false;}
  
  if (nums.length ==0){return false;}
  for (int a=0;a<nums.length;a++){if((nums[a]==2&&nums[a+1]==2)||nums[a]==3&&nums[a+1]==3){return true;}else{return false;}}
  return false;
}

Open in new window

0
awking00Commented:
if (nums.length < 2 || nums[0] != nums[1]) {return false;}
if (nums[0] != 2 || nums[0] != 3) {return false;}
return true;
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
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.

krakatoaCommented:
Does not pass all tests, awking00. :(
0
awking00Commented:
Got it backwards -
if (nums.length < 2 || nums[0] != nums[1]) {return false;}
 if (nums[0] == 2 || nums[0] == 3) {return true;}
 return false;
0
awking00Commented:
Should have changed the first one to AND not OR -
if (nums.length < 2 || nums[0] != nums[1]) {return false;}
 if (nums[0] != 2 && nums[0] != 3) {return false;}
 return true;
0
gudii9Author Commented:
//You may want to be sure you understand the difference between & and &&

&& will not even go to second statement when first one is false right
where as & checks for both expressions before and after & right?


 return nums.length==2&&nums[0]==nums[1]&&(nums[0]|1)==3;
 }

what is meaning of (nums[0]|1)==3 please advise
0
gudii9Author Commented:
public boolean double23(int[] nums) {
  int len=nums.length;

 if(len==2&&(nums[0]==nums[1]))
 return true;
 else return false;
 
 }

Open in new window


when i put && instead of & in my code all worked fine. I wonder why? Please advise
0
gudii9Author Commented:
if(len==2&&(nums[0]==nums[1]))

will go to second statement (nums[0]==nums[1])only when first statement (len==2) is true.

if len not 2 then it will go down and return false automatically right?
0
gudii9Author Commented:
public boolean double23(int[] nums) {
if (nums.length < 2 || nums[0] != nums[1]) {return false;}
 if (nums[0] == 2 || nums[0] == 3) {return true;}
 return false;
 
 }

Open in new window


this also passed all tests but what is 3  and above appoach? I am not clear. Please advise
0
ozoCommented:
when i put && instead of & in my code all worked fine. I wonder why?
because
&& will not even go to second statement when first one is false
0
ozoCommented:
(nums[0]|1) == 3 when nums[0]==2
0
gudii9Author Commented:
nums[0]|1

Open in new window

what it means by above code.
nums[0] element OR 1 if it is true(or false) equate to 3..how nums[0] ==2 i am bit lost. please advise
0
ozoCommented:
(2|1)==3
0
gudii9Author Commented:
(2|1)==3

how it is 3. what is meaning of | operator. Is it like addition operator?
please advise
I never heard | kind of operatoir except it used in context of OR right?

some bit operator?
0
awking00Commented:
>>public boolean double23(int[] nums) {
if (nums.length < 2 || nums[0] != nums[1]) {return false;}
 if (nums[0] == 2 || nums[0] == 3) {return true;}
 return false;
 }
this also passed all tests but what is 3  and above appoach? I am not clear. Please advise<<
The first if says,if the length is 1 or zero or if the first two numbers are not the same, return false. If the first line is not true, then there has to be two numbers and they must be equal and if they are a 2 or a 3, return true else return false.
0
gudii9Author Commented:
return nums.length==2&&nums[0]==nums[1]&&(nums[0]|1)==3;
i above line i was confused with below

(nums[0]|1)==3

but now i undertand that we are checking it is either 2 or 3 only not any other number(sibnce only 2, 3 can be repeated)
2 0010
1 0001 or
  ----
3 0011
 
3 0011
1 0001 or
  ----
3 0011
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.