no23 challenge

Hi,

i am trying

http://codingbat.com/prob/p175689
public boolean no23(int[] nums) {
  
  
  
  
  int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
  if(nums[i]==2 | nums[i]==3)
  return false;
  
  else
  return true;
  }
  
  return false;
}

Open in new window


i failed few tests. 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:
return (nums[0]&~1)!=2&&(nums[1]|1)!=3;

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
ozoCommented:
or
public boolean no23(int[] nums) {
  
  
  
  
  int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
  if(nums[i]==2 | nums[i]==3)
  return false;
  
  //else
  //return true;
  }
  
  return /*false*/true;
}

Open in new window

LazyFolkCommented:
this works

public int[] makeEnds(int[] nums) {
  return new int[]{nums[0],nums[Math.max(0,nums.length-1)]};
}
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

gudii9Author Commented:
 return /*false*/true;

i see above additional line compared to my code  as above what it means by above line?


also what is meaning of below

return (nums[0]&~1)!=2&&(nums[1]|1)!=3;

please advise
gudii9Author Commented:
public boolean no23(int[] nums) {  
  
  
   return new int[]{nums[0],nums[Math.max(0,nums.length-1)]};
}

Open in new window


above giving below error
Compile problems:


Error:      return new int[]{nums[0],nums[Math.max(0,nums.length-1)]};
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from int[] to boolean


see Example Code to help with compile problems

please advise
LazyFolkCommented:
Oh sorry, I did answer to another challenges

here is the correct answer :

public boolean no23(int[] nums) {
   return (nums[0]!=2 && nums[0]!=3  && nums[1]!=2 && nums[1]!=3);
}

Open in new window



It will return true if :

         Number1 is not 2             (nums[0]!=2)
AND Number1 is not 3             (&& nums[0]!=3)
AND Number2 is not 2             (&& nums[1]!=2)
AND Number2 is not 3             (&& nums[1]!=3)
ozoCommented:
return /*false*/true;
means
return true;

return (nums[0]&~1)!=2&&(nums[1]|1)!=3;
means
return (nums[0]!=2&&nums[0]!=3)&&(nums[1]!=2&&nums[1]!=3);

nums[0]&~1 == 2 when nums[0]==2 or nums[0]==3
nums[1]|1 == 3 when nums[1]==2 or nums[1]==3
gudii9Author Commented:
return /*false*/true;
means
return true;

in above code
 /*false*/ false is surrounded with comment right?
gudii9Author Commented:
return (nums[0]&~1)!=2&&(nums[1]|1)!=3;
means
return (nums[0]!=2&&nums[0]!=3)&&(nums[1]!=2&&nums[1]!=3);


is above ~ is java 8 syntax like lambda?

return (nums[0]!=2&&nums[0]!=3)&&(nums[1]!=2&&nums[1]!=3);

how is above with brakets different from below


   return (nums[0]!=2 && nums[0]!=3  && nums[1]!=2 && nums[1]!=3);

without any brackets?
ozoCommented:
(nums[0]&~1)==2 when nums[0]==3

parenthesis are unnecessary in
 return nums[0]!=2 && nums[0]!=3 && nums[1]!=2 && nums[1]!=3;
gudii9Author Commented:
return (nums[0]!=2&&nums[0]!=3)&&(nums[1]!=2&&nums[1]!=3);

nums[0]&~1 == 2 when nums[0]==2 or nums[0]==3
nums[1]|1 == 3 when nums[1]==2 or nums[1]==3

Open in new window


above solution is not clear.
how it check no 2 or no 3 in the given array for different lenghts.

any simple examples on ~
please advise
ozoCommented:
public boolean no23(int[] nums) {
  for( int i : nums ){
     if( i==2||i==3 ){ return false; }
  }
  return true;
}

Open in new window

public boolean no23(int[] nums) {
  for( int i : nums ){
     if( (i&~1)==2 ){ return false; }
  }
  return true;
}

Open in new window

public boolean no23(int[] nums) {
  for( int i : nums ){
     if( (i|1)==3 ){ return false; }
  }
  return true;
}

Open in new window

public boolean no23(int[] nums) {
  for( int i : nums ){
     if( i/2==1 ){ return false; }
  }
  return true;
}

Open in new window

gudii9Author Commented:
public boolean no23(int[] nums) {
  for( int i : nums ){
     if( i==2||i==3 ){ return false; }
  }
  return true;
}

Open in new window


above is clear.

Below lines not clear please advise


if( (i&~1)==2 ){ //what it means by i&~1 means ??

  if( (i|1)==3 ){ //what it means by i|1 ??

 if( i/2==1 ){ //how it checks no 2 as well as no 3.  is it due to division operator only 2, 3 divisible with 1 as result? if i take  i value as 4 then result is 2 which is not equal to 1 so true right??
gudii9Author Commented:
please advise
ozoCommented:
gudii9Author Commented:
1 = 0001
2 = 0010
3 = 0011
 
2 0010
1 0001 or
  ----
3 0011
 
3 0011
1 0001 or
  ----
3 0011


Bitwise OR is clear but bitwise AND solution as below is not clear.
return (nums[0]&~1)!=2&&(nums[1]|1)!=3;
 Please advise
gudii9Author Commented:
2    0010
~1 1110 AND
  -------------------
2  0010
 


3    0011
~1 1110 AND
  ----------------
2   0010




nums[1]|1
2 0010
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
Programming Languages-Other

From novice to tech pro — start learning today.