Skip to content

Instantly share code, notes, and snippets.

Last active Dec 30, 2015
What would you like to do?
UVA - 10038 - Jolly Jumpers

Problem E: Jolly Jumpers

A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance,

1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.


Each line of input contains an integer n <= 3000 followed by n integers representing the sequence.


For each line of input, generate a line of output saying "Jolly" or "Not jolly".

Sample Input

4 1 4 2 3
5 1 4 2 -1 6

Sample Output

Not jolly
import java.util.*;
import java.lang.*;
class Main
public static void main (String[] args) throws java.lang.Exception
Scanner in = new Scanner(;
int N = in.nextInt(), sum = N*(N-1)/2;
int n = in.nextInt();
for(int d, i=1; i<N; i++, sum-=d){
d = Math.abs(n-(n=in.nextInt()));
d = d==0? -1<<20 : d;
System.out.println(sum==0 ? "Jolly" : "Not jolly");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment