java array 实在太难

太痛苦了 这一题写了一上午还是有问题 各位帮忙看看哇
0基础刚入门两周 写了一上午真的崩溃

img

import java.util.Scanner;
public class PernutationProblem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n <= 1) {
            System.out.println("Error in input");
            return;
        }
        int[] mask = new int[n - 1];
        int x1 = sc.nextInt();
        int j = 0;
        while (j < n - 1)
        {
            if (x1 < 0) {
                System.out.println("Invalid input");
                return;
            }
            int x2 = sc.nextInt();
            mask[x1 > x2? x1-x2-1 : x2-x1-1] = 1;
            x1 = x2;
            j++;
        }
        for (int i = 0; i < n - 1; i++)
        {
            if (mask[i] == 0)
            {
                System.out.println("It is not a Magical Series");
                return;
            }
        }
        System.out.println("It is a Magical Series");
    }
}

img