Create a program to calculate the greatest common divisor (GCD) of an array of integers.
HardProblem Statement
Create a program to calculate the greatest common divisor (GCD) of an array of integers.
Sample Input
A hardcoded array of integers, e.g., [12, 15, 18].
Sample Output
GCD of the array: 3
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
fun gcd(a: Int, b: Int): Int {
return if (b == 0) a else gcd(b, a % b)
}
fun gcdOfArray(arr: Array<Int>): Int {
return arr.reduce { acc, num -> gcd(acc, num) }
}
fun main() {
val arr = arrayOf(12, 15, 18)
println("GCD of the array: ${gcdOfArray(arr)}")
}
Input
A hardcoded array of integers, e.g., [12, 15, 18].
Expected Output
GCD of the array: 3