UVA – 10038 – Jolly Jumpers

  • ID : UVA – 10038 – Jolly Jumpers
  • Submissions :
    • Java – Accepted
  • Difficulty : Easy
  • Type : Simple Algebra
  • Time for Submission : 30 minutes
  • Solution Description :
    • keep track of the two current nodes so far.
    • calculate the abs difference and check weather it existed before or not.
    • then mark a flag that will indicate the selected sequence as a spoiled not Jolly’s Sequence
  • Problems :
    • watch out the outbounded index may leak through the short-circuit Or.
    • so I added the else statement to prevent it from passing through the assignment statement ( vis[dif]=true; )only if it succeeded the bound check in the previous if.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N,dif,prev,cur = 0;

		while(in.hasNext()){
			N =in.nextInt();
			cur = in.nextInt();
			boolean[] vis = new boolean[N--];
			boolean good = true;

			while(N--!=0){
				prev = cur;
				cur = in.nextInt();
				dif =Math.abs(cur-prev);
				if(dif==0 || dif >=vis.length || vis[dif])
					good = false;
				else
				vis[dif]=true;
			}
				System.out.println(good ? "Jolly" : "Not jolly");

		}

	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: