Advertisement
Guest User

Untitled

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