Here is the link to the contest. All problems are from Codeforces' problemset.
A. Nth Digit in Sequence
B. Anansi and Trip-Photographs
Input handling
t = int(input()) # Number of test cases for _ in range(t): n, x = map(int, input().split()) # Read n and x heights = list(map(int, input().split())) # Read 2n heights
heights.sort() # Step 1: Sort the heights front_row = heights[:n] # Step 2: First n people go to the front row back_row = heights[n:] # Step 3: Last n people go to the back row # Step 4: Check if each person in the back row is at least x units taller for j in range(n): if back_row[j] - front_row[j] < x: print("NO") break else:
print("YES") ```
Time Complexity
- Sorting the array: (O(2n \log (2n)) = O(n \log n))
- Splitting into front and back rows: (O(n))
- Checking the height condition: (O(n))
Total Time Complexity:
[ O(n \log n) + O(n) + O(n) = O(n \log n) ] which is efficient for ( n \leq 100 ).
Space Complexity
- Input storage: (O(2n)) for storing heights.
- Two separate lists: (O(n)) each for front and back row.
- Overall auxiliary space: (O(n))
Since sorting is done in-place, Total Space Complexity = O(n).
C. Minimal TV Subscriptions
The problem asks us to determine the minimum number of TV show subscriptions needed to ensure that we can watch at least d consecutive days without missing any episodes. The key is to use a sliding window approach to efficiently count the number of unique shows in each window of d consecutive days.
- Sliding Window: We first initialize a window with the first d shows and count how many unique shows are in that window using a hash table (Counter). Then, as the window slides forward by one day, we update the count of unique shows by adding the new day’s show and removing the show that is no longer in the window.
- Efficiency: Instead of recalculating the number of unique shows for every possible window from scratch, we only adjust the window by removing one show and adding another, making the solution efficient.
We start by counting the unique shows in the first d days. As the window slides through the entire schedule, we keep track of the minimum number of unique shows across all windows of length d. This minimum gives us the answer.