# Jolly Jumper Sequence

A sequence of n numbers (n < 3000) is called **Jolly Jumper** if the absolute values of the differences between the successive elements take on all possible values from 1 through n-1. The definition implies that any sequence of a single integer is a jolly jumper.

Examples:

Input:1 4 2 3Output:True This sequence 1 4 2 3 is Jolly Jumper because the absolute differences are 3, 2, and 1.Input:1 4 2 -1 6Output:False The absolute differences are 3, 2, 3, 7. This does not contain all the values from 1 through n-1. So, this sequence is not Jolly.Input:11 7 4 2 1 6Output:True

The idea is to maintain a boolean array to store set of absolute difference of successive elements.

a) If absolute difference between two elements is more than n-1 or 0, return false.

b) If an absolute difference repeated, then all absolute differences from 1 to n-1 can’t be present (Pigeon Hole Principle), return false.

Below is the implementation based on above idea.

## CPP

`// Program for Jolly Jumper Sequence ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check whether given sequence is ` `// Jolly Jumper or not ` `bool` `isJolly(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Boolean vector to diffSet set of differences. ` ` ` `// The vector is initialized as false. ` ` ` `vector<` `bool` `> diffSet(n, ` `false` `); ` ` ` ` ` `// Traverse all array elements ` ` ` `for` `(` `int` `i=0; i < n-1 ; i++) ` ` ` `{ ` ` ` `// Find absolute difference between current two ` ` ` `int` `d = ` `abs` `(a[i]-a[i+1]); ` ` ` ` ` `// If difference is out of range or repeated, ` ` ` `// return false. ` ` ` `if` `(d == 0 || d > n-1 || diffSet[d] == ` `true` `) ` ` ` `return` `false` `; ` ` ` ` ` `// Set presence of d in set. ` ` ` `diffSet[d] = ` `true` `; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = {11, 7, 4, 2, 1, 6}; ` ` ` `int` `n = ` `sizeof` `(a)/ ` `sizeof` `(a[0]); ` ` ` `isJolly(a, n)? cout << ` `"Yes"` `: cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program for Jolly Jumper ` `# Sequence ` ` ` `# Function to check whether given ` `# sequence is Jolly Jumper or not ` `def` `isJolly(a, n): ` ` ` ` ` `# Boolean vector to diffSet set ` ` ` `# of differences. The vector is ` ` ` `# initialized as false. ` ` ` `diffSet ` `=` `[` `False` `] ` `*` `n ` ` ` ` ` `# Traverse all array elements ` ` ` `for` `i ` `in` `range` `(` `0` `, n` `-` `1` `): ` ` ` ` ` `# Find absolute difference between ` ` ` `# current two ` ` ` `d ` `=` `abs` `(a[i]` `-` `a[i ` `+` `1` `]) ` ` ` ` ` `# If difference is out of range or ` ` ` `# repeated, return false. ` ` ` `if` `(d ` `=` `=` `0` `or` `d > n` `-` `1` `or` `diffSet[d] ` `=` `=` `True` `): ` ` ` `return` `False` ` ` ` ` `# Set presence of d in set. ` ` ` `diffSet[d] ` `=` `True` ` ` ` ` `return` `True` ` ` `# Driver Code ` `a ` `=` `[` `11` `, ` `7` `, ` `4` `, ` `2` `, ` `1` `, ` `6` `] ` `n ` `=` `len` `(a) ` ` ` `print` `(` `"Yes"` `) ` `if` `isJolly(a, n) ` `else` `print` `(` `"No"` `) ` ` ` `# This code is contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

Output:

Yes

**Time Complexity:** O(n)

**References:**

http://users.csc.calpoly.edu/~jdalbey/301/Labs/JollyJumpers.html

This article is contributed by **Rahul Agrawal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array
- Form minimum number from given sequence
- Find bitonic point in given bitonic sequence
- Maximum sum Bi-tonic Sub-sequence
- Delete consecutive same words in a sequence
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- k-th missing element in increasing sequence which is not present in a given sequence
- Largest Derangement of a Sequence
- Longest sequence of positive integers in an array
- Check if any valid sequence is divisible by M
- Maximum product quadruple (sub-sequence of size 4) in array
- Find the lexicographical next balanced bracket sequence
- Queries on insertion of an element in a Bitonic Sequence
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]