# either24 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p191878

Psedo code description of approach :
1. Loop throguh given array
2. check if array element 2 with adjacent one also 2 or  array element 4  with adjacent array element also 4
3. if yes return true
4. if no return false

I wrote my code as below

``````public boolean either24(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==2&&nums[i+1]==2)||(nums[i]==4&&nums[i+1]==4)){
res=true;

}
}
return res;
}
``````

I am passing all tests

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

Information Technology SpecialistCommented:
I'm not sure how your code works for the case when a 2 follows a 2 and a 4 follows a 4. The challenge is a little vague on what to return when neither condition is met. Can you post the remaining tests?
Information Technology SpecialistCommented:
I would think the pseudo code approach would be more along the lines of
1) create variables for successive2s and succesive4s (perhaps ints = 0)
2) loop through array
3) if successive 2s exist set the 2s variable to 1
4) if successive 4s exist set the 4s variable to 1
return 2s variable + 4s variable = 1
Author Commented:
``````public boolean either24(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==2&&nums[i+1]==2)||(nums[i]==4&&nums[i+1]==4)){
res=true;

}
}
return res;
}
``````

above is my code
Expected      Run
either24([1, 2, 2]) → true      true      OK
either24([4, 4, 1]) → true      true      OK
either24([4, 4, 1, 2, 2]) → false      true      X
either24([1, 2, 3, 4]) → false      false      OK
either24([3, 5, 9]) → false      false      OK
either24([1, 2, 3, 4, 4]) → true      true      OK
either24([2, 2, 3, 4]) → true      true      OK
either24([1, 2, 3, 2, 2, 4]) → true      true      OK
either24([1, 2, 3, 2, 2, 4, 4]) → false      true      X
either24([1, 2]) → false      false      OK
either24([2, 2]) → true      true      OK
either24([4, 4]) → true      true      OK
either24([2]) → false      false      OK
either24([]) → false      false      OK
other tests
X

i am failing 2 tests. Not sure why. let me check modified pseudo code
Author Commented:
i am failing if both false case try to fix it
Information Technology SpecialistCommented:
Your code returns true when either a "pair or 2s" or a "pair of 4s" exist, but doesn't return false when both cases exist.
Author Commented:
``````public boolean either24(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==2&&nums[i+1]==2)&&(nums[i]==4&&nums[i+1]==4)){
return res;
}
if( (nums[i]==2&&nums[i+1]==2)||(nums[i]==4&&nums[i+1]==4) ){
res=true;

}

}
return res;
}
``````

above is bit close still far. i need to stretch my thinking further in this logic
Information Technology SpecialistCommented:
Think about it this way. There are only three possibilities, either there are no pairs of 2s or 4s, there are both pairs of 2s and 4s, or there is only one pair of 2s or one pair of 4s. The first two possibilities will return false while the third possibility returns true.
Author Commented:
``````public boolean either24(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==2&&nums[i+1]!=2)&&(nums[i]==4&&nums[i+1]!=4)){//first posiiblility no pair
return res;
}
if( (nums[i]==2&&nums[i+1]==2)||(nums[i]==4&&nums[i+1]==4) ){//second posiiblility both

res= false;
return res;

}
if( ((nums[i]==2&&nums[i+1]==2)&&(nums[i]!=4&&nums[i+1]!=4))||
((nums[i]!=2&&nums[i+1]!=2)&&(nums[i]==4&&nums[i+1]==4)) ){//third posiiblility one pair
res=true;
return res;

}
}
return res;
}

/*Think about it this way. There are only three possibilities, either there are no pairs
of 2s or 4s, there are both pairs of 2s and 4s, or there is only one pair of 2s or
one pair of 4s.
The first two possibilities will return false while the third possibility returns true.*/
``````

