-
Notifications
You must be signed in to change notification settings - Fork 40
/
is_jolly_jumper_sequence.c
69 lines (64 loc) · 1.65 KB
/
is_jolly_jumper_sequence.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Date: 2018-10-06
*
* Description:
* Check if an given unsorted array is jolly jumper sequence or not.
* An array having n elements is a jolly jumper sequence if absolute difference
* between adjacent elements ranges from 1 to n - 1, no difference repeated.
* For example - [1, 4, 2, 3], n = 4 is a jolly jumper sequence as absolute
* difference between adjacent elements are 1, 2 and 3.
*
* Approach:
* Iterate over array and find difference between adjacent elements, if it is
* 0, more than n - 1 or already found, break loop with failure message.
* Otherwise save this difference and check for next adjacent element.
*
* Complexity:
* O(N) Time and space
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int n = 0, diff = 0;
int *a = NULL, *map = NULL;
printf("Enter number of elements: ");
scanf("%d", &n);
a = (int *)malloc(sizeof(int) * n);
map = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) {
printf("Enter element[%d]: ", i);
scanf("%d",&a[i]);
map[i] = 0;
}
for (i = 0; i < n-1; i++) {
diff = a[i + 1] - a[i];
diff = diff > 0 ? diff : -diff;
// If diff is 0, more than n - 1 or already found, it's not a jolly jumper
// sequence.
if (!diff || diff > n - 1 || map[diff]) {
printf("NO\n");
return -1;
}
else
map[diff] = 1;
}
printf("YES\n");
return 0;
}
/*
* Output:
* ------------------------
* Enter number of elements: 4
* Enter element[0]: 1
* Enter element[1]: 4
* Enter element[2]: 2
* Enter element[3]: 3
* YES
*
* Enter number of elements: 3
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 3
* NO
*/