🔥 100% Beat the Problem Using Binary Search in All Languages! 🚀
🏠 House Robbery with Non-Adjacent Constraint | Binary Search Solution 🏡IntuitionApproachComplexity
Time complexity:

Space complexity:
O(1)Code
# 🏠 House Robbery with Non-Adjacent Constraint | Binary Search Solution 🏡
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
![image.png](https://assets.leetcode.com/users/images/793cc74e-1810-4fdc-b8ca-3df712106a44_1742012843.6557925.png)

# Approach
<!-- Describe your approach to solving the problem. -->
![image.png](https://assets.leetcode.com/users/images/58ea03ab-c40b-4634-b27f-a0e4cf08d46e_1742012860.5201983.png)


# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

![image.png](https://assets.leetcode.com/users/images/baeb580d-b379-4380-8312-b3f4330d3620_1742013099.763563.png)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
# Code
```cpp []
class Solution {
public:
    int minCapability(vector<int>& nums, int k) {
           int minReward = 1, maxReward = *max_element(nums.begin(), nums.end()),
            totalHouses = nums.size();
        while (minReward < maxReward) {
            int midReward = (minReward + maxReward) / 2;
            int possibleThefts = 0;

            for (int index = 0; index < totalHouses; ++index) {
                if (nums[index] <= midReward) {
                    possibleThefts += 1;
                    index++;  // Skip the next house to maintain the
                              // non-adjacent condition
                }
            }

            if (possibleThefts >= k)
                maxReward = midReward;
            else
                minReward = midReward + 1;
        }

        return minReward;
    }
};
```
```java []
import java.util.*;

class Solution {
    public int minCapability(int[] nums, int k) {
        int minReward = 1, maxReward = Arrays.stream(nums).max().getAsInt();
        int totalHouses = nums.length;

        while (minReward < maxReward) {
            int midReward = (minReward + maxReward) / 2;
            int possibleThefts = 0;

            for (int i = 0; i < totalHouses; i++) {
                if (nums[i] <= midReward) {
                    possibleThefts++;
                    i++;  // Skip adjacent house
                }
            }

            if (possibleThefts >= k)
                maxReward = midReward;
            else
                minReward = midReward + 1;
        }

        return minReward;
    }
}
```
```python []
class Solution:
    def minCapability(self, nums, k):
        min_reward, max_reward = 1, max(nums)
        total_houses = len(nums)

        while min_reward < max_reward:
            mid_reward = (min_reward + max_reward) // 2
            possible_thefts = 0

            index = 0
            while index < total_houses:
                if nums[index] <= mid_reward:
                    possible_thefts += 1
                    index += 2 
                else:
                    index += 1

            if possible_thefts >= k:
                max_reward = mid_reward
            else:
                min_reward = mid_reward + 1

        return min_reward
```
``` go []
package main

import (
	"fmt"
	"math"
)

func minCapability(nums []int, k int) int {
	minReward, maxReward := 1, math.MinInt32
	for _, num := range nums {
		if num > maxReward {
			maxReward = num
		}
	}

	for minReward < maxReward {
		midReward := (minReward + maxReward) / 2
		possibleThefts := 0

		for i := 0; i < len(nums); i++ {
			if nums[i] <= midReward {
				possibleThefts++
				i++ // Skip adjacent house
			}
		}

		if possibleThefts >= k {
			maxReward = midReward
		} else {
			minReward = midReward + 1
		}
	}

	return minReward
}

func main() {
	nums := []int{1, 2, 3, 4, 5}
	k := 2
	fmt.Println(minCapability(nums, k))
}
```
```rust []
fn min_capability(nums: Vec<i32>, k: i32) -> i32 {
    let mut min_reward = 1;
    let mut max_reward = *nums.iter().max().unwrap();

    while min_reward < max_reward {
        let mid_reward = (min_reward + max_reward) / 2;
        let mut possible_thefts = 0;
        let mut i = 0;

        while i < nums.len() {
            if nums[i] <= mid_reward {
                possible_thefts += 1;
                i += 1; // Skip adjacent house
            }
            i += 1; // Move to next house
        }

        if possible_thefts >= k {
            max_reward = mid_reward;
        } else {
            min_reward = mid_reward + 1;
        }
    }

    min_reward
}
```
``` ruby []
def min_capability(nums, k)
  min_reward, max_reward = 1, nums.max

  while min_reward < max_reward
    mid_reward = (min_reward + max_reward) / 2
    possible_thefts, i = 0, 0

    while i < nums.length
      if nums[i] <= mid_reward
        possible_thefts += 1
        i += 1 # Skip adjacent house
      end
      i += 1 # Move to next house
    end

    if possible_thefts >= k
      max_reward = mid_reward
    else
      min_reward = mid_reward + 1
    end
  end

  min_reward
end
```
```c# []
using System;
using System.Linq;

public class Solution {
    public int MinCapability(int[] nums, int k) {
        int minReward = 1, maxReward = nums.Max();
        
        while (minReward < maxReward) {
            int midReward = (minReward + maxReward) / 2;
            int possibleThefts = 0;

            for (int i = 0; i < nums.Length; i++) {
                if (nums[i] <= midReward) {
                    possibleThefts++;
                    i++; // Skip adjacent house
                }
            }

            if (possibleThefts >= k)
                maxReward = midReward;
            else
                minReward = midReward + 1;
        }

        return minReward;
    }
}
```
```javascript []
function minCapability(nums: number[], k: number): number {
    let minReward = 1, maxReward = Math.max(...nums);

    while (minReward < maxReward) {
        let midReward = Math.floor((minReward + maxReward) / 2);
        let possibleThefts = 0;

        for (let i = 0; i < nums.length; i++) {
            if (nums[i] <= midReward) {
                possibleThefts++;
                i++; // Skip adjacent house
            }
        }

        if (possibleThefts >= k)
            maxReward = midReward;
        else
            minReward = midReward + 1;
    }

    return minReward;
}
```
![image.png](https://assets.leetcode.com/users/images/cf091c81-63e5-4150-b321-10a3dfb66d0e_1742013284.1075637.png)


['Binary Search', 'Python', 'C++', 'Java', 'Go', 'TypeScript', 'Rust', 'Ruby', 'JavaScript', 'C#']