Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

271 challenge

Hi

I am trying below challenge
http://codingbat.com/prob/p167430

I was not sure on how to proceed. please advise
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

1, 6, 0 also valid right?(as it says ---->>pattern -- a value, followed by the value plus 5, followed by the value minus 1)
Avatar of gudii9

ASKER

return true if the array contains any of
2, 7, -1
or
2, 7, 0
or
2, 7, 1
or
2, 7, 2
or
2, 7, 3

I have to check in sequence and in pattern right. please advise
1, 6, 0 also valid right?
I believe so
Avatar of gudii9

ASKER

please advise on how should i start thinking about this challenge and proceed?
Avatar of gudii9

ASKER

Please advise
I think you have got to try a few moves and ideas first.
Avatar of gudii9

ASKER

public boolean has271(int[] nums) {
int i=nums.length;

if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j]==7 && nums[j]==1)
return true;
}

}
  return false;
}

Open in new window


i wrote like above. passing in some test cases and failing in others
Expected	Run		
has271({1, 2, 7, 1}) → true	false	X	    
has271({1, 2, 8, 1}) → false	false	OK	    
has271({2, 7, 1}) → true	false	X	    
has271({3, 8, 2}) → true	false	X	    
has271({2, 7, 3}) → true	false	X	    
has271({2, 7, 4}) → false	false	OK	    
has271({2, 7, -1}) → true	false	X	    
has271({2, 7, -2}) → false	false	OK	    
has271({4, 5, 3, 8, 0}) → true	false	X	    
has271({2, 7, 5, 10, 4}) → true	false	X	    
has271({2, 7, -2, 4, 9, 3}) → true	false	X	    
has271({2, 7, 5, 10, 1}) → false	false	OK	    
has271({2, 7, -2, 4, 10, 2}) → false	false	OK	 

Open in new window


please advise
Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?

i see your point now. I improved as below my success rate in test cases but not 100% yet. Please advise
public boolean has271(int[] nums) {
int i=nums.length;

if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1)
return true;
}

}
  return false;
}

Open in new window


Expected	Run		
has271({1, 2, 7, 1}) → true	true	OK	    
has271({1, 2, 8, 1}) → false	false	OK	    
has271({2, 7, 1}) → true	true	OK	    
has271({3, 8, 2}) → true	false	X	    
has271({2, 7, 3}) → true	false	X	    
has271({2, 7, 4}) → false	false	OK	    
has271({2, 7, -1}) → true	false	X	    
has271({2, 7, -2}) → false	false	OK	    
has271({4, 5, 3, 8, 0}) → true	false	X	    
has271({2, 7, 5, 10, 4}) → true	false	X	    
has271({2, 7, -2, 4, 9, 3}) → true	false	X	    
has271({2, 7, 5, 10, 1}) → false	false	OK	    
has271({2, 7, -2, 4, 10, 2}) → false	false	OK	    

Open in new window

Avatar of gudii9

ASKER

has271({2, 7, 5, 10, 4}) → true	false	X	    
has271({2, 7, -2, 4, 9, 3}) → true	false	X

Open in new window


I wonder how above two test cases supposed to be true according to below code challenge description

Given an array of ints, return true if it contains a 2, 7, 1 pattern -- a value, followed by the value plus 5, followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.

has271({1, 2, 7, 1}) → true
has271({1, 2, 8, 1}) → false
has271({2, 7, 1}) → true

5 and -2 are never 'near differ by 2' from 1 right?
please advise
has271({2, 7, 5, 10, 4}) → true      false      X
a value (5), followed by the value plus 5 (10), followed by the value minus 1 (4).
          
has271({2, 7, -2, 4, 9, 3}) → true      false      X
a value (4), followed by the value plus 5 (9), followed by the value minus 1 (3).
Yeah - but that's not all there is to the story.

The final "value" you have to aim at, can be within a tolerance of 2 from "the value". You need to think about how you do that.
Avatar of gudii9

ASKER

still thinking. Seems bit challenging example
OK so what you need to think about is that you are given a set of ints. It doesn't matter what they are.

You are looking for a pattern somewhere in these ints. The search - or "thinking" - you have to do and are asked to carry out is  :

* You are "told" that the first int is the "pattern setter" let's call it. They have used the word "value" actually.

