House Robber IV [C++]
IntuitionWe want to find the minimal capability such that at least ( k ) houses can be robbed without robbing two adjacent houses. A binary search seems suitabl
# Intuition 
We want to find the minimal capability such that at least \( k \) houses can be robbed without robbing two adjacent houses. A binary search seems suitable here because the capability has a monotonic relationship with the ability to rob at least \( k \) houses.

# Approach
- Apply binary search between the minimal and maximal house values.
- For each mid-value (capability), check if it is possible to rob at least \( k \) houses without robbing adjacent ones, considering only houses with a value ≤ capability.
- If possible, reduce the right bound to mid, aiming for lower capabilities.
- Otherwise, increase the left bound to mid + 1.

# Complexity
- Time complexity:
$$O(n \log M)$$ where \(n\) is the number of houses and \(M\) is the maximum value of a house.

- Space complexity:
$$O(1)$$ since no additional data structures are used apart from input.

# Code
```cpp []
class Solution {
public:
    int minCapability(std::vector<int>& nums, int k) {
        int left = *std::min_element(nums.begin(), nums.end());
        int right = *std::max_element(nums.begin(), nums.end());

        while (left < right) {
            int mid = left + (right - left) / 2;
            if (canRobWithCapability(nums, k, mid))
                right = mid;
            else
                left = mid + 1;
        }

        return left;
    }

private:
    bool canRobWithCapability(const std::vector<int>& nums, int k, int capability) {
        int count = 0;
        int i = 0;
        while (i < nums.size()) {
            if (nums[i] <= capability) {
                count++;
                i++; // Skip the next house
            }
            i++;
        }
        return count >= k;
    }
};
```
['C++']