✅🚀 easy binary search code with explanation in all language
exampleComplexityCode

![image.png](https://assets.leetcode.com/users/images/5f703938-faa7-48f5-8f32-9c229a441dcb_1741930962.771123.png)
# example
![image.png](https://assets.leetcode.com/users/images/6c74d9b1-7f22-4efd-b111-820271e4d294_1741930997.3041086.png)


![image.png](https://assets.leetcode.com/users/images/d6d281ec-a925-4c7d-82da-2f6c69d51906_1741931018.6082027.png)


![image.png](https://assets.leetcode.com/users/images/d2d5de6c-103b-4a73-bfe3-4666307fd356_1741931027.9107063.png)

![image.png](https://assets.leetcode.com/users/images/8d79dc29-9ab1-4538-96d7-ba5b49efc7e4_1741931045.4513817.png)





# Complexity
![image.png](https://assets.leetcode.com/users/images/2274c0bc-3bf1-4ee7-997c-0b33c4f3b9f2_1741931206.6777086.png)


# Code
```cpp []
class Solution {
public:
    int maximumCandies(vector<int>& c, long long k) {
        long long sum=accumulate(c.begin(),c.end(),0LL);
        if(sum<k)return 0;
         int low = 1, high = *max_element(c.begin(), c.end()), ans = 0;

    while (low <= high) {
        int mid = low + (high - low) / 2;
        long long count = 0;
        
        for (int candies : c) count += candies / mid;
        
        if (count >= k) { 
            ans = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return ans;
    }
};
```
```java []
import java.util.*;

class Solution {
    public static int maximumCandies(int[] c, long k) {
        long sum = 0;
        for (int num : c) sum += num;
        if (sum < k) return 0;

        int low = 1, high = Arrays.stream(c).max().getAsInt(), ans = 0;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            long count = 0;
            for (int candies : c) {
                count += candies / mid;
                if (count >= k) break;
            }
            if (count >= k) {
                ans = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] c = {5, 8, 6};
        long k = 3;
        System.out.println(maximumCandies(c, k)); // Output: 5
    }
}
```
``` python []
def maximumCandies(c, k):
    if sum(c) < k:
        return 0

    low, high, ans = 1, max(c), 0
    while low <= high:
        mid = (low + high) // 2
        count = sum(candies // mid for candies in c)
        if count >= k:
            ans = mid
            low = mid + 1
        else:
            high = mid - 1
    return ans

print(maximumCandies([5, 8, 6], 3))  # Output: 5

```
```rust []
fn maximum_candies(c: Vec<i32>, k: i64) -> i32 {
    let sum: i64 = c.iter().map(|&x| x as i64).sum();
    if sum < k {
        return 0;
    }

    let (mut low, mut high, mut ans) = (1, *c.iter().max().unwrap(), 0);
    while low <= high {
        let mid = (low + high) / 2;
        let count: i64 = c.iter().map(|&candies| (candies as i64) / (mid as i64)).sum();
        if count >= k {
            ans = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    ans
}

fn main() {
    let c = vec![5, 8, 6];
    let k = 3;
    println!("{}", maximum_candies(c, k)); // Output: 5
}
```
``` ruby []
def maximum_candies(c, k)
  return 0 if c.sum < k

  low, high, ans = 1, c.max, 0
  while low <= high
    mid = (low + high) / 2
    count = c.sum { |candies| candies / mid }
    
    if count >= k
      ans = mid
      low = mid + 1
    else
      high = mid - 1
    end
  end
  ans
end

puts maximum_candies([5, 8, 6], 3)  # Output: 5

```
``` go []
package main

import (
	"fmt"
	"sort"
)

func maximumCandies(c []int, k int64) int {
	sum := int64(0)
	for _, num := range c {
		sum += int64(num)
	}
	if sum < k {
		return 0
	}

	low, high, ans := 1, c[len(c)-1], 0
	for low <= high {
		mid := (low + high) / 2
		count := int64(0)
		for _, candies := range c {
			count += int64(candies) / int64(mid)
			if count >= k {
				break
			}
		}
		if count >= k {
			ans = mid
			low = mid + 1
		} else {
			high = mid - 1
		}
	}
	return ans
}

func main() {
	fmt.Println(maximumCandies([]int{5, 8, 6}, 3)) // Output: 5
}
```
``` javascript []
function maximumCandies(c, k) {
    let sum = c.reduce((a, b) => a + b, 0);
    if (sum < k) return 0;

    let low = 1, high = Math.max(...c), ans = 0;
    while (low <= high) {
        let mid = Math.floor((low + high) / 2);
        let count = c.reduce((acc, candies) => acc + Math.floor(candies / mid), 0);
        if (count >= k) {
            ans = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return ans;
}

console.log(maximumCandies([5, 8, 6], 3)); // Output: 5
```

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