* You are also "told" that you are looking for a "pattern" involving just 3 of the ints you are given. You might be given more than 3 ints, you might be given fewer than 3, and you could be given exactly 3 ints. So however many ints you are given, you must ensure that you look at all of them (the entire [] ) to see if 3 of them follow the pattern that you have been asked to find. For this to happen, you obviously need to have in the back of your mind that the use of a loop could well come into the solution.

* THE "PATTERN"  (the task is) "are there 3 consecutive ints like this : 1) pattern-setter (first) value, followed by 2) pattern-setter PLUS 5, followed by (in the third position) 3) a value which is NOT MORE THAN 2 different from the pattern-setter after 1 has been taken away from the pattern-setter"?

If your ints are for example 8,13,5 then would the pattern have been found in these ints or not???

Well - 8 is our pattern-setter, so that is a given (we accept that no matter what else we are thinking about). Right, so we know that we are asked to discover whether the next int is equal to pattern-setter plus 5. Is it? Yes it is, it's 13. 8 + 5 = 13. Right, now we need to know whether the 3rd int is equal to the pattern-setter minus 1 (which is 8-1 =7) and then ALSO BE not more than 2 away from 7. So is 5 more than two away from 7? No it IS 2 away from 7, so it satisfies our third criterion. Now we have evaluated 3 ints and found that they satisfy the question, we can return true. We found the pattern.

8,13,5 was ok. 8,13,6 would have been too. 8,13,7 would have been also. Can you see the meta here now? If so, go back to the code and match it up with the above explanatory steps, and go over it until you see it flow.
Avatar of gudii9

ASKER

i need to come up some example based on above notes
OK, so this part should be really for you alone, as you have said several times in the past that you could see the ideas behind the code more easily in direct English, rather than in straight code. So now is your chance to think up a good problem ( or Googl for one) -  and then try to give thought to a coding solution for it,  given what you know.
Avatar of gudii9

ASKER

this challenge is going over my head. I will close this and try later.
For this challenge, you probably shouldn't worry too much about using more cases than you need to.
Avatar of gudii9

ASKER

sure. Let me rethink again and again and come up with solution
Avatar of gudii9

ASKER

Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?

above is never true because at same index of array 3 numbers cannot be there.
My solution as below is true for some cases but false for some other

public boolean has271(int[] nums) {
int i=nums.length;

if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1)
return true;
}

}
  return false;
}

Open in new window


please advise
Expected	Run		
has271({1, 2, 7, 1}) → true	true	OK	    
has271({1, 2, 8, 1}) → false	false	OK	    
has271({2, 7, 1}) → true	true	OK	    
has271({3, 8, 2}) → true	false	X	    
has271({2, 7, 3}) → true	false	X	    
has271({2, 7, 4}) → false	false	OK	    
has271({2, 7, -1}) → true	false	X	    
has271({2, 7, -2}) → false	false	OK	    
has271({4, 5, 3, 8, 0}) → true	false	X	    
has271({2, 7, 5, 10, 4}) → true	false	X	    
has271({2, 7, -2, 4, 9, 3}) → true	false	X	    
has271({2, 7, 5, 10, 1}) → false	false	OK	    
has271({2, 7, -2, 4, 10, 2}) → false

Open in new window

Avatar of gudii9

ASKER

public boolean has271(int[] nums) {
int i=nums.length;

if(i>=3){
for(int j=0;j<i-1;j++)
{

if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1){
return true;
}

if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==2){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==3){
return true;
}

if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==0){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==-1){
return true;
}

}
}
  return false;
}

Open in new window


i updated my code and passed more tests as below

Expected	Run		
has271({1, 2, 7, 1}) → true	true	OK	    
has271({1, 2, 8, 1}) → false	false	OK	    
has271({2, 7, 1}) → true	true	OK	    
has271({3, 8, 2}) → true	false	X	    
has271({2, 7, 3}) → true	true	OK	    
has271({2, 7, 4}) → false	false	OK	    
has271({2, 7, -1}) → true	true	OK	    
has271({2, 7, -2}) → false	false	OK	    
has271({4, 5, 3, 8, 0}) → true	false	X	    
has271({2, 7, 5, 10, 4}) → true	false	X	    
has271({2, 7, -2, 4, 9, 3}) → true	false	X	    
has271({2, 7, 5, 10, 1}) → false	false	OK	    
has271({2, 7, -2, 4, 10, 2}) → false	false	OK	    

Open in new window

