Friday, September 28, 2007

Jolly Jumpers

Corrected & Accepted !!! (Thanks to xanden for his comments) 
____________________________________________________________________
Question: Jolly Jumpers
____________________________________________________________________

#include "iostream.h"


int main()
{
int n, n1, n2, arr[3001], diff, flag;
while(cin >> n) {
for(int i = 1; i < n; i++) arr[i] = 0;
cin >> n1;
flag = 0;
for(int i = 1; i < n; i++) {
cin >> n2;
diff = (n1 > n2)?(n1 - n2):(n2 - n1);
if(diff > 0
&& n > diff) {
if(arr[diff] != 1) arr[diff] = 1;
else flag = 1;
}
else flag = 1;
n1 = n2;
}
if(flag == 0) cout << "Jolly" << endl;
else cout << "Not jolly" << endl;
}
return 0;
}
____________________________________________________________________

2 comments:

Xander said...

you have a couple of errors... if(n < diff && diff > 0) should be if (diff < n && diff > 0)
also, you shouldn't break out of the loop if the same difference is found again because you still have remaining unread input (cin >> n2) for the current test case...

Dileep P G said...

Thanks a lot for that !!! i somehow never thought about 1 input set interfering with the next..

Contributors