Advertisement
Guest User

Untitled

a guest
May 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.18 KB | None | 0 0
  1. import java.io.InputStreamReader;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class JollyRancher {
  6.     public static void main(String[] args) throws Exception {
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.  
  9.         String line = br.readLine();
  10.         while (line != null) {
  11.             String[] input = line.split(" ");
  12.             int[] input1 = new int[input.length - 1];
  13.             int n = Integer.parseInt(input[0]);
  14.             for (int i = 1; i < input.length; i++) {
  15.                 input1[i - 1] = Integer.parseInt(input[i]);
  16.             }
  17.             if (n != 1) {
  18.                 System.out.println(jollyRancher((input1), n));
  19.             } else {
  20.                 System.out.println("Jolly");
  21.             }
  22.             line = br.readLine();
  23.         }
  24.     }
  25.  
  26.     private static String jollyRancher(int[] input1, int n) {
  27.         HashSet<Integer> nums = new HashSet<Integer>();
  28.         for (int i = 1; i < n; i++) {
  29.             nums.add(i);
  30.         }
  31.         for (int i = 0; i < input1.length; i++) {
  32.             try {
  33.                 int val = Math.abs(input1[i - 1] - input1[i]);
  34.                 nums.remove(val);
  35.                 if (val < 1 || val > n - 1) {
  36.                     return "Not jolly";
  37.                 }
  38.             } catch (ArrayIndexOutOfBoundsException e) {
  39.  
  40.             }
  41.         }
  42.         if (nums.isEmpty()) {
  43.             return "Jolly";
  44.         } else {
  45.             return "Not jolly";
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement