Write and analyze an algorithm which, given two numbers a and n, computes an. You may assume that n is a non-negative integer. A brute-force approach to this takes O(n) time; give me a significantly more efficient solution.
You are given a board with n holes of various sizes drilled in it, and n bolts to be inserted into them. You are guaranteed that there is a bolt exactly matching the size of each hole, but none of the bolts or holes are labelled, and you don't have a measuring device, nor any way to compare two holes or two pegs together. The only thing you can do is try a peg in a hole to learn whether it's too large, too small, or just right.
Give an algorithm to match each bolt to the appropriate hole, in time O(n log(n)).
An electronic chip is designed in such a manner that any two chips can be connected to test one another; each chip reports whether the other one is good or bad. A good chip always reports correctly whether the other chip is good or bad, but a bad chip's report cannot be trusted. Thus there are four possible outcomes:
Chip A says | Chip B says | Conclusion |
---|---|---|
B is good | A is good | Either both are good, or both are bad |
B is good | A is bad | A (and possibly B) is bad |
B is bad | A is good | B (and possibly A) is bad |
B is bad | A is bad | At least one is bad |