Jolly Jumpers problem, problem getting input

I am trying to solve this problem called "Jolly Jumpers" from programming-challenges.com. I think most of you are familiar with it. The problem goes like this:


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.

Input

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

Output

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

Jolly
Not jolly


Here's what I arrived at:

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
#include <iostream>
#include <cmath>

using namespace std;

bool isItJolly( int diff[], int n ) {
	for ( int i = 0; i < n - 1; i++ ) {
		if ( diff[ i ] == 0 || diff[ i ] >= n )
			return false;
	}
	return true;
}

int main() {
	int n, num[3000], i = 0, difference[3000];
	
	while ( cin ) {
		cin >> n;
		while ( i < n ) {
			cin >> num[ i ];
			i++;
		}

		for ( int j = 1; j < n; j++ ) 
			difference[ j - 1 ] = fabs( num[ j - 1 ] - num[ j ] );
			
		if ( isItJolly( difference, n ) )
			cout << "Jolly\n";
		
		else
			cout << "Not jolly\n";
	}
	
	return 0;
}


I tried to input the values in the problem and the results look like this:
4 1 4 2 3
Jolly
5 1 4 2 -1 6
Jolly
Jolly
Not jolly
Jolly
Jolly



The results from the first line of input was correct but it appears immediately after I enter the values. Then I have to enter the next line of input and it produces 5 results.

Then, I tried entering the previous 2nd line of input first, it looked like this:

5 1 4 2 -1 6
Not jolly
4 1 4 2 3
Jolly
Jolly
Jolly
Not jolly
Not jolly



It produced correct results again for the first line of input but on the 2nd line printed more than one result. So probably, the problem occurs from getting the input.

Also, the program does not end properly, as it still asks for input and you have to type CTRL+C to end it. Will the online judge of programming-challenges.com accept it?

How do I address this problems? What's the most efficient way to solve it? And will the online judge accept it even if the result comes right after entering each line of input? Thanks!
Use while(cin>>n) The input will end when reaching eof (linux <C-D>)
I suppose that they will redirect stdin to a file.

About your issue, you are not restarting 'i'
I'm not sure how your code checks for 'jolliness'. You're just checking if every distance is between 1 and n-1, which is part of it, but not all.

ne555 (2421) Feb 22, 2012 at 11:31pm

Use while(cin>>n) The input will end when reaching eof (linux <C-D>)
I suppose that they will redirect stdin to a file.

About your issue, you are not restarting 'i'


Thanks! Restarting 'i' worked. Judge didn't accept it though



Gaminic (1026) Feb 22, 2012 at 11:39pm

I'm not sure how your code checks for 'jolliness'. You're just checking if every distance is between 1 and n-1, which is part of it, but not all.


Oh, thanks for pointing that out. I missed the part that checks if all the difference takes on all possible values within the range. Is there a C++ function that checks if a value is already previously contained in an array?
Topic archived. No new replies allowed.