stil failing on 4 test cases. please advise
Do you understand why those 4 test cases should return true?
If so, can you add conditions to handle them?
So I wasted the time and effort of my comment at  ID: 40444324 - which you have not read at all apparently.

Instead, you have hard-wired your code to look at "271". I said in my above comment, that IT DOES NOT MATTER what those 3 ints are - they COULD BE ANYTHING - "0,1,2", "7,9,11" . . .  you name it.

Re-read my quoted comment, and think again.

---------
And . . .  the last code you posted looks NOTHING LIKE the code skeleton I posted at  : ID: 40438936
Avatar of gudii9

ASKER

sure i will try
You are looking for the relationship between 3 consecutive ints.

That relationship HAS to be :

1st int = anything

2nd int = 1st int plus 5

3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 3.

If you are given more than 3 ints, then move along the array one place (leaving the first int out of it) and include the next int in the triplet. This is where you need the for loop.
Avatar of gudii9

ASKER

has271({3, 8, 2}) → true	false	X
has271({4, 5, 3, 8, 0}) → true	false	X	    
has271({2, 7, 5, 10, 4}) → true	false	X	    
has271({2, 7, -2, 4, 9, 3}) → true	false	X

Open in new window

i am failing above four.
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 3.
you mean it should be as below right
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 4.
Avatar of gudii9

ASKER

1st int = anything

2nd int = 1st int plus 5
above is clear

Below is not clear. I am trying to re read again and again to understand it

If you are given more than 3 ints, then move along the array one place (leaving the first int out of it) and include the next int in the triplet. This is where you need the for loop.
Avatar of gudii9

ASKER

public class Test34 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		/*has271({3, 8, 2}) → true	false	X
		has271({4, 5, 3, 8, 0}) → true	false	X	    
		has271({2, 7, 5, 10, 4}) → true	false	X	    
		has271({2, 7, -2, 4, 9, 3}) → true	false	X*/
		has271({3, 8, 2});
	}

	
	public static boolean has271(int[] nums) {
		int i=nums.length;

		if(i>=3){
		for(int j=0;j<i-1;j++)
		{

		if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1){
		return true;
		}

		if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==2){
		return true;
		}
		if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==3){
		return true;
		}

		if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==0){
		return true;
		}
		if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==-1){
		return true;
		}

		}
		}
		  return false;
		}
}

Open in new window


i am getting error. please advies
you mean it should be as below right

3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 4.

No. Going upwards from 3. Because 3 would be the "expected" value - because that's 4-1, which is 3. So 3 is in a sense the "correct" value BUT the rules say that you can deviate by 2 or less from that "correct" value, so you can have 3+1 (4) or 3+2 (5) going up.

Going down - if your 1st int is 4 - then the "correct" third int SHOULD be 3, but it COULD also be 2 or 1.
Look, whatever code you decide to write,  you simply :

* get the first int
* see if the next int is equal to the first int +5 and if it is then :
* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.

and if you are given more than 3 ints, then put the above into a for loop, and move along the array.
Avatar of gudii9

ASKER

public class Test34 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		

		int arr[]={1,8,2};
		System.out.println("value is--->"+has271(arr));
	}

	
	public static boolean has271(int[] nums) {
		
		/** get the first int
		* see if the next int is equal to the first int +5 and if it is then :
		* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.

		and if you are given more than 3 ints, then put the above into a for loop, and move along the array.*/
		int i=nums.length;

		if(i>=3){
		for(int j=0;j<i-1;)
		{
			
			if(nums[1]==nums[0]+5 && nums[2]==nums[0]-1);
			j=j+2;
			return true;
		}
		}
		  return false;
		}
}

Open in new window

i wrote as aboe to check second and third value of the array based on first. I see i am getting true no matter what i pass as below
value is--->true


Once i make above working i will think about adding additionally
or is 2 away from the first int-1 IN EITHER DIRECTION.
please advise
Avatar of gudii9

ASKER

Expected	Run		
has271({1, 2, 7, 1}) → true	true	OK	    
has271({1, 2, 8, 1}) → false	true	X	    
has271({2, 7, 1}) → true	true	OK	    
has271({3, 8, 2}) → true	true	OK	    
has271({2, 7, 3}) → true	true	OK	    
has271({2, 7, 4}) → false	true	X	    
has271({2, 7, -1}) → true	true	OK	    
has271({2, 7, -2}) → false	true	X	    
has271({4, 5, 3, 8, 0}) → true	true	OK	    
has271({2, 7, 5, 10, 4}) → true	true	OK	    
has271({2, 7, -2, 4, 9, 3}) → true	true	OK	    
has271({2, 7, 5, 10, 1}) → false	true	X	    
has271({2, 7, -2, 4, 10, 2}) → false	true	X	    

