Solved

# either24  challenge

Posted on 2016-08-03
107 Views
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

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 11
• 7

LVL 32

Expert Comment

ID: 41742808
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?
0

LVL 32

Expert Comment

ID: 41742834
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
0

LVL 7

Author Comment

ID: 41742845
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
0

LVL 7

Author Comment

ID: 41742859
i am failing if both false case try to fix it
0

LVL 32

Expert Comment

ID: 41742860
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.
0

LVL 7

Author Comment

ID: 41742870
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
0

LVL 32

Expert Comment

ID: 41742886
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.
0

LVL 7

Author Comment

ID: 41743138
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?
0

LVL 32

Expert Comment

ID: 41743240
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
0

LVL 7

Author Comment

ID: 41743296
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
0

LVL 32

Accepted Solution

awking00 earned 500 total points
ID: 41744185
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;
0

LVL 7

Author Comment

ID: 41745126
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?
0

LVL 7

Author Comment

ID: 41745909
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?
0

LVL 7

Author Comment

ID: 41746417
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);}
0

LVL 7

Author Comment

ID: 41746424
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?
0

LVL 7

Author Comment

ID: 41746570
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
0

LVL 7

Author Comment

ID: 41746577
my debug results are as below

index has2s has4s
0        false    false
1        true     false
2        true     false
3        true     true
0

LVL 32

Expert Comment

ID: 41747190
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.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Need help aligning JRadioButton that put in a box using GridBagLayout 2 42
DTD and JAVA versions 1 55
IBM TS2900 (3572) Tape Autoloader Java? 12 106
Cannot locate cell 15 41
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may heâ€¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This videoâ€¦
###### Suggested Courses
Course of the Month6 days, 14 hours left to enroll