Tower of Hanoi: A Recursive Challenge The Tower of Hanoi is a classic puzzle involving three pegs and a number of disks of different sizes. The objective is to move the entire stack to another peg, following these rules: only one disk can be moved at a time, and a larger disk cannot be placed on top...
Problem Context An island has two types of inhabitants: truth-tellers, who always tell the truth, and liars, who always lie. Each inhabitent has a unique integer identifier. You are allowed n questions. Each question must be direcetd to one inhabitant, asking whether another inhabitant is a truth-te...
Longest Increasing Subsequence To determine the length of the longest strictly increasing subsequence, dynamic programming is applied where dp[i] represents the length of the longest subsequence ending at index i. class Solution { public: int lengthOfLIS(vector<int>& sequence) { int size =...
Problem A: Symmetric Pair Removal Given a binary string of length n, repeatedly remove a pair of characters from opposite ends if they differ (one is '0' and the other is '1'). Calculate the remaining length after no more valid pairs can be removed. A two-pointer technique efficiently solves this pr...
L1-1: Print Greeting Print a fixed greeting message. print("yuan shen, qi dong!") L1-2: Value Comparison Given a real number x, compare it to 1 and print the appropriate relational operator. #include <iostream> using namespace std; int main() { double val; cin >> val; if(val &l...
Problem Analysis The road construction problem involves determining the minimum time required to complete paving operations across multiple segments. While algorithm tags suggest greedy approaches and binary indexed trees, a dynamic programming solution provides an elegant and efficeint implementati...
Contest Preparation Algorithm This solution handles two special cases before applying the general formulla: #include <iostream> using namespace std; int main() { int n, m; cin >> n >> m; if (n == 0) { cout << 0 << endl; } else if (n <= m) { cout << 2 << e...
Longest Monotonic Subsequence via Greedy Array Core Concept The "pseudo-monotonic stack" approach for finding longest monotonic subsequences uses a dynamic array that maintains potential candidates for optimal sequence construction. This method efficiently builds the solution in O(n log n)...
Task 1: Bitwise Manipulation Strategy The objective involves transforming an integer based on specific bitwise properties. The core logic requires isolating the odd component of the input value by shifting out trailing zeros. Once the odd base is identified, it is shifted left by a count derived fro...
Interval Inversion Count Given an array of length n with elements bounded by a small constant (≤ 50), we need to answer m queries of the form: count the number of inversions inside subarray [l, r]. A naive O(n2) per query is too slow, and a Mo’s algorithm solution, while feasible for moderate constr...