Open in new window

i am failing some test cases as above
Avatar of gudii9

ASKER

public class Test34 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		

		int arr[]={1,8,2};
		System.out.println("value is--->"+has271(arr));
	}

	
	public static boolean has271(int[] nums) {
		
		/** get the first int
		* see if the next int is equal to the first int +5 and if it is then :
		* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.

		and if you are given more than 3 ints, then put the above into a for loop, and move along the array.*/
		int i=nums.length;

		if(i>=3){
		for(int j=0;j<i-1;)
		{
			
			if(nums[1]==nums[0]+5 && (nums[2]==nums[0]-1||nums[2]==nums[0]-2||nums[2]==nums[0]+1||nums[2]==nums[0]+2));
			j=j+2;
			return true;
		}
		}
		  return false;
		}
}

Open in new window


i updated my code as above still failing 5 test cases
I feel i am getting close
Expected	Run		
has271({1, 2, 7, 1}) → true	true	OK	    
has271({1, 2, 8, 1}) → false	true	X	    
has271({2, 7, 1}) → true	true	OK	    
has271({3, 8, 2}) → true	true	OK	    
has271({2, 7, 3}) → true	true	OK	    
has271({2, 7, 4}) → false	true	X	    
has271({2, 7, -1}) → true	true	OK	    
has271({2, 7, -2}) → false	true	X	    
has271({4, 5, 3, 8, 0}) → true	true	OK	    
has271({2, 7, 5, 10, 4}) → true	true	OK	    
has271({2, 7, -2, 4, 9, 3}) → true	true	OK	    
has271({2, 7, 5, 10, 1}) → false	true	X	    
has271({2, 7, -2, 4, 10, 2}) → false	true	X	    

Open in new window


please advise
Avatar of gudii9

ASKER

public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
		for(int j=0;j<i-1;)
		{
			
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
			j=j+2;
			return true;
		}
		}
		  return false;
		}

Open in new window


i updated further still above 5 test cases failing. please advise
You are not looking at the skeleton code I posted still. Why are you not asking questions about that? Why are you making statements like this >if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
You are given 3 ints.

1st int is . ............................. 103

Right.

2nd int is .............................. 109

Is 103 + 5 = to 109 ? No. So that means the test already flunked. Return false.

How about .......... 1st int = 90.

2nd int =  95.

Is 90 + 5 = to 95??????????????????????. Answer = YES, it is. Right. So that's one "if statement out of the way!"

3rd int = 92

Is 92 = to (95-1), give or take 2?????????????????????????????????????????????????????????????????????

YES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IT IS !!!!!!!!!!!!!!!!!!!!!!!!!!!!

2nd if statisfied. WE HAVE A SATISFACTORY int triplet here!. Return TRUE!

----------------------------------------------------------------------------------------------------------------------------

Now . . . .  the places where you see 90, 95 and 92 above :

PUT ANY INTEGERS YOU LIKE, AND GO THROUGH THE STEPS JUST LIKE WE WENT THROUGH THEM ABOVE - EXACTLY THE SAME LOGIC, BUT USING YOUR NEW NUMBERS. SO WHEN YOU ADD 5, YOU ADD 5 TO THE FIRST NUMBER. WHEN YOU SEE -1, YOU TAKE ONE AWAY FROM YOUR FIRST INT. WHEN YOU DECIDE WHETHER THE 3RD INT YOU CHOSE IS OK, ASK YOUSELF IF IT IS NOT MORE THAN 2 DIFFERENT FROM THE FIRST INT-1.
Avatar of gudii9

ASKER

let me try.
Avatar of gudii9

ASKER

public boolean has271(int[] nums) {
int i=nums.length;

            for(int j=0;j<i-2;j++)
            {
                  
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
                  j=j+2;
                  return true;
            }
            
              return false;
            }


