Problem:
Greatest Common Divisor

Write a program which finds the greatest common divisor of two natural numbers a and b

Input

a and b are given in a line sparated by a single space.

Output

Output the greatest common divisor of a and b.

Constrants

1 ≤ a, b ≤ 109

Hint

You can use the following observation:

For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y)

Sample Input 1

54 20

Sample Output 1

2

Sample Input 2

147 105

Sample Output 2

21

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

class Main {

	public static void main(String[] args) {

		Input stdIn = new Input();
		List<Integer> inputline = stdIn.getLine();
		int x = inputline.get(0);
		int y = inputline.get(1);

		List<Integer> xDivisor = new ArrayList<>();
		List<Integer> yDivisor = new ArrayList<>();

		for (int i = 1; i <= x; i++) {
			if (x % i == 0) {
				xDivisor.add(i);
			}
		}
		for (int i = 1; i <= y; i++) {
			if (y % i == 0) {
				yDivisor.add(i);
			}
		}
//				System.out.println(xDivisor);
//				System.out.println(yDivisor);

		for (int i = xDivisor.size() - 1; i > 0; i--) {
			for (int j = yDivisor.size() - 1; j > 0; j--) {
//				System.out.println("i:" + xDivisor.get(i));
//				System.out.println("j" + yDivisor.get(j));
				if (xDivisor.get(i).equals(yDivisor.get(j))) {
					System.out.println(yDivisor.get(j));
					System.exit(0);
				}
			}
		}
	}
}
class Input{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public Input() {
	}

	public int getValue() {
		int n = 0;
		try {
			n = Integer.parseInt(br.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return n;
	}

	public List<Integer> getLine() {
		try {
			String[] s = br.readLine().trim().split(" ");
			List<Integer> ary = new ArrayList<>();
			for (int i = 0; i < s.length; i++) {
				ary.add(Integer.parseInt(s[i]));
			}
			return ary;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void close() {
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}