Append and Delete

Solutions The challenging part of this exercise is how to handle operations in excess. If a string is empty, we can consume as many operations we want (as the problem specifies). So, the easy case is when we have a number of operations that is greater than the sum of length of both strings. In this case the solution is “Yes” because we can just remove all the characters from one, consume excess operations by repeatedly performing the second operation from the empty string, and finally appending the other characters. [Read More]
string  math 

Circular Array Rotation

Solutions You can actually rotate the array: in Javascript (by Alessia Bragagnolo) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 function circularArrayRotation(a, k, queries) { let result = []; while (k > 0) { let tmp = a.pop(); a.unshift(tmp); k--; } for (let i = 0; i < queries.length; i++) { result.push(a[queries[i]]); } return result; } But… we don’t really need to rotate the array: [Read More]
math 

Fair Rations

You are the benevolent ruler of Rankhacker Castle, and today you’re distributing bread. Your subjects are in a line, and some of them already have some loaves. Times are hard and your castle’s food stocks are dwindling, so you must distribute as few loaves as possible according to the following rules: Every time you give a loaf of bread to some person , you must also give a loaf of bread to the person immediately in front of or behind them in the line (i. [Read More]
math  ad hoc 

Maximum Perimeter Triangle

Solutions This problem simulates the situation when you need some additional “domain knowledge” to find a solution. In this case the domain is “geometry” and you have to find (or recall) the Triangle inequality: for any triangle, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side In other terms, for any three sticks \(a, b, c\), the following must be true: [Read More]
greedy  math 

Product of Array Except Self

Given an array of \( n \) integers where \( n > 1 \), \( nums \), print an array \( output \) such that \( output[i] \) is equal to the product of all the elements of \( nums \) except \( nums[i] \).

Solve the problem in \( O(N) \) without using division.

[Read More]

Restaurant

Solutions We have to cut a rectangular bread having size \(l * b\) into squares of equal dimension such that no piece of original bread is left over. So we have to make cuts only vertically or horizontally. Hence we can conclude that if the length of a side of the square is \(a\), both \(l\) and \(b\) has to be divisible by \(a\). In addition, \(a\) has to be as large as possible. [Read More]
math 

The Salesman in a Rush

You are a salesman and your goal is to visit all the houses along a certain street. You walk at a constant rate and can start and end at any location. You may visit the houses in any order you wish. Given the locations of the houses along the street, what is the minimum amount of time needed for you to visit all the houses? Formally, we represent the street as a number line and the positions of the houses as numbers along this line. [Read More]
math