100% faster | C++ | Python | Java | Binary Search with Logic
\n\nIdea\n\n start can be taken as 1 , highest we can distribute equally is average.\n\n Use Binary Search and try to distribute the candies equal to mid, if po
![image](https://assets.leetcode.com/users/images/82a7e416-2997-4676-929e-712206ab4b1a_1648961313.8131979.png)\n\n**Idea**\n\n* start can be taken as 1 , highest we can distribute equally is average.\n\n* Use Binary Search and try to distribute the candies equal to mid, if possible try to maximize it by moving on right, else if not possible try it by decreasing the value by moving on left\n* **TC: O(NlogN) SC: O(1)**\n\n```\ndef canSplit(candies, mid, k):\n    split = 0\n    for i in candies:\n        split += i//mid\n    if split >= k:\n        return True\n    else:\n        return False\n    \nclass Solution:\n    def maximumCandies(self, candies: List[int], k: int) -> int:\n        end = sum(candies)//k\n        start = 1\n        ans = 0\n        while start <= end:\n            mid = (start + end)//2\n            if canSplit(candies, mid, k):\n                start = mid + 1\n                ans = mid\n            else:\n                end = mid - 1\n        return ans\n        \n```\n\n**C++**\n\n```\nclass Solution {\npublic:\n    bool canSplit(vector<int>& candies, long long k, long long mid) {\n        long long split = 0;\n        for(int i = 0; i < candies.size(); ++i) {\n            split += candies[i]/mid;\n        }   \n        if(split >= k)\n            return true;\n        else\n            return false;\n    }\n    \n    int maximumCandies(vector<int>& candies, long long k) {\n        long long sum = 0;\n        for(int i = 0; i < candies.size(); ++i) {\n            sum += candies[i];\n        }\n        long long start = 1, end = sum/k;\n        long long ans = 0;\n        while(start <= end) {\n            long long mid = (start + end)/2;\n            if(canSplit(candies, k, mid)) {\n                ans = mid;\n                start = mid + 1;\n            } else {\n                end = mid-1;\n            }\n        }\n        return ans;\n    }\n};\n```\n\n**Java**\n```\nclass Solution {\n    public boolean canSplit(int[] candies, long k, long mid) {\n        long split = 0;\n        for(int i = 0; i < candies.length; ++i) {\n            split += candies[i]/mid;\n        }   \n        if(split >= k)\n            return true;\n        else\n            return false;\n    }\n    \n    public int maximumCandies(int[] candies, long k) {\n        long sum = 0;\n        for(int i = 0; i < candies.length; ++i) {\n            sum += candies[i];\n        }\n        long start = 1, end = sum;\n        long ans = 0;\n        while(start <= end) {\n            long mid = (start + end)/2;\n            if(canSplit(candies, k, mid)) {\n                ans = mid;\n                start = mid + 1;\n            } else {\n                end = mid-1;\n            }\n        }\n        return (int)ans;\n    }\n}\n```
['Binary Search', 'C', 'Python', 'C++', 'Java']