Hello everyone!
Welcome to the editorial for our mashup contest. We selected a mix of Arrays, HashMaps, Math, Strings, Sliding Window and Prefix Sum problems. The goal was to strengthen logical thinking and problem-solving skills without getting bogged down in complex data structures.
Below you will find hints, tutorials, and solutions for each problem. We used spoiler tags to keep things clean. Try to solve it yourself before clicking!
Happy coding!
Tags: implementation strings
Difficulty: ⭐️ (Very Easy)
This is a simple pattern-checking problem that tests your understanding of strings and substrings.
Understanding the ProblemThe Idea
Polycarp answers "Yes" by repeating it many times:
YesYesYesYesYes...
But you only hear a substring of that long string.
What You Need to Check
Given a string s, determine:
Is s a substring of the infinite string "YesYesYes..."?
Important Notes
- Case matters: "YES" is NOT the same as "Yes".
- The pattern strictly repeats "Yes".
Hint - Build a long enough string like "YesYesYesYes...".
- Check if
s exists inside it. - Since |s| ≤ 50, repeating "Yes" about 20 times is enough.
TutorialApproach
Instead of dealing with an infinite string, we simulate it.
Step-by-step:
- Create a long string: base = "YesYesYesYesYes..."
- For each test case:
- Read string
s - Check if
s is a substring of base
- If yes, print "YES", otherwise print "NO"
Why This Works
Any valid substring of "YesYesYes..." must appear in a sufficiently long repetition.
Since: - Maximum |s| = 50 - "Yes" length = 3
Repeating "Yes" around 20 times (length = 60) is enough.
Common Mistakes
- Using "YES" instead of "Yes"
- Forgetting substring must be continuous
- Not repeating "Yes" enough times
Solution (Python)```python
def solve(): t = int(input())
base = "Yes" * 20 # long enough for all cases
for _ in range(t):
s = input().strip()
print("YES" if s in base else "NO")solve()
Complexity: Time: O(t × |s|) Space: O(1)
Tags: implementation math
Difficulty: ⭐️ (Very Easy)
This is a very simple counting problem.
Understanding the ProblemThe Goal
We are given: - An integer array a of size n
Operation
You can: - Pick two indices i < j - Such that: — a[i] == a[j] — Both indices are not used before
Each such pair gives +1 score.
Task
Find the maximum score.
Hint - Count how many times each number appears
- From each value, you can form:
count // 2 pairs
TutorialApproach
We need to maximize the number of valid pairs.
Key Observation
- Each pair needs 2 equal elements
- So for a value appearing
k times:
number of pairs = k // 2
Step-by-Step Logic
- Read
n and array a - Count frequency of each number
- For each frequency:
- Print total score
Example
Input: 6
1 2 3 1 2 3
Frequencies: 1 → 2
2 → 2
3 → 2
Pairs: 1 → 1 pair
2 → 1 pair
3 → 1 pair
Total = 3
Why this works
Each pair uses exactly 2 equal elements, and we cannot reuse indices.
Complexity
Solution (Python)def solve():
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
freq = {}
for x in arr:
freq[x] = freq.get(x, 0) + 1
score = 0
for count in freq.values():
score += count // 2
print(score)
solve()
Tags: brute force implementation math sliding window
Difficulty: ⭐️⭐️ (Easy)
This is a parity-based selection problem that can be solved using counting and simple constraints.
Understanding the ProblemThe Goal
We are given: - An array a of size n - An integer x
Task
Select exactly x elements such that: - Their sum is odd
Key Observation
- Sum is odd ⇔ number of odd elements selected is odd
- Even numbers do not change parity of the sum
Hint 1Count how many odd and even numbers exist.
Hint 2Try to pick an odd count of odd numbers (at least 1), and fill the rest with even numbers.
Hint 3 (Important)Let: - o = number of odd elements
- e = number of even elements
You need to pick k odd numbers such that: - k is odd
- 0 ≤ k ≤ o
- x - k ≤ e
Check if such k exists.
TutorialApproach
We only care about parity, not actual values.
Step-by-step:
- Count:
o = number of odd elements e = number of even elements
- We want to pick
x elements with odd sum - That means:
- pick an odd number of odd elements (
k) - remaining (
x - k) must be even elements
- Compute:
l = max(1, x - e) → minimum odd elements needed r = min(x, o) → maximum odd elements possible
- Check if there exists an odd
k in [l, r]
Why this works
- Odd + Odd = Even
- Even + Even = Even
- Odd + Even = Odd
So we must ensure an odd count of odd numbers is selected.
Example
Input: n = 3, x = 3
a = [101, 102, 103]
Odds = 2, Evens = 1
We must pick all 3: - Odd count = 2 → even sum → not valid
Answer: No
Complexity
- Time: O(n) per test case
- Space: O(1)
Solution (Python)t = int(input())
for _ in range(t):
n, x = map(int, input().split())
a = list(map(int, input().split()))
# count odd and even numbers
o = sum(1 for v in a if v % 2)
e = n - o
# possible range of odd numbers we can take
l = max(1, x - e)
r = min(x, o)
# check if there exists an odd number in [l, r]
print("Yes" if l <= r and (l % 2 == 1 or r > l) else "No")
Tags: implementation data structures DP prefix sum
Difficulty: ⭐️⭐️⭐️ (Medium)
This is a classic prefix sum problem that allows answering range queries efficiently.
Understanding the ProblemThe Goal
We are given: - A string s consisting of '.' and '#' - m queries, each with (l, r)
Task
For each query, count how many indices i satisfy: - l ≤ i < r - s[i] == s[i+1]
In other words, count how many adjacent equal characters exist in the range.
Hint 1Directly checking each query will be too slow.
Hint 2Precompute results using a prefix sum array.
Hint 3 (Important)Build an array where: - pref[i] = number of positions j ≤ i such that s[j] == s[j-1]
Then each query becomes: - pref[r-1] - pref[l-1]
TutorialApproach
We preprocess the string to answer queries in constant time.
Step-by-step:
- Create a prefix array
pref - For each position
i:
- If
s[i] == s[i-1], increment count
- Store cumulative counts in
pref - For each query
(l, r):
- Answer =
pref[r-1] - pref[l-1]
Why this works
pref[i] stores how many equal adjacent pairs exist up to index i - Subtracting gives count within a range
Example
s = "......"
Pairs: (1,2), (2,3), (3,4), (4,5), (5,6) → all equal
So prefix: [0,1,2,3,4,5]
Query (1,6): Answer = pref[5] — pref[0] = 5
Complexity
- Preprocessing: O(n)
- Each query: O(1)
- Total: O(n + m)
- Space: O(n)
Solution (Python)s = input().strip()
n = len(s)
# build prefix array
pref = [0] * n
for i in range(1, n):
pref[i] = pref[i-1] + (1 if s[i] == s[i-1] else 0)
m = int(input())
res = []
for _ in range(m):
l, r = map(int, input().split())
# convert to 0-based and use prefix sum
res.append(str(pref[r-1] - pref[l-1]))
print('\n'.join(res))
Tags: implementation data structures prefix sum binary search
Difficulty: ⭐️⭐️⭐️ (Medium)
This is a prefix sum + difference array problem that efficiently handles range updates and queries.
Understanding the ProblemThe Goal
We are given: - n ranges [l, r] (recipes) - A threshold k - q queries [a, b]
Task
A temperature is admissible if: - It is covered by at least k ranges
For each query [a, b], count how many admissible temperatures exist in that range.
Hint 1Use a difference array to efficiently count how many ranges cover each temperature.
Hint 2Convert the coverage into a prefix sum array.
Hint 3 (Important)Build another prefix array where: - pref[i] = number of admissible temperatures ≤ i
Then each query becomes: - pref[b] - pref[a-1]
TutorialApproach
We solve this in three steps.
Step 1: Range Frequency using Difference Array
- For each range
[l, r]: diff[l] += 1 diff[r+1] -= 1
Then compute prefix sum to get: - cnt[i] = how many ranges include temperature i
Step 2: Mark Admissible Temperatures
- If
cnt[i] ≥ k, then temperature i is valid - Build a prefix array:
pref[i] = number of admissible temperatures up to i
Step 3: Answer Queries
For each query [a, b]: - Answer = pref[b] - pref[a-1]
Why this works
- Difference array handles range updates in O(1)
- Prefix sum converts it into actual frequencies
- Another prefix sum enables fast queries
Complexity
- Building arrays: O(MAX)
- Each query: O(1)
- Total: O(n + q + MAX)
- Space: O(MAX)
(MAX ≈ 200000)
Solution (Python)n, k, q = map(int, input().split())
MAX = 200005
# difference array
diff = [0] * MAX
for _ in range(n):
l, r = map(int, input().split())
diff[l] += 1
diff[r + 1] -= 1
# build coverage count
cnt = [0] * MAX
for i in range(1, MAX):
cnt[i] = cnt[i-1] + diff[i]
# build admissible prefix
pref = [0] * MAX
for i in range(1, MAX):
pref[i] = pref[i-1] + (1 if cnt[i] >= k else 0)
# answer queries
for _ in range(q):
a, b = map(int, input().split())
print(pref[b] - pref[a-1])
Auto comment: topic has been updated by gemechualemu (previous revision, new revision, compare).