something like above?
Above fails few test cases.
Expected      Run
either24([1, 2, 2]) → true      false      X
either24([4, 4, 1]) → true      false      X
either24([4, 4, 1, 2, 2]) → false      false      OK
either24([1, 2, 3, 4]) → false      false      OK
either24([3, 5, 9]) → false      false      OK
either24([1, 2, 3, 4, 4]) → true      false      X
either24([2, 2, 3, 4]) → true      false      X
either24([1, 2, 3, 2, 2, 4]) → true      false      X
either24([1, 2, 3, 2, 2, 4, 4]) → false      false      OK
either24([1, 2]) → false      false      OK
either24([2, 2]) → true      false      X
either24([4, 4]) → true      false      X
either24([2]) → false      false      OK
either24([]) → false      false      OK
other tests
X
how did you conclude there are only 3 possibilities not 4 or 5?
Information Technology SpecialistCommented:
I probably should have stated it differently. I guess there really are 4 possibilities
1) There are no pairs of 2s or 4s
2) There are 1 or more pairs of both 2s and 4s
3) There are 1 or more pairs of 2s but no pairs of 4s
4) There are 1 or more pairs of 4s but no pairs of 2s

Rather than combining the 2s and 4s, you might try evaluating them separately. So looking at the 4 listed possibilities, you would have
1) No and No
2) Yes and Yes
3) Yes and No
4) No and Yes
Making 1 and 2 = false and 3 and 4 = true
Author Commented:
``````public boolean either24(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==2&&nums[i+1]!=2)&&(nums[i]==4&&nums[i+1]!=4)){//first posiiblility no pair
return res;
}
if( (nums[i]==2&&nums[i+1]==2)||(nums[i]==4&&nums[i+1]==4) ){//second posiiblility both

res= false;
return res;
}
if( ((nums[i]==2&&nums[i+1]==2)&&(nums[i]!=4&&nums[i+1]!=4))
){//third posiiblility one pair
res=true;
return res;
}

if(  ((nums[i]!=2&&nums[i+1]!=2)&&(nums[i]==4&&nums[i+1]==4))
){//fourth posiiblility one pair
res=true;
return res;
}
}
return res;
}
I probably should have stated it differently. I guess there really are 4 possibilities
1) There are no pairs of 2s or 4s
2) There are 1 or more pairs of both 2s and 4s
3) There are 1 or more pairs of 2s but no pairs of 4s
4) There are 1 or more pairs of 4s but no pairs of 2s
Rather than combining the 2s and 4s, you might try evaluating them separately. So looking at the 4 listed possibilities, you would have
1) No and No
2) Yes and Yes
3) Yes and No
4) No and Yes
Making 1 and 2 = false and 3 and 4 = true

/*Think about it this way. There are only three possibilities, either there are no pairs
of 2s or 4s, there are both pairs of 2s and 4s, or there is only one pair of 2s or
one pair of 4s.
The first two possibilities will return false while the third possibility returns true.*/
``````
like above it fails below?
Expected      Run
either24([1, 2, 2]) → true      false      X
either24([4, 4, 1]) → true      false      X
either24([4, 4, 1, 2, 2]) → false      false      OK
either24([1, 2, 3, 4]) → false      false      OK
either24([3, 5, 9]) → false      false      OK
either24([1, 2, 3, 4, 4]) → true      false      X
either24([2, 2, 3, 4]) → true      false      X
either24([1, 2, 3, 2, 2, 4]) → true      false      X
either24([1, 2, 3, 2, 2, 4, 4]) → false      false      OK
either24([1, 2]) → false      false      OK
either24([2, 2]) → true      false      X
either24([4, 4]) → true      false      X
either24([2]) → false      false      OK
either24([]) → false      false      OK
other tests
X
Your progress graph for this problem
Information Technology SpecialistCommented:
Again, I would suggest evaluating the 2s and 4s separately -
boolean has2s = false;
boolean has4s = false;
for (int j = 0; j < nums.length - 1; j++) {
if (nums[j]==2&&nums[j+1]==2) {
has2s = true;
}
if (nums[j]==4&&nums[j+1]==4) {
has4s = true;
}
}
return (has2s && !has4s || !has2s && has4s) ? true : false;

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.

