From b768376ff074e019da356f86205e453c1649faed Mon Sep 17 00:00:00 2001 From: Loosetooth Date: Sun, 2 Feb 2025 23:52:26 +0100 Subject: [PATCH] first solution --- 5kyu/smallest-possible-sum/description.md | 26 ++++++++++ 5kyu/smallest-possible-sum/solution.ts | 58 +++++++++++++++++++++++ 5kyu/smallest-possible-sum/tests.ts | 8 ++++ 3 files changed, 92 insertions(+) create mode 100644 5kyu/smallest-possible-sum/description.md create mode 100644 5kyu/smallest-possible-sum/solution.ts create mode 100644 5kyu/smallest-possible-sum/tests.ts diff --git a/5kyu/smallest-possible-sum/description.md b/5kyu/smallest-possible-sum/description.md new file mode 100644 index 0000000..d8432ee --- /dev/null +++ b/5kyu/smallest-possible-sum/description.md @@ -0,0 +1,26 @@ +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 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 +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. \ No newline at end of file diff --git a/5kyu/smallest-possible-sum/solution.ts b/5kyu/smallest-possible-sum/solution.ts new file mode 100644 index 0000000..36eb1c7 --- /dev/null +++ b/5kyu/smallest-possible-sum/solution.ts @@ -0,0 +1,58 @@ +// idea: keep the array sorted +// whenever we execute the subtraction, place the result in the correct position + +/** + * Inserts the value in a sorted array to keep it sorted. + * The sort direction is assumed to be ascending. + * @param arr The sorted array + * @param value The value to insert + */ +const sortInsert = (arr: number[], value: number): void => { + let i = 0; + while (i < arr.length && arr[i] < value) { + i++; + } + + arr.splice(i, 0, value); +} + +/** + * Does a single operation on the given array. + * Keeps the array sorted. + * @returns True if the array is all the same, false otherwise. + */ +const operate = (arr: number[]): boolean => { + // last element is the largest + // find the first element that is not the largest + let i = arr.length - 1; + while (i >= 0 && arr[i] === arr[arr.length - 1]) { + i--; + } + + if (i < 0) { + // all elements are the same + return true; + } + + // subtract + const value = arr[arr.length - 1] - arr[i]; + + // remove the last element + arr.pop(); + + // insert the value in the correct position + sortInsert(arr, value); + + return false; +} + +export function solution(numbers: number[]): number { + numbers.sort((a, b) => a - b); + let done = false; + while (!done) { + done = operate(numbers); + } + + // sum the numbers + return numbers.reduce((acc, num) => acc + num, 0); +} \ No newline at end of file diff --git a/5kyu/smallest-possible-sum/tests.ts b/5kyu/smallest-possible-sum/tests.ts new file mode 100644 index 0000000..25aed6c --- /dev/null +++ b/5kyu/smallest-possible-sum/tests.ts @@ -0,0 +1,8 @@ +// See https://www.chaijs.com for how to use Chai. +import { assert } from "chai"; + +import { solution } from "./solution"; + +describe('Example test', function() { + it('[6,9,12]', () => assert.strictEqual(solution([6,9,21]),9)); +}); \ No newline at end of file