New answers tagged

0

The claim is equivalent to claiming that every deepest leaf in a tree rooted at $s$ is an endpoint to a diameter. So for contradiction, assume a diameter exists whose endpoints are $(a, b)$, and that a deeper node $d$ exists. Either the shallowest node $x$ in the $a \to b$ path is an ancestor of $d$ or it isn't: $x$ is an ancestor case: x / \ / y a ...


0

You could perhaps use a 2 dimensional k-d tree to store the set of free positions $(x, y)$ where the left upper corner of a new square could be chosen without overlapping the existing squares. Each node of the tree covers a region in the area $a$ and you can decorate this node with this region's coordinate and the number of free positions that it contains. ...


2

(BTW, I think the code in the first edition of the question was the issue -- not the background info with respect to the sexagenary cycle, which is actually good to include to understand the motivation for the problem). To put the problem a bit differently, you have the sets $\mathbb{Z}/m\mathbb{Z}$ = $\{0,1,...,m-1\}$ and $\mathbb{Z}/n\mathbb{Z} = \{0,1,......


-1

Yes, there is. Because everything is given, you only want to solve a specific instance of a more general problem. The solution is: return 2;


0

My answer for this was like below function solution($A) { // write your code in PHP7.0 $res = 0; $h = 0; $i = 0; foreach ($A as $a) { if (isset($A[$i+1])) { if ($a == $A[$i+1]) { $res++; continue; } if ($h >= 2) { $res++; $h = 0; } if ($a < $A[$i+1])...


2

Your interviewer is wrong. Suppose that you had an algorithm which gets as input a sorted integer array $A_1,\ldots,A_n$, outputs Yes iff $A_i + A_j = 0$ for some $i \neq j$, and always accesses at most $n-1$ inputs. Suppose first that $n = 2m$ is even, and consider the following sorted integer array $B$: $$ -2m, -2(m-1), \ldots, -4, -2, 1, 3, \ldots, 2(m-1)-...


0

Find the last occurence of any h, i, j, k, l, n. If none is found, there is no solution. If the last one is found at position r, then the string we are looking for must start at position before r. Then you start from the left looking for the first letter among a, b, c, d, e. If you don't find one before position r then there is no solution. Otherwise call ...


Top 50 recent answers are included