# GCD Greatest Common Divisor

```java
package GCD;

import java.awt.*;

/**
 * Created by yang on 1/17/17.
 */
public class Solution {
    public static int gcd(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int gcd = array[0];
        for (int i = 1; i < array.length; i++) {
            gcd = helper(gcd, array[i]);
        }

        return gcd;
    }

    private static int helper(int num1, int num2) {
        if (num1 == 0 || num2 == 0) {
            return 0;
        }

        while (num1 != 0 && num2 != 0) {
            if (num2 > num1) {
                num1 ^= num2;
                num2 ^= num1;
                num1 ^= num2;
            }
            int tmp = num1 % num2;
            num1 = num2;
            num2 = tmp;
        }

        return num1 + num2;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{64, 8, 16, 32, 128};
        System.out.println(gcd(arr));
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/amazon/gcd-greatest-common-divisor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
