-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsecutivePairs.java
More file actions
33 lines (27 loc) · 983 Bytes
/
ConsecutivePairs.java
File metadata and controls
33 lines (27 loc) · 983 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
29
30
31
32
33
// Simple consecutive pairs
// DESCRIPTION:
// In this Kata your task will be to return the count of pairs that have consecutive numbers as follows:
// pairs([1,2,5,8,-4,-3,7,6,5]) = 3
// The pairs are selected as follows [(1,2),(5,8),(-4,-3),(7,6),5]
// --the first pair is (1,2) and the numbers in the pair are consecutive; Count = 1
// --the second pair is (5,8) and are not consecutive
// --the third pair is (-4,-3), consecutive. Count = 2
// --the fourth pair is (7,6), also consecutive. Count = 3.
// --the last digit has no pair, so we ignore.
// More examples in the test cases.
// Good luck!
// Please also try Simple time difference
import java.util.Arrays;
import java.lang.Math;
class Solution{
public static int solve(int [] arr){
int consecutive = 0;
for(int i = 0; i < arr.length-1; i+=2){
int result = Math.abs(arr[i]-arr[i+1]);
if(result == 1){
consecutive++;
}
}
return consecutive;
}
}