Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.
function maxa(arr) { let π° = 0; for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.π; j++) { let cA = Math.abs(i - j); cA *= Math.min(βοΈ[i], arr[j]); if (cA > max) max = cA; } } return max; } let A = maxa([5, π§, 3, 7, 1, 4]); // βοΈ = ? (identifier) // π = ? (identifier) // π§ = ? (number) // π° = ? (identifier) // such that A = 20 (number)
Today's challenge is relatively simple but does require some brain power.
The first bug π° is a variable declaration, after quickly skimming all lines the only variable that's used but hasn't been declared yet is max
.
The second bug π is very likely to be the propery length
of an array.
The third bug βοΈ is a variable which is being used in an array like fashion, just like its neighbor, so my best bet is that it should be arr
.
The final bug π§ should be a number, and it's part of the input for the function maxa
; we must ensure that A = 20
to complete the challenge. To determine this number we have to analyze what the function does.
The funcion maxa
begins with a for-loop starting at i = 0
over all numbers, then a second for-loop that starts at j = i+1
. The variable cA
determines the distance between i
and j
, it's then multiplied by the minimum of the values at i
and j
; finally the largest recorded value for cA
is stored in (and returns) max
.
The goal is to find i
and j
such that max = 20
. Let's write in pseudo-code to help us out:
max = cA * min(arr_i, arr_j) factors of 20 are: 1 * 20 2 * 10 4 * 5 ----- let Y = abs(i - j), Y must be either 4 or 5 let Z = min(arr_i, arr_j), Z must be either 5 or 4 then cA = Y * Z = 20 ----- the largest value for Y is abs(0 - 5) = 5 then Z = min(5, 4) = 4 then cA = 5 * 4 = 20 --> requirement met
Since the position of π§ in the array is neither 0 or 5, its value doesn't matter as long as it won't result in a cA
value larger than 20. So we can pick the smallest value such as 1:
By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Get started and become a certified Codr today at https://nevolin.be/codr/
Top comments (1)
Same here. everyday I solve a competitive programming question but on Code Chef.