Author Commented:
``````public boolean either24(int[] nums) {boolean has2s = false;
boolean has4s = false;
for (int j = 0; j < nums.length - 1; j++) {
if (nums[j]==2&&nums[j+1]==2) {
has2s = true;
}
if (nums[j]==4&&nums[j+1]==4) {
has4s = true;
}
}
return (has2s && !has4s || !has2s && has4s) ? true : false;}
``````

above passes all tests.

How evaluating separately makes such huge difference compared to evaluating combinedly as in my original aproach?

``````public boolean either24(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==2&&nums[i+1]==2)||(nums[i]==4&&nums[i+1]==4)){
res=true;

}
}
return res;
}
``````
when to go combined approach and when to go individual approach?
Author Commented:
``````public boolean either24(int[] nums) {boolean has2s = false;
boolean has4s = false;
for (int j = 0; j < nums.length - 1; j++) {
if (nums[j]==2&&nums[j+1]==2) {
has2s = true;
}
if (nums[j]==4&&nums[j+1]==4) {
has4s = true;
}
}
return (has2s && !has4s || !has2s && has4s) ? true : false;}
``````

is there is a way i can combine above solution as one if statement rather than two?
Author Commented:
or like below

``````public boolean either24(int[] nums) {boolean has2s = false;
boolean has4s = false;
for (int j = 0; j < nums.length - 1; j++) {
if (nums[j]==2&&nums[j+1]==2) {
has2s = true;
}
if (nums[j]==4&&nums[j+1]==4) {
has4s = true;
}
}
return (has2s ^ has4s);}
``````
Author Commented:
lets say my aray is {1,2,2,4,4}

has2s  becomes false true false false
has4s becomes false false false true
for each iteration of for loop starting index 0

does return statement return (has2s ^ has4s);} runs four times or only once?
Author Commented:
``````public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar={2,2,1,4,4};
System.out.println("is==="+either24(ar));

}

public static boolean either24(int[] nums) {
boolean has2s = false;
boolean has4s = false;
for (int j = 0; j < nums.length - 1; j++) {
if (nums[j]==2&&nums[j+1]==2) {
has2s = true;
}
if (nums[j]==4&&nums[j+1]==4) {
has4s = true;
System.out.println("is--->>"+has4s);
}
}
return (has2s ^ has4s);

}

}
``````

i debuggged my code in eclipse as return is after for loop it is returned only once at the end of the method after for loop.

I wonder why above code is giving below output

is--->>true

it supposed to give true as output.

``````	boolean has2s = false;
boolean has4s = false;
for (int j = 0; j < nums.length - 1; j++) {
if (nums[j]==2&&nums[j+1]==2) {
has2s = true;
}
if (nums[j]==4&&nums[j+1]==4) {
has4s = true;
System.out.println("is--->>"+has4s);
}
}
return (has2s ^ has4s);

``````

Expected      Run
either24([1, 2, 2]) → true      true      OK
either24([4, 4, 1]) → true      true      OK
either24([4, 4, 1, 2, 2]) → false      false      OK
either24([1, 2, 3, 4]) → false      false      OK
either24([3, 5, 9]) → false      false      OK
either24([1, 2, 3, 4, 4]) → true      true      OK
either24([2, 2, 3, 4]) → true      true      OK
either24([1, 2, 3, 2, 2, 4]) → true      true      OK
either24([1, 2, 3, 2, 2, 4, 4]) → false      false      OK
either24([1, 2]) → false      false      OK
either24([2, 2]) → true      true      OK
either24([4, 4]) → true      true      OK
either24([2]) → false      false      OK
either24([]) → false      false      OK
other tests
OK
Author Commented:
my debug results are as below

index has2s has4s
0        false    false
1        true     false
2        true     false
3        true     true
Information Technology SpecialistCommented:
I'm not sure what your latest issue is since all tests are passing. The only difference between what I suggested and what you have done is the use of the exclusive or operator ("^"), which is perfectly fine and somewhat more direct. I only showed the return (has2s && !has4s || !has2s && has4s) ? true : false;} to point out the logic that if the first is true but not the second or the second is true but not the first, then return true else return false, which is precisely what the ^ operator does. Since we are comparing booleans that can only have a value of true or false, we could also have simply said return has2s != has4s.
###### 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.