Given an array X of positive integers, its elements are to be transformed by running the following operation on them as many times as required:
if X[i] > X[j] then X[i] = X[i] - X[j]
When no more transformations are possible, return its sum (“smallest possible sum”).
For instance, the successive transformation of the elements of input X = [6, 9, 21] is detailed below:
The returning output is the sum of the final transformation (here 9).
There are performance tests consisted of very big numbers and arrays of size at least 30000. Please write an efficient algorithm to prevent timeout.
The solution in Kotlin
Test cases to validate our solution