Here is the mashup link (the problems are from Codeforces' problem set).
A. Nanati's Special number
To solve the problem we need to find the character with the highest alphabetical order in our string, since Abenezer will need at least that alphabet size and won't need more. To do this iterate through the string and find the character with the highest alphabetical order. Output the maximum alphabetical order found. The solution can be done in $$$O(n)$$$.
t = int(input())
for _ in range(t):
n = int(input())
s = input()
ma = 0
for ch in s:
ma = max(ma, ord(ch))
print(ma - 96)
B. Monochromatic Striped Pattern
What if we use a fixed size sliding window?
To obtain a segment of $$$k$$$ cells with black color, we must paint all the white cells within that segment black. We then consider all segments of length $$$k$$$ (there are only $$$n - k$$$ such segments) and choose the one with the fewest white cells on it. Implementing this involves using a sliding window of size $$$k$$$. Since we have only two colors, we can simplify our implementation by only using two pointers.
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = input()
recolored = n
black_count = 0
left = 0
for right, color in enumerate(s):
black_count += (color == 'B')
if right - left + 1 == k:
recolored = min(recolored, k - black_count)
black_count -= (s[left] == 'B')
left += 1
print(recolored)
C. Polycarp and the Divine Tongue
Underline every letter $$$w$$$, so that it can't be mistaken with $$$vv$$$. The string splits into segments of letters $$$v$$$. Out of each pair of consecutive letters $$$v$$$ at least one should be underlined. So for the minimum amount you can underline either all the odd positions of the segment or all the even ones. Thus, if the length of the segment is $$$len$$$ , then $$$⌊len/2⌋$$$ underlined letters is enough.
t = int(input())
for _ in range(t):
s = input()
left = 0
underline = 0
for right in range(len(s)):
if s[right] == 'w':
if s[left] == 'w':
underline += 1
else:
underline += 1 + (right - left)//2
left = right + 1
if left < len(s):
underline += (len(s) - left)//2
print(underline)
D. Distinctive Components
Because the constraints are small, brute forcing it is possible. So, check if every number inside your array can be expressed as a subarray sum.
Use two pointers technique to efficiently find subarrays with sums equal to the current element.
We're given an array of numbers and we're trying to find out how many special numbers are there in the array. A special number is one that can be expressed as the sum of two or more consecutive numbers in the array. To solve this, we'll go through each number in the array one by one and check if it satisfies the condition of being special. We'll do this by considering it as a potential sum of consecutive numbers starting from the beginning of the array. We'll keep track of the sum of these consecutive numbers as we move through the array.
Here's how it works: Imagine we're at a certain number in the array. We start considering it as the sum of consecutive numbers starting from the first number in the array. As we move forward, we add each number to our running sum. If at any point our running sum equals the current number, and we've considered at least two numbers in the subarray (because a single number by itself doesn't count as a sum of consecutive numbers), then we've found a special number. We'll count this and move on to the next number in the array.
By repeating this process for each number in the array, we'll be able to count all the special numbers. This approach allows us to efficiently find special numbers without needing to store all the possible consecutive sums separately. It's a straightforward method that checks each number individually to determine if it's special.
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
count = 0
for num in a:
left = 0
run_sum = 0
for right in range(n):
run_sum += a[right]
while run_sum > num:
run_sum -= a[left]
left += 1
if run_sum == num and left < right:
count += 1
break
print(count)
E. Neighboring sets
Consider sorting the array.
A sliding window where the right most number minus the left most number is $$$<= 2$$$ could tell us a lot of information.
For a window size $$$m$$$, where $$$m >= 3$$$, if the right most number minus the left most number is $$$<= 2$$$, how many triples can we form if we take the current right element as the maximum value? Lets label the numbers in the window as $$$w_{1}, w_{2}, w_{3}, ..., w_{m - 2}, w_{m - 1}, w_{m}$$$. Now if we fix the maximum value to be $$$w_{m}$$$:
- For $$$w_{m - 1}$$$ as the middle value, we can take the remaining $$$m - 2$$$ numbers, $$$w_{1}, w_{2}, w_{3}, ..., w_{m - 2}$$$, to be the minimum number.
- For $$$w_{m - 2}$$$ as the middle value, we can take the remaining $$$m - 3$$$ numbers, $$$w_{1}, w_{2}, w_{3}, ..., w_{m - 3}$$$, to be the minimum number.
- For $$$w_{2}$$$ as the middle value, we can take the remaining number $$$w_{1}$$$ to be the minimum number.
.
.
.
From the above possible middle values, in total, we can form $$$(m - 2) + (m - 3) + ... + 1$$$ triples.
Hence, with the current right value we can form $$$(m - 2)*(m - 1)/2$$$ triples.
t = int(input())
for _ in range(t):
n = int(input())
a = sorted(map(int, input().split()))
left = 0
triples = 0
for right in range(n):
num = a[right]
while num - a[left] > 2:
left += 1
win_size = right - left + 1
if win_size >= 3:
triples += (win_size - 2)*(win_size - 1)//2
print(triples)