Solved

either24  challenge

Posted on 2016-08-03
19
83 Views
Last Modified: 2016-08-08
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;
}

Open in new window




I am passing all tests

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 11
  • 7
19 Comments
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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

by:gudii9
Comment Utility
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;
}

Open in new window


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

by:gudii9
Comment Utility
i am failing if both false case try to fix it
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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

by:gudii9
Comment Utility
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;
}

Open in new window


above is bit close still far. i need to stretch my thinking further in this logic
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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

by:gudii9
Comment Utility
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.*/

Open in new window


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 31

Expert Comment

by:awking00
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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.*/

Open in new window

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 31

Accepted Solution

by:
awking00 earned 500 total points
Comment Utility
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

by:gudii9
Comment Utility
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;}

Open in new window


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

Open in new window

when to go combined approach and when to go individual approach?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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;}

Open in new window


is there is a way i can combine above solution as one if statement rather than two?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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);}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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?
i am bit confused. please advise
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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);
	  
	}

}

Open in new window


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.

same code is passing all tests in coding bat. please advise

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

Open in new window


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

by:gudii9
Comment Utility
my debug results are as below

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

Expert Comment

by:awking00
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now