Posts

HackerRank: Sock Merchant (Easy)

Link to code challenge function sockMerchant(n, ar) { let pairs = 0; let sum = 1; let arr = ar.sort(); for (let i = 1; i 1) pairs += Math.floor(sum / 2); sum = 1; } } return pairs; }

HackerRank: Migratory Birds (Easy)

Link to code challenge Naive approach. function migratoryBirds(arr) { let num = Infinity; let count = 0; let largestCount = 0; let newArr = arr.sort(); for (let i = 0; i largestCount) { largestCount = count; num = newArr[i - 1]; } //DON'T need this else if statement since newArr[i - 1] will always be greater than num as the arr is in numerical order else if (count === largestCount) { if (newArr[i - 1]

HackerRank: Birthday Chocolate (easy)

Link to code challenge function birthday(s, d, m) { let count = 0; let pointer = 0; while (pointer

HackerRank- Kangaroo (easy)

Link to code challenge Needed assistance on 2nd formula instead of doing an iteration. You first find the difference between the starting points of the two kangaroos. Next, you find the difference between the speed at which each kangaroo travels. This difference is how much the distance between the two kangaroos is decreasing by at each "iteration". Therefore, the difference in speed has to be a factor of the difference in their starting points in order for both kangaroos to eventually be on the same points. If the difference in distance is 7 but at each iteration the distance is reduced by 2, they will never be on the same point (7 / 2 !== 0). function kangaroo(x1, v1, x2, v2) { let pos1 = x1; let pos2 = x2; if ( (x1 > x2 && v1 > v2) || (x1

HackerRank- Mini-Max Sum (easy)

Link to code challenge function miniMaxSum(arr) { let newArr = arr.sort((a, b) => a - b); let lowest = newArr.slice(0, newArr.length - 1).reduce((a, b) => a + b); let highest = newArr.slice(1).reduce((a,b) => a + b); console.log(lowest, highest); }

Hacker Rank- Staircase (Easy)

Link to code challenge function staircase(n) { let count = n - 1; for (let i = "#"; i.length Without creating a new variable: function staircase(n) { for (let i = "#"; i.length

Hacker Rank- Diagonal Difference (Easy)

Link to code challenge function diagonalDifference(arr) { console.log(arr); let sum1 = 0; let sum2 = 0; let j = 0; let k = arr.length - 1; for (let i = 0; i