Algorithmic Puzzles
ALGORITHMIC PUZZLES
Anany Levitin
and
Maria Levitin
Oxford University Press, Inc., publishes works that further
Oxford Universitys objective of excellence in research, scholarship, and education.
Oxford New York
Auckland Cape Town Dares Salaam Hong Kong Karachi
Kuala Lumpur Madrid Melbourne Mexico City Nairobi
New Delhi Shanghai Taipei Toronto With offices in
Argentina Austria Brazil Chile Czech Republic France Greece
Guatemala Hungary Italy Japan Poland Portugal Singapore
South Korea Switzerland Thailand Turkey Ukraine Vietnam Copyright 2011 by Oxford University Press
Published by Oxford University Press, Inc.
198 Madison Avenue, New York, New York 10016
http://www.oup.com
Oxford is a registered trademark of Oxford University Press All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior permission of Oxford University Press.
Library of Congress Cataloging-in-Publication Data
Levitin, Anany.
Algorithmic puzzles / Anany Levitin, Maria Levitin.
p. cm.
Includes bibliographical references and index.
ISBN 978-0-19-974044-4 (pbk.)
1. Mathematical recreations. 2. Algorithms.
I. Levitin, Maria. II. Title.
QA95.L475 2011
793.74 dc22 2010052043
9 8 7 6 5 4 3 2 1
Printed in the United States of America on acid-free paper
To Max with love
Contents
WHAT IS THIS BOOK ABOUT?
This book is a collection of algorithmic puzzlespuzzles that involve, explicitly or implicitly, clearly defined procedures for solving problems. It is a unique collection of such puzzles. The book includes some old classics, which have become a part of mathematics and computer science folklore. It also contains newer examples, some of which have been asked during job interviews at major companies.
The book has two main goals:
To entertain a wide range of readers interested in puzzles
To promote development of high-level algorithmic thinking (with no computer programming), supported by a carefully developed list of general algorithm design strategies and analysis techniques
Although algorithms do constitute the cornerstone of computer science and no sensible computer programming is possible without them, it is a common misconception to equate the two. Some algorithmic puzzles predate computers by more than a thousand years. It is true, however, that the proliferation of computers has made algorithmic problem solving important in many areas of modern life, from hard and soft sciences to art and entertainment. Solving algorithmic puzzles is the most productive and definitely most enjoyable way to develop and strengthen ones algorithmic thinking skills.
WHOM IS THIS BOOK FOR?
There are three large categories of readers who should be interested in this book:
Puzzle lovers
People interested in developing algorithmic thinking, including teachers and students
People preparing for interviews with companies giving puzzles as well as people conducting such interviews
All we have to say to puzzle lovers is to reassure them that they could enjoy this collection as they would a collection not dedicated to any particular theme or type of puzzle. They will encounter a few all-time favorites, but, hopefully, will also find a number of little-known puzzle gems. No computing background or even an interest in it is assumed; such a reader can simply ignore references to specific algorithm design strategies and analysis techniques in the solutions given.
Algorithmic thinking has recently become somewhat of a buzz word among computer science educators, and with some justice: ubiquity of computers in todays world does make algorithmic thinking a very important skill for almost any student. Puzzles are an ideal vehicle for mastering this important skill for two reasons. First, puzzles are fun, and a person is normally willing to put more effort into solving them than in doing routine exercises. Second, algorithmic puzzles force a solver to think on a more abstract level. Even computer science students have a tendency to think about algorithmic problems in terms of a computer language they know instead of applying general design and analysis strategies. Puzzles can rectify this important deficiency.
The puzzles in this book can certainly be used for individual study. Together with the tutorials, they provide, in our view, a good introduction to main algorithmic ideas. They can also be used by teachers of computing coursesboth at the college and secondary school levelas supplemental exercises and project topics. The book might also be of interest for problem-solving courses, especially those based on puzzles.
As to people preparing for interviews, they should find the book helpful in two ways. First, it contains many examples of puzzles they may encounter, with complete solutions and comments. Second, the book also provides concise tutorials on algorithm design strategies and analysis techniques. After all, managers offering puzzles during interviews claim that they are more interested in the way an interviewee approaches a puzzle than in an actual solution to it. Showing an expertise in applying general design strategies and analysis techniques should then be a highly attractive way to impress the potential employer.
WHAT PUZZLES ARE INCLUDED IN THE BOOK?
Algorithmic puzzles constitute a small fraction among thousands of mathematical puzzles invented over the years. In selecting puzzles for the book, we have sought puzzles that satisfy the following criteria.
First, we wanted puzzles that illustrate some general principle in design or analysis of algorithms.
Second, we were looking for beauty and elegance, the subjectivity of those qualities notwithstanding.
Third, we wanted the puzzles to run a wide range of difficulty levels. Puzzle difficulty is hard to pinpoint; math professors have been occasionally stumped by puzzles easily solved by middle school students. Still, we have divided the books puzzles into three sectionsEasier Puzzles, Puzzles of Medium Difficulty, and Harder Puzzlesto give readers some help in gauging the puzzles difficulty. Within each of these three sections, we have tried to order the puzzles in increasing level of difficulty as well. The puzzles in the Easier Puzzles section require only middle school mathematics. Although solutions to a few problems in the other two sections do use proofs by induction, high school mathematics should, in general, suffice for solving all the books puzzles. In addition, topics such as binary numbers and simple recurrence relations are briefly reviewed in the second tutorial. This does not mean, of course, that all the puzzles in the book are easy. Some of themespecially those at the end of the last sectionare truly hard. But their difficulty does not lie in some sophisticated mathematics, and the reader should not be intimidated by them.
Fourth, we have felt compelled to include a few puzzles because of their historical importance. Finally, we only included puzzles with clear statements and solutions devoid of any tricks such as intentional ambiguity, word play, and so on.
One more important comment needs to be made here. Many puzzles in this book can be solved by exhaustive search or backtracking. (These strategies are explained in the books first tutorial.) It is
Next page