Algorithms
This area contains some code I've written in a Programming Challenges course. The problems and challenges presented here are a great way to improve both algorithmic and coding skills. Most of these problems are designed to be solved by short, clever algorithms. Concepts like object-oriented programming (useful for building large, reusable programs) may not apply in this domain. If you want to see some different and "well planned" code, please check out my open source projects instead :)
The Programming Challenges book can be used for self-study. All the solutions available here (coded in C or C++) have been accepted by the online judges, i.e., they are correct (they solve the problem).
When you select a source code, a new window containing the solution will appear. If the new window doesn't show up, please deactivate any popup blockers you have for a while.
| ID | Problem name | Date | Source code |
|---|---|---|---|
| Lesson 1: Introduction | |||
| UVa 272 | TeX Quotes | 2009-02-17 | tex.cpp |
| UVa 100 | The 3n + 1 problem - hint | 2009-02-17 | 3n_1.cpp |
| UVa 458 | The Decoder | 2009-02-17 | decoder.cpp |
| Lesson 2: Introduction | |||
| UVa 10258 | Contest Scoreboard - hint | 2009-02-19 | scoreboard.cpp |
| UVa 10104 | Euclid Problem | 2009-02-19 | euclid.cpp |
| SPOJBR F91 | f91 | 2009-02-19 | f91.cpp |
| UVa 424 | Integer Inquiry | 2009-02-19 | inquiry.cpp |
| UVa 10189 | Minesweeper | 2009-02-19 | minesweeper.cpp |
| SPOJBR PLACAR | Quem vai ser reprovado | 2009-02-19 | reprovado.cpp |
| UVa 382 | Perfection | 2009-02-20 | perfect.cpp |
| Lesson 3: Stacks | |||
| UVa 673 | Parentheses Balance | 2009-03-03 | balance.cpp |
| UVa 732 | Anagrams by Stack - hint | 2009-03-05 | anagram.cpp |
| UVa 514 | Rails | 2009-03-05 | rails.cpp |
| Lesson 4: Queues | |||
| SPOJ ONP | Transform the Expression | 2009-03-05 | transform.cpp |
| SPOJBR NUMERDOS | Numero de Erdos - hint | 2009-03-06 | erdos.cpp |
| SPOJ EXPRESS | Expressions | 2009-03-07 | expressions.cpp |
| SPOJ HIKE | Hike on a Graph - hint | 2009-03-09 | hike.cpp |
| Lesson 5: Data Structures | |||
| UVa 540 | Team Queue | 2009-03-07 | team_queue.cpp |
| UVa 10377 | Maze Traversal | 2009-03-10 | maze.cpp |
| UVa 10142 | Australian Voting | 2009-03-11 | australian.c |
| Lesson 6: Trees | |||
| UVa 536 | Tree Recovery | 2009-03-12 | treerec.cpp |
| UVa 122 | Trees on the level | 2009-03-12 | treelevel.cpp |
| UVa 112 | Tree Summing | 2009-03-14 | treesum.cpp |
| Lesson 7: Trees | |||
| UVa 615 | Is It A Tree? | 2009-03-17 | istree.cpp |
| UVa 10701 | Pre, in and post | 2009-03-17 | preinpost.cpp |
| UVa 10308 | Roads in the North | 2009-03-22 | roads.cpp |
| Lesson 8: Interval Trees / Range Minimum Query (RMQ) | |||
| SPOJBR SORVETE | Sorvete | 2009-03-21 | sorvete.cpp |
| SPOJ FREQUENT | Frequent Values - COOL!! - hint | 2009-07-03 | frequent.cpp |
| Lesson 9: Backtracking | |||
| SPOJBR JUNINA | Festa Junina | 2009-04-01 | junina.cpp |
| SPOJ WORDS1 | Play on Words - hint | 2009-04-06 | words1.c |
| Lesson 10: Backtracking | |||
| SPOJ COMCB | Complete Chess Boards | 2009-04-02 | chess.cpp |
| SPOJBR DOMINO | Domino | 2009-04-06 | domino.cpp |
| SPOJ EUROPEAN | European railroad tracks | 2009-04-07 | european.cpp |
| Lesson 11: Backtracking | |||
| UVa 843 | Crypt Kicker - hint | 2009-04-14 | crypt.cpp |
| UVa 861 | Little Bishops | 2009-04-19 | bishop2.c |
| UVa 10160 | Servicing Stations - hint | 2009-04-26 | servstat.cpp |
| Lesson 12: Backtracking | |||
| SPOJ SOLIT | Solitaire | 2009-04-26 | solit.cpp |
| UVa 10181 | 15-Puzzle Problem - COOL!! - hint | 2009-05-18 | 15puzzle.cpp |
| SPOJ SOLIT | Solitaire (faster) - nice! | 2009-05-18 | solit2.c |
| Lesson 13: Sorting | |||
| UVa 612 | DNA Sorting - hint | 2009-04-23 | dna.cpp |
| UVa 299 | Train Swapping - hint | 2009-04-23 | train.cpp |
| UVa 10062 | Tell me the frequencies | 2009-04-24 | tellfreq.cpp |
| UVa 10698 | Football Sort | 2009-04-25 | football.cpp |
| UVa 120 | Stacks of Flapjacks | 2009-04-25 | flapjack.cpp |
| Lesson 14 - Sorting | |||
| UVa 10905 | Children's Game | 2009-04-28 | children.cpp |
| UVa 11057 | Exact Sum | 2009-04-28 | exact.cpp |
| UVa 263 | Number Chains | 2009-04-28 | chain.cpp |
| UVa 10810 | Ultra-Quicksort - hint | 2009-04-28 | ultra.cpp |
| Lesson 15 - Arithmetic and Algebra | |||
| UVa 10018 | Reverse and Add | 2009-05-05 | revadd.cpp |
| UVa 10077 | The Stern-Brocot Number System - hint | 2009-05-07 | brocot.cpp |
| UVa 474 | Heads/Tails Probability | 2009-05-14 | prob.cpp |
| UVa 701 | The Archeologists' Dilemma | 2009-05-14 | arch.cpp |
| UVa 847 | A Multiplication Game | 2009-05-16 | mulg.cpp |
| Lesson 16 - Combinatorics | |||
| UVa 10198 | Counting - hint | 2009-05-07 | cnt.cpp |
| UVa 10254 | The Priest Mathematician | 2009-05-13 | priest.cpp |
| UVa 846 | Steps | 2009-05-16 | steps.cpp |
| Lesson 17 - Combinatorics | |||
| UVa 10784 | Diagonal - hint | 2009-05-16 | diag.cpp |
| UVa 10219 | Find the ways! - hint | 2009-05-16 | fways.cpp |
| UVa 10338 | Mischiveous Children | 2009-05-16 | mchild.cpp |
| Lesson 18 - Dynamic Programming | |||
| UVa 10066 | The Twin Towers | 2009-06-02 | twintowers.cpp |
| UVa 830 | Biblioteca Otima | 2009-06-05 | bibo.c |
| UVa 10154 | Weights and Measures - nice! - hint | 2009-06-29 | weme.cpp |
| Lesson 19 - Dynamic Programming | |||
| SPOJBR 1356 | Parque Jurassico | 2009-06-04 | parque.c |
| UVa 10003 | Cutting Sticks | 2009-06-07 | cstick.cpp |
| UVa 10271 | Chopsticks - nice! | 2009-06-27 | chopsticks.cpp |
| SPOJBR 1758 | Last Year at Marienbad | 2009-06-30 | marienba.cpp |
| Lesson 20 - Dynamic Programming | |||
| SPOJBR 2607 | Campo de Minhocas | 2009-06-09 | minhoca.cpp |
| SPOJBR 1365 | Palindrome | 2009-06-28 | palindrome.cpp |
| SPOJ 43 | Copying Books | 2009-07-01 | cbook.cpp |
| UVa 10261 | Ferry Loading - hint | 2009-07-01 | ferry.cpp |
| Lesson 21 - Miscellaneous / Graphs | |||
| UVa 10340 | All in All | 2009-06-18 | allinall.cpp |
| UVa 10305 | Ordering Tasks - hint | 2009-06-18 | ordtask.cpp |
| UVa 558 | Wormholes | 2009-06-18 | wormhole.cpp |
| UVa 423 | MPI Maelstrom | 2009-06-27 | mpi.cpp |
| UVa 200 | Rare Order | 2009-06-27 | rareorder.cpp |
| UVa 10278 | Fire Station | 2009-06-28 | firestation.cpp |
| UVa 10603 | Fill - hint | 2009-07-02 | fill.cpp |
| TopCoder | |||
| ApplePie | TCHS18 - ApplePie | 2009-04-07 | tchs18_250.cpp |
| FrogDerby | TCHS18 - FrogDerby | 2009-04-07 | tchs18_500.cpp |
| WarehouseJob | TCHS18 - WarehouseJob | 2009-04-07 | tchs18_1000.cpp |
| DartThrow | TCHS19 - DartThrow | 2009-04-07 | tchs19_250.cpp |
| RubeGoldberg | TCHS19 - RubeGoldberg | 2009-04-07 | tchs19_1000.cpp |
| SalePitch | TCHS19 - SalePitch | 2009-04-07 | tchs19_500.cpp |
| Postnet | TCHS20 - Postnet | 2009-05-05 | tchs20_500.cpp |
| Surname | TCHS20 - Surname | 2009-05-05 | tchs20_250.cpp |
| Touchdown | TCHS20 - Touchdown | 2009-05-05 | tchs20_1000.cpp |
| HuffmanDecoding | SRM308 - HuffmanDecoding | 2009-05-31 | srm308_500.cpp |
| MedianOfNumbers | SRM308 - MedianOfNumbers | 2009-05-31 | srm308_250.cpp |
| TreasuresPacking | SRM308 - TreasuresPacking | 2009-05-31 | srm308_1000.cpp |
| Library | SRM384 - Library | 2009-05-31 | srm384_500.cpp |
| PowerGame | SRM384 - PowerGame | 2009-05-31 | srm384_1000.cpp |
| Prank | SRM384 - Prank | 2009-05-31 | srm384_250.cpp |
| GoodAndBadPostmen | TCHS30 - GoodAndBadPostmen | 2009-06-25 | tchs30_500.cpp |
| HowManyBirthdays | TCHS30 - HowManyBirthdays | 2009-06-25 | tchs30_250.cpp |
| FactoVisors | SRM406 - FactoVisors | 2009-06-29 | srm406_500.cpp |
| HappyCells | SRM406 - HappyCells | 2009-06-30 | srm406_250.cpp |
| AcidRain | TCHS30 - AcidRain | 2009-07-01 | tchs30_1000.cpp |
| PolygonColors | SRM406 - PolygonColors | 2009-07-02 | srm406_1000.cpp |
| Graph Algorithms | |||
| SPOJBR MESA | Mesa da Sra Montagny! | 2009-02-25 | ep1.c |
| SPOJBR ENERGIA | Transmissao de Energia | 2009-03-08 | ep2.c |
| SPOJBR PASSEIO | Passeio da bicicleta | 2009-03-21 | ep3.c |
| SPOJBR PREEMPOS | Pre, Em e Pos | 2009-04-02 | ep4.c |
| SPOJBR MANUT | Manutencao | 2009-04-11 | ep5.c |
| SPOJBR CHICAGO | 106 miles to Chicago | 2009-05-15 | ep6.c |
| SPOJBR CIPO | Sistema Cipoviario | 2009-06-03 | ep7.c |


