题目描述
一个nn个元素的整数数组,如果数组两个连续元素之间差的绝对值包括了[1,n-1][1,n−1]之间的所有整数,则称之符合“欢乐的跳”,如数组1 4 2 31423符合“欢乐的跳”,因为差的绝对值分别为:3,2,13,2,1。
给定一个数组,你的任务是判断该数组是否符合“欢乐的跳”。
输入格式
每组测试数据第一行以一个整数n(1≤n≤1000)开始,接下来n个空格隔开的在[-10^8−10 ^8, 10^810 8 ]之间的整数。
输出格式
对于每组测试数据,输出一行若该数组符合“欢乐的跳”则输出"Jolly",否则输出"Not jolly"。
输入输出样例
输入
4 1 4 2 3
输出
Jolly
输入
5 1 4 2 -1 6
输出
Not jolly
说明/提示
1≤n≤1000
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < n - 1; i++) {
int temp = Math.abs(arr[i] - arr[i+1]);
min = Math.min(temp, min);
max = Math.max(temp, max);
}
if(min < 1 || max > n - 1) System.out.println("Not jolly");
else System.out.println("Jolly");
}
}