i modifiede as above. passed few more test cases but still not 100% correct
Expected	Run		
has271({1, 2, 7, 1}) → true	true	OK	    
has271({1, 2, 8, 1}) → false	true	X	    
has271({2, 7, 1}) → true	true	OK	    
has271({3, 8, 2}) → true	true	OK	    
has271({2, 7, 3}) → true	true	OK	    
has271({2, 7, 4}) → false	true	X	    
has271({2, 7, -1}) → true	true	OK	    
has271({2, 7, -2}) → false	true	X	    
has271({4, 5, 3, 8, 0}) → true	true	OK	    
has271({2, 7, 5, 10, 4}) → true	true	OK	    
has271({2, 7, -2, 4, 9, 3}) → true	true	OK	    
has271({2, 7, 5, 10, 1}) → false	true	X	    
has271({2, 7, -2, 4, 10, 2}) → false	true	X	   

Open in new window

Sorry that i am not able to completely follow on this challenge(since it seems bit stretch for me). Please advise how to fix and improve my code
Avatar of gudii9

ASKER

public boolean has271(int[] nums) {
int i=nums.length;

		for(int j=0;j<i-2;j++)
		{
			
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
			
			return true;
		}
		
		  return false;
		}

Open in new window


i modified furthwer now only 4 test cases failing instead of 5 compared to earlier comment
Avatar of gudii9

ASKER

has271({1, 2, 8, 1}) → false	true	X	    

has271({2, 7, 5, 10, 1}) → false	true	X	    
has271({2, 7, -2, 4, 10, 2}) → false	true	X	    

has271({2, 7, 4}) → false	true	X	    

Open in new window

({1, 2, 8, 1})  so . . . 1 + 5 = 6, so this one fails immediately. 2 + 5 = 7, so this fails because you are looking for an 8 there, and now there are not enough other ints left to test 3 in a row. So this set should return false.

({2, 7, 5, 10, 1})  . . . so 2 + 5 =7, so we are ok so far. But 5 is more than 2 away from 2-1 (which is 1), so that's a fail. So now 7 +5 is 12, not 10, so that's a fail. Then finally, 5 +5 IS 10, but again 5-1 (which is 4) is more than 2 away from 1, so the whole thing fails again to false.

has271({2, 7, -2, 4, 10, 2}) .... so 2+5 = 7, but 2-1 (which is 1) is not 2 away from -2, so fail. Now 7+5 isn't -2, so another fail. Now -2 +5 isn't 4, so another fail. Finally 4+5 isn't 10, so there's another fail, and no need to even look at the last 2 in that sequence.

has271({2, 7, 4}) . . . so 2+5 is 7, so that's ok, and so is 2-1 (which is 1) 2 or less away from 4? No, so another fail to false.
Here is some idiosyncratic pseudo-code which may help you :

if (    2nd int = 1st int plus 5   ) {we are ok so far; now test the third int; is the 3rd int AT MOST 2 different from the 1st int-1? If it is (  return true;            );}

{else return false;}

Keep doing this until you can't test 3 sequential ints from the series you are given because you've already moved along the series too far.
Avatar of gudii9

ASKER

is the 3rd int AT MOST 2 different from the 1st int-1? If it is (  return true;            );}

i mistakenly thought as below instead till now
is the 3rd int AT MOST 2 different from the 1st int? If it is (  return true;            );}


Given an array of ints, return true if it contains a 2, 7, 1 pattern -- a value, followed by the value plus 5, followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.
Avatar of gudii9

ASKER

public boolean has271(int[] nums) {
int i=nums.length;

		for(int j=0;j<i-2;j++)
		{
			
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]-3||nums[j+2]==nums[j]||nums[j+2]==nums[j]+1));
			
			return true;
		}
		
		  return false;
		}

Open in new window


i wrote as above still 5 failing
The reason your code is failing is because you have failed to understand the question.

I am not sure that any more help will do you any good here. The task is simple.

You get a series of ints.

The SECOND int has to be EXACTLY equal to the first int + 5.

If that test is passed, then the third int has to be equal to the first int -1, OR equal to the first int -1,GIVE OR TAKE 2.
In this series of ints:

1,2,32,33,45,766,12,2,7,1

only the last 3 ints will pass the test and return true. There are 8, repeat 8 sets of possible 3 ints you have to evaluate :

1,2,32
2,32,33
32,33,45
33,45,766
45,766,12
766,12,2
12,2,7
and
2,7,1.
. . . . so given the same set of ints as I just used above, with one change :

