Easy binary search solution in java. Optimized approach, with intuition
IntuitionThe problem requires us to rob exactly k houses such that no two adjacent houses are robbed while minimizing the maximum amount among the robbed houses
# Intuition
The problem requires us to rob exactly `k` houses such that no two adjacent houses are robbed while minimizing the maximum amount among the robbed houses.

- Since we need to find the **minimum possible capability (`m`)**, we can use **binary search** on the possible values of `m`.
- The idea is to check if we can rob `k` houses with a given `m` and adjust our search range accordingly.

---

# Approach
1. **Find the Range of `m`**
   - The minimum possible `m` is the smallest element in `nums` (lowest house value).
   - The maximum possible `m` is the largest element in `nums` (highest house value).

2. **Binary Search on `m`**
   - Set `min = smallest value in nums`, `max = largest value in nums`.
   - Perform a **binary search** on this range to find the smallest `m` that allows robbing exactly `k` houses.
   - **Check feasibility with `stealOrNot(nums, k, m)`**:
     - Try to rob houses where `nums[i] ≤ m`, ensuring **no adjacent houses** are robbed.
     - If we can rob `k` houses, decrease `m` (search in the **lower** range).
     - Otherwise, increase `m` (search in the **higher** range).
   - The answer is the smallest valid `m`.

---

# Complexity

- **Time Complexity:**  
  - Finding `min` and `max` takes **O(n)**.
  - Binary search on `m` runs in **O(log(max-min))** iterations.
  - Each binary search step checks feasibility using **O(n)** iteration.
  - **Overall complexity: O(n log(max-min))**.

- **Space Complexity:**  
  - We use only a few extra variables.
  - **O(1) (constant space complexity)**.

# Code
```java []
class Solution {
    private boolean stealOrNot(int[] nums, int k, int m) {

        for(int i = 0; i < nums.length; i++) {
            if(nums[i] <= m) {
                k--;
                i++;
            }
            if(k <= 0) {
                return true;
            }
        }
        return false;
    }
    public int minCapability(int[] nums, int k) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;

        for(int i = 0; i < nums.length; i++) {
            min = Math.min(min, nums[i]);
            max = Math.max(max, nums[i]);
        }
        int ans = max;
        while(min <= max) {
            int mid = (min + max)/2;

            if(stealOrNot(nums, k, mid)) {
                max = mid - 1;
                ans = mid;
            }
            else {
                min = mid + 1;
            }
        }
        return ans;

    }
}
```
['Array', 'Binary Search', 'Java']