Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created January 13, 2016 11:23
Show Gist options
  • Save sudipto80/9d728b3bd43fec614444 to your computer and use it in GitHub Desktop.
Save sudipto80/9d728b3bd43fec614444 to your computer and use it in GitHub Desktop.
Jolly Jumper
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int jolly( int nums[], int length)
{
//Pre-conditions
if(length <= 0 )
return 0;
int i;
int output[length];
for (i = 1; i <= length - 1; i++)
{
int current = abs(nums[i] - nums[i - 1]);
//If consecutive numbers are same
//The sequence can't be jolly.
//If the subtraction result fall beyond
//the last acceptable value of the range
//1 to n-1 then the sequence can't be jolly
//we needn't look any further.
if (current == 0 || current > length)
return 0;
else
output[current] = 1;
}
for (i = 1; i < length; i++)
{
if (output[i] != 1)
return 0;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment