Problem Statement:
There are N cranes (numbered from 0 to N-1) arranged in a line along a road. Road begins at position 0. The K-th crane is located at distance P[K] from the beginning of the road and its arm length is equal to A[K]. The cranes cannot change their positions.
There is a package, initially located at position B, that has to be moved by the cranes to position E. The K-th crane can pick up the package only if the distance between its position and the package position is less than or equal to A[K] (the package is within arm range from the crane's position). A package can be moved by a crane to an arbitrary position within the crane's arm reach (between P[K] - A[K] and P[K] + A[K] for the K-th crane).
Example:
For example, if P[K] = 5 and A[K] = 3, the K-th crane can move packages anywhere between positions 2 and 8, including both of the boundaries.

Objective:
Determine whether it is possible to move the package from position B to position E using some (possibly all) of the cranes. Any crane can be used an arbitrary number of times.
Write a function to solve this logic.
Every crane defines a specific range or "interval" on the road where it can pick up and drop off a package. Specifically, the K-th crane covers the interval [P[K] - A[K], P[K] + A[K]].
If the working ranges of two cranes overlap or touch, they can pass the package to each other. Therefore, a group of overlapping cranes forms a single, large contiguous "delivery zone." To determine if a package can be moved from point B to point E, we just need to verify if both B and E fall completely within one of these merged continuous zones.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canMovePackage(int b, int e, const vector<int>& p, const vector<int>& a) {
// If the package is already at the destination
if (b == e) return true;
int n = p.size();
if (n == 0) return false;
// Step 1: Create coverage intervals for each crane
vector<pair<int, int>> intervals;
for (int i = 0; i < n; ++i) {
intervals.push_back({p[i] - a[i], p[i] + a[i]});
}
// Step 2: Sort intervals based on starting positions
sort(intervals.begin(), intervals.end());
vector<pair<int, int>> mergedZones;
int current_start = intervals[0].first;
int current_end = intervals[0].second;
// Step 3: Merge overlapping coverage zones
for (int i = 1; i < n; ++i) {
if (intervals[i].first <= current_end) {
// Overlapping intervals: extend the reach if necessary
current_end = max(current_end, intervals[i].second);
} else {
// No overlap: save the current zone and start a new one
mergedZones.push_back({current_start, current_end});
current_start = intervals[i].first;
current_end = intervals[i].second;
}
}
// Add the final zone
mergedZones.push_back({current_start, current_end});
// Step 4: Check if B and E are inside the same continuous zone
int start_pos = min(b, e);
int end_pos = max(b, e);
for (const auto& zone : mergedZones) {
if (zone.first <= start_pos && zone.second >= end_pos) {
return true;
}
}
return false;
}