A. Missing Level
If you had all numbers from 1 to n, what would their total sum be?
Subtract the numbers Daniel already played from that total. The remaining number is the missing level.
Sum of 1..n = n*(n+1)//2. Subtract the sum of given numbers → missing level. Time: O(n)
n = int(input()) arr = list(map(int, input().split())) total = n * (n + 1) // 2 print(total — sum(arr))
From a free cell you can jump at most k cells ahead. If there is a block of consecutive obstacles, you must be able to jump over it — what is required from k relative to the block size?
If a block of # is too long and the robot cannot jump that far, he will get stuck.
Count consecutive #. If any group has length ≥ k, the robot cannot pass. Otherwise, he can reach the end.
Time Complexity: O(n)
n, k = map(int, input().split())
s = input().strip()
count = 0
for ch in s:
if ch == '#':
count += 1
if count >= k:
print("NO")
break
else:
count = 0
else:
print("YES")
Go from left to right. Keep track of the maximum height allowed for the current book.
For each book, try the larger side if possible. If not, use the smaller side.
- Set
prevto a very large number. - For each book: — Let
big = max(w, h),small = min(w, h). — Ifbig <= prev, choosebig.
— Else ifsmall <= prev, choosesmall.
— Else answer is NO. - If all books work, answer is YES.
Time Complexity: O(n)
n = int(input())
prev = 10**18
for _ in range(n):
w, h = map(int, input().split())
big = max(w, h)
small = min(w, h)
if big <= prev:
prev = big
elif small <= prev:
prev = small
else:
print("NO")
break
else:
print("YES")
Check the total size without compression first. Then, if needed, compress files that reduce the most size.
Sort files by how much size they reduce. Apply the biggest reductions first.
- Let
total = sum(all original sizes). - If
total <= m, answer is 0. - Else, compute how much each file reduces, sort reductions decreasing, apply them one by one until
total <= m. - If even after compressing all files,
total > m, print -1.
Time Complexity: O(n log n)
n, m = map(int, input().split())
total = 0
saves = []
for _ in range(n):
a, b = map(int, input().split())
total += a
saves.append(a — b)
if total <= m:
print(0)
else:
saves.sort(reverse=True)
count = 0
for s in saves:
total -= s
count += 1
if total <= m:
print(count)
break
else:
print(-1)
Sort all blocks from smallest to largest. Some may already be next to each other in a tower.
If two consecutive numbers in sorted order are already next to each other in one tower, we don’t need to split them.
Count how many consecutive pairs we can keep. The rest may need splits.
- Record each block’s tower and position.
- Sort all blocks.
- Count “good adjacencies”: consecutive sorted blocks in the same tower.
splits = (total_blocks - good_adj) - number_of_towerscombines = splits + (number_of_towers - 1)
Time Complexity: O(N log N)
n = int(input())
pos = {}
vals = []
for t in range(n):
data = list(map(int, input().split()))
k = data[0]
blocks = data[1:]
for i, v in enumerate(blocks):
pos[v] = (t, i)
vals.append(v)
vals.sort()
good = 0
for i in range(len(vals) — 1):
t1, p1 = pos[vals[i]]
t2, p2 = pos[vals[i+1]]
if t1 == t2 and p2 == p1 + 1:
good += 1
N = len(vals)
segments = N — good
splits = segments — n
combines = segments — 1
print(splits, combines)



