Easy & Detailed Approch ✅🚀 | Binary Search 🔎 | C++ | Java | Python | Js 🔥 
Intuition🧠 The key idea is to use Binary Search to efficiently find the maximum number of candies that can be distributed per child. Since the number of candies
# Intuition
🧠 The key idea is to use **Binary Search** to efficiently find the maximum number of candies that can be distributed per child. Since the number of candies per child can range between 1 and the maximum candy count, Binary Search optimizes the search space.

# Approach
🚀 Steps to solve the problem:
1. **Sort** the `candies` array to simplify the Binary Search process.
2. Use **Binary Search** with `start = 1` and `end = max(candies)`. 
3. In each iteration, calculate `mid` as the potential maximum candies per child.
4. Use the `solve` function to check if distributing `mid` candies per child is possible.
   - In `solve`, iterate through the array and calculate the total number of pieces each element can contribute.
5. If `solve` returns `true`, update `answer` and move the `start` pointer to `mid + 1`.
6. Otherwise, move the `end` pointer to `mid - 1`.
7. Return the maximum valid answer.

# Complexity
⏳ **Time Complexity:** $$O(n \log m)$$  
- `n` is the size of the `candies` array.  
- `m` is the maximum number of candies in the array (range of binary search).

🧱 **Space Complexity:** $$O(1)$$  
- Only a few extra variables are used for calculation.


# Code
```cpp []
class Solution {
public:
    // Function to check if 'val' candies can be given to at least 'k' children
    bool solve(vector<int>& candies, long long k, int val) {
        long long total_pieces = 0; // Total pieces we can create

        for (int i = 0; i < candies.size(); i++) {
            if (val > candies[i]) {
                continue; // Skip if current candy count is less than 'val'
            } else {
                total_pieces += candies[i] / val; // Count how many pieces we can create
            }
        }

        return total_pieces >= k; // Return true if we can distribute enough pieces
    }

    int maximumCandies(vector<int>& candies, long long k) {
        sort(candies.begin(), candies.end()); // Sort candies array
        int start = 1;                         // Minimum possible candy count per child
        int end = candies[candies.size() - 1]; // Maximum possible candy count per child
        int answer = 0;                        // Store the maximum valid answer

        // Binary Search to find the maximum valid number of candies per child
        while (start <= end) {
            int mid = (start + end) / 2; // Midpoint for binary search

            if (solve(candies, k, mid)) {
                answer = mid; // Store the valid value
                start = mid + 1; // Try for a bigger value
            } else {
                end = mid - 1; // Try for a smaller value
            }
        }

        return answer; // Return the maximum valid answer
    }
};
```
```javascript []
var maximumCandies = function(candies, k) {
    // Function to check if 'val' candies can be distributed to at least 'k' children
    const solve = (candies, k, val) => {
        let totalPieces = 0; // Track total number of candy pieces
        
        // Iterate through each candy count
        for (let candy of candies) {
            if (val <= candy) { // Only count valid pieces
                totalPieces += Math.floor(candy / val); 
            }
        }
        return totalPieces >= k; // Return true if enough pieces are possible
    };

    // Sort the candies array for efficient binary search
    candies.sort((a, b) => a - b);
    let start = 1, end = candies[candies.length - 1], answer = 0;

    // Binary search to find the maximum number of candies per child
    while (start <= end) {
        let mid = Math.floor((start + end) / 2); // Calculate mid value

        if (solve(candies, k, mid)) {
            answer = mid; // Update valid answer
            start = mid + 1; // Try for larger values
        } else {
            end = mid - 1; // Reduce search range
        }
    }
    return answer; // Return the maximum valid answer
};
```
```python []
def maximumCandies(candies, k):
    # Function to check if 'val' candies can be distributed to at least 'k' children
    def solve(candies, k, val):
        total_pieces = sum(candy // val for candy in candies if val <= candy)
        return total_pieces >= k

    # Sort the candies array for efficient binary search
    candies.sort()
    start, end, answer = 1, candies[-1], 0

    # Binary search to find the maximum number of candies per child
    while start <= end:
        mid = (start + end) // 2  # Calculate mid value
        if solve(candies, k, mid):
            answer = mid  # Update valid answer
            start = mid + 1  # Try for larger values
        else:
            end = mid - 1  # Reduce search range

    return answer  # Return the maximum valid answer
```
```java []
import java.util.*;
class Solution {
    // Function to check if 'val' candies can be distributed to at least 'k' children
    public boolean solve(int[] candies, long k, int val) {
        long totalPieces = 0; // Track total number of candy pieces
        for (int candy : candies) {
            if (val <= candy) {
                totalPieces += candy / val; // Add possible pieces
            }
        }
        return totalPieces >= k; // Return true if enough pieces are possible
    }

    public int maximumCandies(int[] candies, long k) {
        Arrays.sort(candies); // Sort the array for efficient binary search
        int start = 1, end = candies[candies.length - 1], answer = 0;

        // Binary search to find the maximum number of candies per child
        while (start <= end) {
            int mid = (start + end) / 2; // Calculate mid value

            if (solve(candies, k, mid)) {
                answer = mid; // Update valid answer
                start = mid + 1; // Try for larger values
            } else {
                end = mid - 1; // Reduce search range
            }
        }
        return answer; // Return the maximum valid answer
    }
}
```

['Array', 'Binary Search', 'C++', 'Java', 'Python3', 'JavaScript']