1,2,32,33,45,766,12,2,7,-1


would that series return true or false??

Don't write code - just look at the series with your eyes.
Avatar of gudii9

ASKER

public boolean has271(int[] nums) {
if(nums.length>2){

for(int i=0;i<nums.length-1;i++)
{
if((nums[i+1]==(nums[i]+5))&& (Math.abs(nums[i]-nums[i+2])<=3)) 
{
return true;
}

Open in new window


}
}
return false;
}

i kind of modified and failing in 2 instead
Expected	Run		
has271({1, 2, 7, 1}) → true	true	OK	    
has271({1, 2, 8, 1}) → false	false	OK	    
has271({2, 7, 1}) → true	true	OK	    
has271({3, 8, 2}) → true	true	OK	    
has271({2, 7, 3}) → true	true	OK	    
has271({2, 7, 4}) → false	true	X	    
has271({2, 7, -1}) → true	true	OK	    
has271({2, 7, -2}) → false	false	OK	    
has271({4, 5, 3, 8, 0}) → true	true	OK	    
has271({2, 7, 5, 10, 4}) → true	true	OK	    
has271({2, 7, -2, 4, 9, 3}) → true	true	OK	    
has271({2, 7, 5, 10, 1}) → false	true	X	    
has271({2, 7, -2, 4, 10, 2}) → false	false	OK	   

Open in new window

Avatar of gudii9

ASKER

The SECOND int has to be EXACTLY equal to the first int + 5.
100% clear
then the third int has to be equal to the first int -1, OR equal to the first int -1,GIVE OR TAKE 2.
still clear 50%
Avatar of gudii9

ASKER

In this series of ints:

1,2,32,33,45,766,12,2,7,1

only the last 3 ints will pass the test and return true. There are 8, repeat 8 sets of possible 3 ints you have to evaluate :

1,2,32
2,32,33
32,33,45
33,45,766
45,766,12
766,12,2
12,2,7
and
2,7,1.
100% clear
Avatar of gudii9

ASKER

. . . . so given the same set of ints as I just used above, with one change :

1,2,32,33,45,766,12,2,7,-1


would that series return true or false??

based on the challenge it should be true

followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.
above highlighted one is in 2,7,1 or the one italicized above?

basically 3rd=2nd plus/minus atmost 2(or may be 3) right?
>>basically 3rd=2nd plus/minus atmost 2(or may be 3) right? << NO!

2nd = 1st +5. End of story.

3rd = (1st-1) give or take 2.
>>based on the challenge it should be true<<

Correct.
Avatar of gudii9

ASKER

3rd = (1st-1) give or take 2.

Open in new window


i got it

public boolean has271(int[] nums) {
if(nums.length>2){

for(int i=0;i<nums.length-2;i++)
{
if((nums[i+1]==(nums[i]+5))&& (Math.abs(nums[i+2]-(nums[i]-1))<=2)) 
{
return true;
}

}
}
return false;
}

Open in new window


above passed fine

if i think pattern numbers as x y z
y=x+5
z=(x-1) plus or minus 2
followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.

 above highlighted one is in 2,7,1 or the one italicized above?

I am having trouble getting my head around what you are asking here. But let me try :
"followed by the value minus 1" means : an int in the 3rd position which is equal to the 1st int -1.
" Additionally the 271 counts even if the "1" differs by 2 or less from the correct value" means : the int in the position that the 1 is in, in the 271 above, should return true EVEN IF the value in that position (always the third position of the 3 ints) is "OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int" - in other words, the 2 in the 271 above.
That sounds about right now.

See how much shorter your code is now you understand the challenge. :) Well done.
Avatar of gudii9

ASKER

int in the position that the 1 is in


challenge should have mentioned as above instead of saying "1"

 "OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int"

Open in new window


i was thinking above should be as below instead
 "OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int - 1"

Open in new window

please correct me if i am wrong
Right - I made a mistake, which in my defence is hardly surprising amidst all this narrative.

So

codingbat means "the 1st int" - in other words, the 2 in the 271 above.

should indeed read :

codingbat means "the 1st int" - in other words, the 2 in the 271 above - BUT MINUS ONE IOW, whatever the first int is, but minus 1.

Anyway, you understood it all already, so don't start overthinking things now. Your own xyz explanation was what did the trick for you I think.
Avatar of gudii9

ASKER

trick is experts advise.