Problem 1: Threshold-based Item Counting
The first challenge involves determining how many items in a fixed-size collection (10 elements) satisfy a specific condition. The logic requires comparing each item's value against a threshold value. This threshold is derived from a base input value added to a constant offset of 30 units.
The solution involves iterating through the collection of values, performing the comparison, and incrementing a counter for every successful match.
#include <iostream>
#include <vector>
int main() {
std::vector<int> heights(10);
for (int i = 0; i < 10; ++i) {
std::cin >> heights[i];
}
int reach_limit;
std::cin >> reach_limit;
const int BENCH_OFFSET = 30;
int count = 0;
for (int h : heights) {
if (reach_limit + BENCH_OFFSET >= h) {
count++;
}
}
std::cout << count;
return 0;
}
The time complexity for this operation is O(1) since the input size is constant.
Problem 2: Interval Marking on a Linear Axis
This problem involves managing a set of points located on a linear axis from 0 to L. We are given m intervals defined by start and end coordinates. Any point falling within these intervals must be marked as removed. The objective is to calculate the number of points that remain unmarked after processing all intervals.
Given the constraint that L does not exceed 10,000, a boolean array or vector is an efficient data structure to track the status of each point. We initialize all positions as "present" (false), iterate through the provided intervals to mark the specific ranges as "removed" (true), and finally count the indices that remain false.
#include <iostream>
#include <vector>
int main() {
int road_length, zone_count;
std::cin >> road_length >> zone_count;
// Indices 0 through road_length represent the trees
std::vector<bool> is_cut(road_length + 1, false);
for (int i = 0; i < zone_count; ++i) {
int start, end;
std::cin >> start >> end;
for (int j = start; j <= end; ++j) {
is_cut[j] = true;
}
}
int remaining = 0;
for (int i = 0; i <= road_length; ++i) {
if (!is_cut[i]) {
remaining++;
}
}
std::cout << remaining;
return 0;
}
The time complexity is O(L * m), which is optimal given the problem constraints.