-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHAS271.java
More file actions
28 lines (24 loc) · 725 Bytes
/
HAS271.java
File metadata and controls
28 lines (24 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
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
*/
package school;
public class HAS271 {
public static void main(String[] args){
int[] a = {1,2,7,1};
boolean answer = has271(a);
System.out.println(answer);
}
public static boolean has271(int[] nums) {
for(int i=0; i<nums.length-2; i++){
int a = nums[i];
if((nums[i+1]==a+5) && (Math.abs(nums[i+2]-(a-1))<=2)){
return true;
}
}return false;
}
}