Algorithm Problem Archive

A personal archive documenting my journey through advanced algorithmic problem solving, featuring curated problems from CCC,CCO, IOI, and USACO, along with detailed explanations, solution strategies, and refined code implementations in C++ or Java or Python
  • Lazy Segment Tree

    Before in a segment tree, we could only perform range query and point update. What if we wanted to perform range update and range queries? Naively looping over every index and updating it would cause O(NlogN) time complexity per query, which is far too slow. Instead we need to use a lazy segment tree.  To…

  • CCO ’99 P2 – Common Words

    Canadian Computing Competition: 1999 Stage 2, Day 1, Problem 2 Given a sequence of  words from a newspaper article and an integer , find the  most common word(s). Input Specification Input will consist of an integer  followed by  data sets. Each data set begins with a line containing  and , followed by  lines, each containing a word of up to  lowercase letters. There will…

  • CCC ’18 S2 – Sunflowers

    Canadian Computing Competition: 2018 Stage 1, Junior #4, Senior #2 Barbara plants  different sunflowers, each with a unique height, ordered from smallest to largest, and records their heights for  consecutive days. Each day, all of her flowers grow taller than they were the day before. She records each of these measurements in a table, with one row…

  • CCC ’01 S2 – Spirals

    Canadian Computing Competition: 2001 Stage 1, Junior #4, Senior #2 A spiral of numbers can start and end with any positive integers less than . Write a program which will accept two positive integers  and  as input, and output a list of numbers from  to  inclusive, shown in a spiral. You may assume that the end value is greater than…

  • CCC ’97 S1 – Sentences

    Write a program which accepts as input a list of subjects, a list of verbs, and a list of objects, and produces all possible sentences which consist of a subject, a verb, and an object. Input Specification The first line of the input file contains a positive integer  which is the number of data sets which…

  • CCC ’07 S2 – Boxes

    Canadian Computing Competition: 2007 Stage 1, Senior #2 Nowadays, almost any item can be bought and sold on the internet. The problem is shipping. Before an item can be sent, it must be carefully packaged in a cardboard box to protect it. While items come in many shapes and sizes, finding a box just the…

  • CCC ’15 S1 – Zero That Out

    Canadian Computing Competition: 2015 Stage 1, Senior #1 Your boss has asked you to add up a sequence of positive numbers to determine how much money your company made last year. Unfortunately, your boss reads out numbers incorrectly from time to time. Fortunately, your boss realizes when an incorrect number is read and says “zero”,…

  • CCC ’98 S2 – Cross Number Puzzle

    Write a program to print: Input Specification There is no input. Output Specification All the perfect numbers between  and  inclusive on one line, followed by all integers between  and  inclusive which are equal to the sum of the cubes of their digits on one line. Solution

  • CCC ’13 S1 – From 1987 to 2013

    Canadian Computing Competition: 2013 Stage 1, Junior #3, Senior #1 You might be surprised to know that 2013 is the first year since 1987 with distinct digits. The years 2014, 2015, 2016, 2017, 2018, 2019 each have distinct digits. 2012 does not have distinct digits, since the digit 2 is repeated. Given a year, what…

  • CCC ’15 S1 – Zero That Out

    Canadian Computing Competition: 2015 Stage 1, Senior #1 Your boss has asked you to add up a sequence of positive numbers to determine how much money your company made last year. Unfortunately, your boss reads out numbers incorrectly from time to time. Fortunately, your boss realizes when an incorrect number is read and says “zero”,…