Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a program to calculate the greatest common divisor (GCD) of an array of integers.

Hard

Problem 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)}") }
Log in or sign up to run or submit your code.Log inSign up
Input
A hardcoded array of integers, e.g., [12, 15, 18].
Expected Output
GCD of the array: 3