Jolly Jumper Sequence

04 Jul, 2022
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 3
Output: True
This sequence 1 4 2 3 is Jolly Jumper because
the absolute differences are 3, 2, and 1.

Input: 1 4 2 -1 6  
Output: 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 6
Output: True
The idea is to maintain a boolean array to store set of absolute difference of successive elements. 
If absolute difference between two elements is more than n-1 or 0, return false. If an absolute difference repeated, then all absolute differences from 1 to n-1 can’t be present (Pigeon Hole Principle), return false.
If absolute difference between two elements is more than n-1 or 0, return false. 
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. 
C++
Java
Python3
C#
Javascript
// Program for Jolly Jumper Sequence#include<bits/stdc++.h>using namespace std; // Function to check whether given sequence is// Jolly Jumper or notbool 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 Codeint 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;}
// Program for Jolly Jumper Sequenceimport java.util.*; class GFG{ // Function to check whether given sequence// is Jolly Jumper or notstatic boolean isJolly(int a[], int n){    // Boolean vector to diffSet set of differences.    // The vector is initialized as false.    boolean []diffSet = new boolean[n];     // Traverse all array elements    for (int i = 0; i < n - 1 ; i++)    {        // Find absolute difference        // between current two        int d = Math.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 Codepublic static void main(String[] args){    int a[] = {11, 7, 4, 2, 1, 6};    int n = a.length;    if(isJolly(a, n))        System.out.println("Yes");    else        System.out.println("No");}} // This code is contributed by Rajput-Ji
# Python3 Program for Jolly Jumper# Sequence # Function to check whether given# sequence is Jolly Jumper or notdef 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 Codea = [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
// Program for Jolly Jumper Sequenceusing System; class GFG{ // Function to check whether given sequence// is Jolly Jumper or notstatic Boolean isJolly(int []a, int n){    // Boolean vector to diffSet set of differences.    // The vector is initialized as false.    Boolean []diffSet = new Boolean[n];     // Traverse all array elements    for (int i = 0; i < n - 1 ; i++)    {        // Find absolute difference        // between current two        int d = Math.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 Codepublic static void Main(String[] args){    int []a = {11, 7, 4, 2, 1, 6};    int n = a.Length;    if(isJolly(a, n))        Console.WriteLine("Yes");    else        Console.WriteLine("No");}} // This code is contributed by PrinciRaj1992
<script> // Javascript program for Jolly Jumper Sequence // Function to check whether given sequence is// Jolly Jumper or notfunction isJolly(a, n){         // Boolean vector to diffSet set of    // differences. The vector is    // initialized as false.    let diffSet = new Array(n).fill(false);     // Traverse all array elements    for(let i = 0; i < n - 1; i++)    {                 // Find absolute difference between        // current two        let d = Math.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 Codelet a = [ 11, 7, 4, 2, 1, 6 ];let n = a.length; isJolly(a, n) ? document.write("Yes") :                document.write("No");                 // This code is contributed by _saurabh_jaiswal </script>
Output: 
Yes
Time Complexity: O(n)
This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Rajput-Ji
princiraj1992
_saurabh_jaiswal
hardikkoriintern
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Chocolate Distribution Problem
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Find subarray with given sum | Set 1 (Nonnegative Numbers)
Move all negative numbers to beginning and positive to end with constant extra space
Count pairs with given sum