Easy C++ Solution | Step-wise Approach |  Beats 97.73% | Binary Search
Intuition and Approach\n\nThe problem involves distributing candies into piles such that each pile contains a certain number of candies. The goal is to maximize
# Intuition and Approach\n\nThe problem involves distributing candies into piles such that each pile contains a certain number of candies. The goal is to maximize the number of candies in each pile while ensuring that at least k piles are formed.\n\n-- The maximum candies a person can get would be the maximum element present in the candies array. \n\n1) **Initial Bounds**: \n-- Initially, we set our lower bound ($$low$$) to 1, as we can\'t have a pile with zero candies, and our upper bound (high) to the maximum number of candies in the given candies array.\n\n2) **Binary Search:** \n-- Since we want to maximize the number of candies in each pile, we can use binary search to efficiently find the optimal number. \n-- We\'ll iteratively narrow down our search space until we find the maximum number of candies that satisfies the condition of having at least k piles.\n\n3) **Checking Validity:** \n-- The $$isValid()$$ function checks whether it\'s possible to form at least k piles with a given number of candies (mid) per pile. It iterates through the candies vector, calculates how many piles can be formed with the current number of candies per pile ($$mid$$), and checks if this count is greater than or equal to k.\n\n4) **Updating Bounds:** \n-- Based on the result of the validity check, we update our search bounds accordingly. \n-- If it\'s possible to form at least k piles with $$mid$$ candies per pile, we update the result and adjust our lower bound to mid + 1, indicating that we can potentially increase the number of candies per pile. \n-- Otherwise, we adjust our upper bound to mid - 1, indicating that we need to decrease the number of candies per pile.\n\n-------\n\n# Complexity\n- Time complexity: O(N*log(max_candies))\n \n-- where N is the number of candies in the input vector and max_candies is the maximum number of candies in the input vector.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n-----\n\n### If you find this solution helpful, consider giving it an upvote!\n\n#### Do check out all other solutions posted by me here: https://github.com/adityajamwal02/Leetcode-Community-Solutions\n---\n\n# Code\n```\nclass Solution {\npublic:\n    bool isValid(vector<int> &candies, long long k, long long mid){\n        long long counter=0;    // No. of piles\n        for(auto it : candies){\n            counter+=(it/mid);\n        }\n    return counter>=k;\n    }\n    \n    int maximumCandies(vector<int>& candies, long long k) {\n        long long n=candies.size(), low=1, high=*max_element(candies.begin(),candies.end()), result=0;\n        while(low<=high){\n            long long mid=low+(high-low)/2;\n            if(isValid(candies,k,mid)){\n                result=mid;\n                low=mid+1;\n            }else{\n                high=mid-1;\n            }\n        }\n    return (int)result;\n    }\n};\n```
['Array', 'Binary Search', 'C++']