C++ || Binary Search || Simple Explanation
Approach\n Describe your approach to solving the problem. \nWhat comes to my mind is to check what number of candies(search space) is satisfying children.\nSo b
# Approach\n<!-- Describe your approach to solving the problem. -->\nWhat comes to my mind is to check what number of candies(search space) is satisfying children.\nSo by this intuition we can find the search space. \n\nMinimum candy child can  get will be 1:\nExample: [1,2,3,4] and k=5\nTotal 5 children and all of them should have same amount of candies\nso for example let candy=2 now check whether we can get 2 candies from all the piles or not\n1st pile:- candies[0]=1 and we can\'t get 2 candies -> count=0\n2nd pile:- candies[1]=2 we can get 2 candies -> count=1\n3rd pile:- candies[2]=3 we can get 2 candies -> count=2\n4th pile:- candies[3]=4 we can get total pair of 2 candy -> count=4\n\nAt end we can only satisfy 4 children but we need to satisfy 5 children(k=5)\nso minimum value child can get will be 1 candy.\n\nMaximum candy child can get will be maximum element in candies:\nExample candies=[1,2,3,4] and k=1\nHere we need to satisy 1 children and it should have maximum candy we can just allocate all candy to single children \nso maximum value of candy child can get will be 4.\n\nWe got our search space here\nstart=1 and end=max_element\n\nNow approach is to apply binary search and check whether particular number of candies satisfies all the children or not\nExample: candies=[5,8,6], k = 3\nsuppose we reached mid=5 now count the number of 5 candies from each pile:\n```\n bool isSatisfying(vector<int> candies,long long mid ,long long k){\n        long long count=0;\n\n        for(auto x: candies){\n            count=count+(x/mid);\n        }\n        if(count>=k) return true;\n        return false;\n    }\n```\nWe are dividing each piles with mid value and checking how many number of 5 candies we can get.\nFor [5,8,6]->\n    Each child gets 1 pile of total 5 candies\n    5/5=1 -> For first child\n    8/5=1 -> For second child\n    6/5=1 -> For third child\n\nWe can distribute max 5 candies to 3 children\nIf count is greater or equal to children then we can distribute equal candies to all children .\n\n# Complexity\n- Time complexity:\n $$O(n(log(end)))$$ where n=candies.size() and end=max_element\n\n- Space complexity\n    $$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n    bool isSatisfying(vector<int> candies,long long mid ,long long k){\n        long long count=0;\n\n        for(auto x: candies){\n            count=count+(x/mid);\n        }\n\n        if(count>=k) return true;\n        return false;\n    }\n    int maximumCandies(vector<int>& candies, long long k) {\n        int start=1;\n        int end=*max_element(candies.begin(),candies.end());\n        int ans=0;\n\n        while(start<=end){\n            int mid=start+(end-start)/2;\n            if(isSatisfying(candies,mid,k)){\n                //we need to maximize search space so storing answer and finding anther maxi\n                ans=mid;\n                start=mid+1;\n            }\n            else{\n                end=mid-1;\n            }\n        }\n\n        return ans;\n    }\n};\n```
['Array', 'Math', 'Binary Search', 'C++']