271 challenge

Hi

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

I was not sure on how to proceed. please advise
LVL 7
gudii9Asked:
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.

ozoCommented:
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
0
gudii9Author Commented:
1, 6, 0 also valid right?(as it says ---->>pattern -- a value, followed by the value plus 5, followed by the value minus 1)
0
gudii9Author Commented:
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
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

ozoCommented:
1, 6, 0 also valid right?
I believe so
0
gudii9Author Commented:
please advise on how should i start thinking about this challenge and proceed?
0
gudii9Author Commented:
Please advise
0
krakatoaCommented:
I think you have got to try a few moves and ideas first.
0
gudii9Author Commented:
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
0
ozoCommented:
Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?
0
krakatoaCommented:
Your code needs to mirror what the question is asking for. Here is code that works, but without the key pieces in place. The logic flow is more important than the key pieces right now, so can you describe to us what the aim of the code below is? Once you've done that, you can fill in the "xxx"s and tell us what the missing values should be:

public boolean has271(int[] nums) {
 for(int i=0;i<xxxxxxxxxxxxx;i++){
 
 if(nums[i+1]==xxxxxxxxx)
 
   if (Math.xxx((numsxxxxx)-nums[i+2])<=x){return true;}
 
 }
 return false;
}

Open in new window

0

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
gudii9Author Commented:
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

0
gudii9Author Commented:
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
0
ozoCommented:
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).
0
krakatoaCommented:
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.
0
gudii9Author Commented:
still thinking. Seems bit challenging example
0
krakatoaCommented:
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.
0
gudii9Author Commented:
i need to come up some example based on above notes
0
krakatoaCommented:
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.
0
gudii9Author Commented:
this challenge is going over my head. I will close this and try later.
0
ozoCommented:
For this challenge, you probably shouldn't worry too much about using more cases than you need to.
0
gudii9Author Commented:
sure. Let me rethink again and again and come up with solution
0
gudii9Author Commented:
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

0
gudii9Author Commented:
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
0
ozoCommented:
Do you understand why those 4 test cases should return true?
If so, can you add conditions to handle them?
0
krakatoaCommented:
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
0
gudii9Author Commented:
sure i will try
0
krakatoaCommented:
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.
0
gudii9Author Commented:
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.
0
gudii9Author Commented:
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.
0
gudii9Author Commented:
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
0
krakatoaCommented:
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.
0
krakatoaCommented:
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.
0
gudii9Author Commented:
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
0
gudii9Author Commented:
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
0
gudii9Author Commented:
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
0
gudii9Author Commented:
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
0
krakatoaCommented:
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));
0
krakatoaCommented:
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.
0
gudii9Author Commented:
let me try.
0
gudii9Author Commented:
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
0
gudii9Author Commented:
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
0
gudii9Author Commented:
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

0
krakatoaCommented:
({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.
0
krakatoaCommented:
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.
0
gudii9Author Commented:
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.
0
gudii9Author Commented:
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
0
krakatoaCommented:
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.
0
krakatoaCommented:
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.
0
krakatoaCommented:
. . . . 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.
0
gudii9Author Commented:
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

0
gudii9Author Commented:
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%
0
gudii9Author Commented:
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
0
gudii9Author Commented:
. . . . 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?
0
krakatoaCommented:
>>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.
0
krakatoaCommented:
>>based on the challenge it should be true<<

Correct.
0
gudii9Author Commented:
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
0
krakatoaCommented:
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.
0
krakatoaCommented:
That sounds about right now.

See how much shorter your code is now you understand the challenge. :) Well done.
0
gudii9Author Commented:
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
0
krakatoaCommented:
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.
0
gudii9Author Commented:
trick is experts advise.
0
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.