I have posted my solution to the jolly jumpers programming challenge(detailed here) for your review:
A sequence of n > 0 integers is called a jolly jumper if the absolute values of the differences between successive elements take on all possible values 1 through n - 1. For instance,
1 4 2 3
is a jolly jumper, because the absolute differences are 3, 2, and 1, respectively. The definition implies that any sequence of a single integer is a jolly jumper. Write a program to determine whether each of a number of sequences is a jolly jumper.
Input
Each line of input contains an integer n < 3, 000 followed by n integers representing the sequence.
Output
For each line of input generate a line of output saying
Jolly
orNot jolly
.Sample Input
4 1 4 2 3 5 1 4 2 -1 6
Sample Output
Jolly Not jolly
//platform specific code
#ifdef WINDOWS
#include "stdafx.h"
#endif // WINDOWS
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::vector;
const int MAX_SEQUENCE_LENGTH = 3000;
int main()
{
bool not_jolly;
vector<int> sequence(MAX_SEQUENCE_LENGTH, 0);
vector<bool> in_sequence(MAX_SEQUENCE_LENGTH, false);
int sequence_length;
while (cin >> sequence_length) {
not_jolly = false;
std::fill(sequence.begin(), sequence.end(), 0);
std::fill(in_sequence.begin(), in_sequence.end(), false);
for (int i = 0;i < sequence_length; i++) {
cin >> sequence[i];
}
for (int i = 1;i < sequence_length; i++) {
in_sequence[abs(sequence[i] - sequence[i - 1])] = true;
}
for (int i = 1;i < sequence_length;i++) {
if (!in_sequence[i]) {
not_jolly = true;
break;
}
}
if (not_jolly == false) {
cout << "Jolly" << "\n";
} else {
cout << "Not jolly" << "\n";
}
}
}
in_sequence
to be the same size as the maximum sequence length, but use (the absolute value of) a difference between inputs as an index into it. Consider an input of2 0 5000
. This will attempt to write toin_sequence[5000]
, which doesn't exist. The description saysn
will be less than 3000, but doesn't place that restriction on the range of the other inputs. \$\endgroup\$ – Jerry Coffin Nov 4 '16 at 16:02