Search This Blog

Saturday, 1 January 2022

Title: Implementation of jolly jumper sequence problem.

Title: Implementation of jolly jumper sequence problem.
Aim: - To study the jolly jumper sequence problem using an array.
Theory:-

            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 absolute 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


Program:

#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];
cout<<"Enter Number Of Elements :";
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<<"It is Jolly.";
else
cout<<"It is not Jolly";
}
return 0;
}

Output:





No comments:

Post a Comment

Search This Blog

Contact Form

Name

Email *

Message *

Popular Posts