UVA 10038 – Jolly Jumpers

Easy problem. Take all differences, put them in a HashSet and then check if all the requred numbers are in the HashSet or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.StringTokenizer;

/**
 *
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 *
 */
public class P10038 {
  public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);
    StringTokenizer st;

    String line = null;
    while ((line = br.readLine()) != null) {
      st = new StringTokenizer(line.trim());
      int N = Integer.parseInt(st.nextToken());
      if (N == 1) {
        out.println("Jolly");
      } else {
        HashSet<Integer> ans = new HashSet<Integer>();
        int last = 0;
        for (int i = 0; i < N; i++) {
          if (i == 0) {
            last = Integer.parseInt(st.nextToken());
          } else {
            int tmp = Integer.parseInt(st.nextToken());
            ans.add(Math.abs(tmp - last));
            last = tmp;
          }
        }

        if (ans.size() != N - 1) {
          out.println("Not jolly");
        } else {
          boolean found = true;
          for (int i = 1; i < N; i++) {
            if (!ans.contains(i)) {
              found = false;
              break;
            }
          }
          if (found) {
            out.println("Jolly");
          } else {
            out.println("Not jolly");
          }
        }
      }
    }
    out.close();
    br.close();
  }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.