4 kyu

Smallest possible sum

1,393 of 8,042dkhaburdzania

Description:

Description

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:

X_1 = [6, 9, 12] # -> X_1[2] = X[2] - X[1] = 21 - 9
X_2 = [6, 9, 6]  # -> X_2[2] = X_1[2] - X_1[0] = 12 - 6
X_3 = [6, 3, 6]  # -> X_3[1] = X_2[1] - X_2[0] = 9 - 6
X_4 = [6, 3, 3]  # -> X_4[2] = X_3[2] - X_3[1] = 6 - 3
X_5 = [3, 3, 3]  # -> X_5[1] = X_4[0] - X_4[1] = 6 - 3

The returning output is the sum of the final transformation (here 9).

Example

solution([6, 9, 21]) #-> 9
(solution [6 9 21]) ;-> 9
(solution (list 6 9 21)) ;-> 9
Solution([]int{6,9,21}) //-> 9

Solution steps:

[6, 9, 12] #-> X[2] = 21 - 9
[6, 9, 6] #-> X[2] = 12 - 6
[6, 3, 6] #-> X[1] = 9 - 6
[6, 3, 3] #-> X[2] = 6 - 3
[3, 3, 3] #-> X[1] = 6 - 3
[6 9 12] ;-> X[2] = 21 - 9
[6 9 6] ;-> X[2] = 12 - 6
[6 3 6] ;-> X[1] = 9 - 6
[6 3 3] ;-> X[2] = 6 - 3
[3 3 3] ;-> X[1] = 6 - 3
[6, 9, 12] //-> X[2] = 21 - 9
[6, 9, 6] //-> X[2] = 12 - 6
[6, 3, 6] //-> X[1] = 9 - 6
[6, 3, 3] //-> X[2] = 6 - 3
[3, 3, 3] //-> X[1] = 6 - 3

Additional notes:

There are performance tests consisted of very big numbers and arrays of size at least 30000. Please write an efficient algorithm to prevent timeout.

Algorithms
Mathematics
Arrays

Stats:

CreatedFeb 8, 2014
PublishedFeb 8, 2014
Warriors Trained32732
Total Skips8507
Total Code Submissions62496
Total Times Completed8042
Ruby Completions294
JavaScript Completions1393
Python Completions3489
PHP Completions255
Crystal Completions22
C++ Completions1175
Kotlin Completions250
Haskell Completions243
Clojure Completions82
Racket Completions29
Go Completions378
C Completions518
COBOL Completions8
Rust Completions216
D Completions10
TypeScript Completions163
Scala Completions25
CoffeeScript Completions10
Total Stars826
% of votes with a positive feedback rating84% of 942
Total "Very Satisfied" Votes707
Total "Somewhat Satisfied" Votes177
Total "Not Satisfied" Votes58
Total Rank Assessments10
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • dkhaburdzania Avatar
  • jhoffner Avatar
  • Unnamed Avatar
  • GiacomoSorbi Avatar
  • raulbc777 Avatar
  • donaldsebleung Avatar
  • docgunthrop Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • Cyhan Avatar
  • rowcased Avatar
  • kdmatrosov Avatar
  • Nightingale Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • cliffstamp Avatar
  • user8436785 Avatar
  • gargamel Avatar
  • dolamroth Avatar
  • akar-0 Avatar
  • sid114 Avatar
  • KayleighWasTaken Avatar
  • HartlIKS Avatar
Ad