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