C++ Easy solution, with Explanation
Lets see about the brute force solution.\n\nWe can check for candies=1,2 and so,on until we find an answer.\n\nCan\'t we do something better,instead of linear s
Lets see about the brute force solution.\n\nWe can check for candies=1,2 and so,on until we find an answer.\n\nCan\'t we do something better,instead of linear search ?.\n\nYes, we can do **binary search.**\n \nIf we can divide the candies into piles containing x number of candies all the numbers below x,\nwill also be satisfied.\n\nIn this question we have to find the maximum number of candies.\n\nLets see the lowest value is 1 and highest value as the maximum element in candies.\n\nNow we will find the mid, and we will check weather we can divide mid amount of candies into piles \nsuch that they will be sufficient for k children.\n\nFinally we return the ans.\n\n```\nclass Solution {\npublic:\n    typedef long long ll;\n    \n    bool solve(vector<int>& v, ll mid, ll k){\n        int n = v.size();\n        ll cnt = 0;\n        for(int i=0;i<n;i++){\n            cnt += (v[i]/mid);\n            if(cnt>=k) return true;\n        }\n        return false;\n    }\n    \n    // v is the candies vector.\n    int maximumCandies(vector<int>& v, long long k) {\n        int n = v.size();\n\t\tint mx = 0;\n        for(int i=0;i<n;i++){\n\t\t\tmx = max(mx,v[i]);\n        }\n        \n        ll low = 1, high = mx;\n        ll ans = 0;\n        while(low<=high){\n            ll mid = low + (high-low)/2;\n            if(solve(v,mid,k)){\n                ans = mid;\n                low = mid+1;\n            }\n            else{\n                high = mid-1;\n            }\n        }\n        return ans;\n    }\n};\n```\n\n**Time Complexity= O(nlogp)   // p is the maximum candy.\nSpace Complexity- O(1);**\n\n\nUpvote if it helps!!\nThank you!!\n\nMore binary search problems:\n\nhttps://leetcode.com/problems/capacity-to-ship-packages-within-d-days/\nhttps://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/\nhttps://leetcode.com/problems/find-the-duplicate-number/\nhttps://leetcode.com/problems/minimum-size-subarray-sum/\nhttps://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/\nhttps://leetcode.com/problems/most-beautiful-item-for-each-query/\nhttps://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/\nhttps://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/\nhttps://leetcode.com/problems/heaters/\nhttps://leetcode.com/problems/minimum-absolute-sum-difference/\nhttps://leetcode.com/problems/frequency-of-the-most-frequent-element/
['Binary Search', 'C']