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.
Below is the implementation based on above idea.
C++
#include<bits/stdc++.h>
using namespace std;
bool isJolly( int a[], int n)
{
vector< bool > diffSet(n, false );
for ( int i=0; i < n-1 ; i++)
{
int d = abs (a[i]-a[i+1]);
if (d == 0 || d > n-1 || diffSet[d] == true )
return false ;
diffSet[d] = true ;
}
return true ;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static boolean isJolly( int a[], int n)
{
boolean []diffSet = new boolean [n];
for ( int i = 0 ; i < n - 1 ; i++)
{
int d = Math.abs(a[i] - a[i + 1 ]);
if (d == 0 || d > n - 1 ||
diffSet[d] == true )
return false ;
diffSet[d] = true ;
}
return true ;
}
public 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" );
}
}
|
Python3
def isJolly(a, n):
diffSet = [ False ] * n
for i in range ( 0 , n - 1 ):
d = abs (a[i] - a[i + 1 ])
if (d = = 0 or d > n - 1 or diffSet[d] = = True ):
return False
diffSet[d] = True
return True
a = [ 11 , 7 , 4 , 2 , 1 , 6 ]
n = len (a)
print ( "Yes" ) if isJolly(a, n) else print ( "No" )
|
C#
using System;
class GFG
{
static Boolean isJolly( int []a, int n)
{
Boolean []diffSet = new Boolean[n];
for ( int i = 0; i < n - 1 ; i++)
{
int d = Math.Abs(a[i] - a[i + 1]);
if (d == 0 || d > n - 1 ||
diffSet[d] == true )
return false ;
diffSet[d] = true ;
}
return true ;
}
public 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" );
}
}
|
Javascript
<script>
function isJolly(a, n)
{
let diffSet = new Array(n).fill( false );
for (let i = 0; i < n - 1; i++)
{
let d = Math.abs(a[i] - a[i + 1]);
if (d == 0 || d > n - 1 ||
diffSet[d] == true )
return false ;
diffSet[d] = true ;
}
return true ;
}
let a = [ 11, 7, 4, 2, 1, 6 ];
let n = a.length;
isJolly(a, n) ? document.write( "Yes" ) :
document.write( "No" );
</script>
|
Output:
Yes
Time Complexity: O(n)
Auxiliary Space :O(n), since we used vector of size n.
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...