Solved

# 271 challenge

Posted on 2014-11-09
85 Views
Hi

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

0
Question by:gudii9
• 34
• 21
• 6

LVL 84

Assisted Solution

ozo earned 200 total points
ID: 40431931
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

LVL 7

Author Comment

ID: 40433385
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

LVL 7

Author Comment

ID: 40433511
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

LVL 84

Expert Comment

ID: 40433986
1, 6, 0 also valid right?
I believe so
0

LVL 7

Author Comment

ID: 40435944
0

LVL 7

Author Comment

ID: 40438144
0

LVL 16

Expert Comment

ID: 40438237
I think you have got to try a few moves and ideas first.
0

LVL 7

Author Comment

ID: 40438630
``````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;
}
``````

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
``````

0

LVL 84

Expert Comment

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

LVL 16

Accepted Solution

krakatoa earned 300 total points
ID: 40438936
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;
}
``````
0

LVL 7

Author Comment

ID: 40441152
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;
}
``````

``````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
``````
0

LVL 7

Author Comment

ID: 40441165
``````has271({2, 7, 5, 10, 4}) â†’ true	false	X
has271({2, 7, -2, 4, 9, 3}) â†’ true	false	X
``````

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?
0

LVL 84

Expert Comment

ID: 40441181
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

LVL 16

Expert Comment

ID: 40441218
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

LVL 7

Author Comment

ID: 40444075
still thinking. Seems bit challenging example
0

LVL 16

Expert Comment

ID: 40444324
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

LVL 7

Author Comment

ID: 40455859
i need to come up some example based on above notes
0

LVL 16

Expert Comment

ID: 40456185
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

LVL 7

Author Comment

ID: 40457701
this challenge is going over my head. I will close this and try later.
0

LVL 84

Expert Comment

ID: 40457710
For this challenge, you probably shouldn't worry too much about using more cases than you need to.
0

LVL 7

Author Comment

ID: 40457720
sure. Let me rethink again and again and come up with solution
0

LVL 7

Author Comment

ID: 40463188
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;
}
``````

``````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
``````
0

LVL 7

Author Comment

ID: 40463214
``````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;
}
``````

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
``````
0

LVL 84

Expert Comment

ID: 40463619
Do you understand why those 4 test cases should return true?
If so, can you add conditions to handle them?
0

LVL 16

Expert Comment

ID: 40464044
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

LVL 7

Author Comment

ID: 40466758
sure i will try
0

LVL 16

Expert Comment

ID: 40466782
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

LVL 7

Author Comment

ID: 40467020
``````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
``````
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

LVL 7

Author Comment

ID: 40467027
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

LVL 7

Author Comment

ID: 40467088
``````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;
}
}
``````

0

LVL 16

Expert Comment

ID: 40467123
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

LVL 16

Expert Comment

ID: 40467136
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

LVL 7

Author Comment

ID: 40467723
``````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;
}
}
``````
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

or is 2 away from the first int-1 IN EITHER DIRECTION.
0

LVL 7

Author Comment

ID: 40467730
``````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
``````
i am failing some test cases as above
0

LVL 7

Author Comment

ID: 40467736
``````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;
}
}
``````

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
``````

0

LVL 7

Author Comment

ID: 40467740
``````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;
}
``````

0

LVL 16

Expert Comment

ID: 40468143
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

LVL 16

Expert Comment

ID: 40468162
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

LVL 7

Author Comment

ID: 40476126
let me try.
0

LVL 7

Author Comment

ID: 40492090
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
``````
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

LVL 7

Author Comment

ID: 40492097
``````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;
}
``````

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

LVL 7

Author Comment

ID: 40492104
``````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
``````
0

LVL 16

Expert Comment

ID: 40492134
({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

LVL 16

Expert Comment

ID: 40492165

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

LVL 7

Author Comment

ID: 40492190
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

LVL 7

Author Comment

ID: 40492198
``````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;
}
``````

i wrote as above still 5 failing
0

LVL 16

Expert Comment

ID: 40492213
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

LVL 16

Expert Comment

ID: 40492226
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

LVL 16

Expert Comment

ID: 40492236
. . . . 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

LVL 7

Author Comment

ID: 40492290
``````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;
}
``````

}
}
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
``````
0

LVL 7

Author Comment

ID: 40492293
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

LVL 7

Author Comment

ID: 40492299
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

LVL 7

Author Comment

ID: 40492305
. . . . 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

LVL 16

Expert Comment

ID: 40492368
>>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

LVL 16

Expert Comment

ID: 40492372
>>based on the challenge it should be true<<

Correct.
0

LVL 7

Author Comment

ID: 40492376
``````3rd = (1st-1) give or take 2.
``````

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

above passed fine

if i think pattern numbers as x y z
y=x+5
z=(x-1) plus or minus 2
0

LVL 16

Expert Comment

ID: 40492385
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

LVL 16

Expert Comment

ID: 40492393

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

LVL 7

Author Comment

ID: 40492417
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"
``````

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"
``````
please correct me if i am wrong
0

LVL 16

Expert Comment

ID: 40492473
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.

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

LVL 7

Author Comment

ID: 40493947
0

## Featured Post

### Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to thâ€¦
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.