C PROGRAM. Multi-Jolly Jumper A sequence of n>0 integers is called a jolly jumpe
ID: 3734479 • Letter: C
Question
C PROGRAM. Multi-Jolly Jumper
A sequence of n>0 integers is called a jolly jumper if the absolute values of the dierences between successive elements are in ascending or descending order. For instance,
1 4 2 3
is a jolly jumper, because the absolute differences are in descending order 3, 2, and 1, respectively. The denition 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. Another example:
1 3 6 10
Is a jolly jumper, because the absolute differences are in ascending order: 2,3, and 4
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” or “Not jolly”.
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” or “Not jolly”. If Jolly, indicate if it is in ascending or descending order.
Sample Input
4 1 4 2 3
5 1 4 2 -1 6
4 1 3 6 10
Sample Output
Jolly descending
Not jolly
Jolly ascending
Explanation / Answer
Hi Student,
Please find the solution below :
#include <stdio.h>
void checkJolly(int arr[], int n) {
int temp[n];
temp[0] = arr[0];
int less=0,greater =0,i,val;
for( i = 1 ; i<n; i++){
val = arr[i]-arr[i-1];
if(val <0)
temp[i] = -val;
else
temp[i]=val;
}
for(int i=2;i<n; i++){
if(temp[i-1] < temp[i])
less++;
else
greater++;
}
if(greater!=0 && less!=0){
printf("Not Jolly");
}
else if(greater!=0){
printf("Jolly Descending");
}
else{
printf("Jolly Ascending");
}
}
int main(void) {
int arr[3000];
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
checkJolly(arr,n);
return 0;
}
If you like this answer, give a thumbs up! Happy learning :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.