Using Binary and greedy concept || cpp || fast one
IntuitionThis solution leverages binary search and greedy selection to efficiently find the minimum possible value for the non-contiguous subsequence of size kA
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
This solution leverages binary search and greedy selection to efficiently find the minimum possible value for the non-contiguous subsequence of size k
# Approach
<!-- Describe your approach to solving the problem. -->

isOk() Function:

This function checks if it's possible to pick k non-consecutive elements from the array where each selected element is less than or equal to the current binary search mid value.
It increments count whenever an element less than or equal to num is selected.
If count reaches k, the function returns true.
Non-adjacency is ensured by skipping the next element (i++).
minCapability():

Binary search is performed between minElem and maxElem.
In each iteration, we calculate mid and use isOk() to check if k non-consecutive elements can be selected with values less than or equal to mid.
If isOk() returns true, this means that mid could be a valid capability, and we try to minimize further.
If isOk() returns false, we increase the lower bound (minElem = mid + 1).
Binary Search Result:

The final result is the smallest value num such that it is possible to select k non-adjacent elements less than or equal to num.

Hope you understood this approach. Please upvote this:)
Thanks and keep learning!!

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


# Code
```cpp []
class Solution {
public:
    // Helper function to check if we can pick 'k' non-contiguous elements <= num
    bool isOk(int num, vector<int>& nums, int k) {
        int count = 0;  // Count of selected elements

        // Iterate over the array and try to select elements <= num
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] <= num) {
                count++;  // Include this element

                // Skip the next element to ensure non-adjacency
                i++;

                // If we've selected 'k' elements, return true
                if (count >= k) {
                    return true;
                }
            }
        }

        // If we can't select 'k' elements, return false
        return false;
    }

    int minCapability(vector<int>& nums, int k) {
        // Initialize binary search range with the minimum and maximum elements in the array
        int minElem = *min_element(nums.begin(), nums.end());
        int maxElem = *max_element(nums.begin(), nums.end());

        // Binary search for the minimum value
        while (minElem < maxElem) {
            int mid = minElem + (maxElem - minElem) / 2;

            // Check if 'mid' can be a valid capability
            if (isOk(mid, nums, k)) {
                maxElem = mid;  // Try to minimize further
            } else {
                minElem = mid + 1;  // Increase the lower bound
            }
        }

        return minElem;  // This is the minimum valid capability
    }
};

```
['C++']