Challenge - 5 Problems
Euclidean Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of GCD calculation using Euclidean Algorithm
What is the output of the following TypeScript code that calculates the GCD of two numbers using the Euclidean Algorithm?
DSA Typescript
function gcd(a: number, b: number): number {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
console.log(gcd(48, 18));Attempts:
2 left
💡 Hint
Remember the Euclidean Algorithm repeatedly replaces the larger number by the remainder until the remainder is zero.
✗ Incorrect
The Euclidean Algorithm finds the greatest common divisor by repeatedly taking the remainder. gcd(48,18) is 6 because 48 % 18 = 12, then gcd(18,12), 18 % 12 = 6, then gcd(12,6), 12 % 6 = 0, so gcd is 6.
❓ Predict Output
intermediateOutput of LCM calculation using GCD
What is the output of this TypeScript code that calculates the LCM of two numbers using their GCD?
DSA Typescript
function gcd(a: number, b: number): number {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a: number, b: number): number {
return (a * b) / gcd(a, b);
}
console.log(lcm(21, 6));Attempts:
2 left
💡 Hint
LCM can be found by multiplying the numbers and dividing by their GCD.
✗ Incorrect
GCD of 21 and 6 is 3. LCM = (21 * 6) / 3 = 126 / 3 = 42.
🧠 Conceptual
advancedWhy does the Euclidean Algorithm work for GCD?
Which of the following best explains why the Euclidean Algorithm correctly finds the GCD of two numbers?
Attempts:
2 left
💡 Hint
Think about how the remainder relates to the original numbers and their divisors.
✗ Incorrect
The Euclidean Algorithm works because the GCD of two numbers also divides their difference (or remainder). Replacing the larger number by the remainder keeps the GCD unchanged until the remainder is zero.
🔧 Debug
advancedIdentify the error in this GCD function
What error will this TypeScript code produce when trying to calculate gcd(10, 5)?
DSA Typescript
function gcd(a: number, b: number): number {
while (b > 0) {
const temp = a % b;
a = b;
b = temp;
}
return a;
}
console.log(gcd(10, 5));Attempts:
2 left
💡 Hint
Check the loop condition and the variable updates carefully.
✗ Incorrect
The code correctly implements the Euclidean Algorithm. For gcd(10,5), it outputs 5 as expected.
❓ Predict Output
expertOutput of GCD and LCM for multiple numbers
What is the output of this TypeScript code that calculates the GCD and LCM of an array of numbers?
DSA Typescript
function gcd(a: number, b: number): number {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a: number, b: number): number {
return (a * b) / gcd(a, b);
}
function gcdArray(arr: number[]): number {
return arr.reduce((acc, val) => gcd(acc, val));
}
function lcmArray(arr: number[]): number {
return arr.reduce((acc, val) => lcm(acc, val));
}
const numbers = [12, 15, 20];
console.log({ gcd: gcdArray(numbers), lcm: lcmArray(numbers) });Attempts:
2 left
💡 Hint
Calculate GCD and LCM step by step for the array elements.
✗ Incorrect
GCD(12,15)=3, GCD(3,20)=1. LCM(12,15)=60, LCM(60,20)=(60*20)/20=60. Output: {"gcd":1, "lcm":60} (option A).
