Advertisement
Guest User

Untitled

a guest
May 24th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.04 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.             System.out.println(jollyRancher((input1), n));
  18.             line = br.readLine();
  19.         }
  20.     }
  21.  
  22.     private static String jollyRancher(int[] input1, int n) {
  23.         HashSet<Integer> nums = new HashSet<Integer>();
  24.         for (int i = 1; i < n; i++) {
  25.             nums.add(i);
  26.         }
  27.         for (int i = 0; i < input1.length; i++) {
  28.             try {
  29.                 int val = Math.abs(input1[i - 1] - input1[i]);
  30.                 nums.remove(val);
  31.             } catch (ArrayIndexOutOfBoundsException e) {
  32.  
  33.             }
  34.         }
  35.         if (nums.isEmpty()) {
  36.             return "Jolly";
  37.         } else {
  38.             return "Not jolly";
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement