UVA – 10783 – Odd Sum

  • ID: UVA – 10783 – Odd Sum
  • Difficulty : Easy
  • Type : Simple Math.
  • Description:-
    • could have iterated throw odd numbers within those constrains
    • but use the Arithematic Sequence Sum Law for O(1)

Arithematic Sequence Sum Law

  • Code:


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		int Case = 0, T = in.nextInt();
		while (T-- != 0)
			System.out.format("Case %d: %d\n", ++Case,
					sum(in.nextInt(), in.nextInt()));

	}

	public static int sum(int a, int b) {
		if (b == 0)
			return 0;
		return ((b + 1) / 2)*((b + 1) / 2) - (a / 2)*(a / 2);
	}
}
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: