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

}
``````

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

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:
//You may want to be sure you understand the difference between & and &&
return nums.length==2&&nums[0]==nums[1]&&(nums[0]|1)==3;
Commented:
``````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;
}
``````
Information Technology SpecialistCommented:
if (nums.length < 2 || nums[0] != nums[1]) {return false;}
if (nums[0] != 2 || nums[0] != 3) {return false;}
return true;

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.

Commented:
Does not pass all tests, awking00. :(
Information Technology SpecialistCommented:
Got it backwards -
if (nums.length < 2 || nums[0] != nums[1]) {return false;}
if (nums[0] == 2 || nums[0] == 3) {return true;}
return false;
Information Technology SpecialistCommented:
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;
Author 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;
}

Author Commented:
``````public boolean double23(int[] nums) {
int len=nums.length;

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

}
``````

when i put && instead of & in my code all worked fine. I wonder why? Please advise
Author 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?
Author 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;

}
``````

this also passed all tests but what is 3  and above appoach? I am not clear. Please advise
Commented:
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
Commented:
(nums[0]|1) == 3 when nums[0]==2
Author Commented:
``````nums[0]|1
``````
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
Commented:
(2|1)==3
Author Commented:
(2|1)==3

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

some bit operator?
Information Technology SpecialistCommented:
>>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.
Author 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
###### 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.