Easy to Understand & Simplified Solution using Binary Search
\n# Please upvote my solution I am crying :-(\n# Intuition\n Describe your first thoughts on how to solve this problem. \nEasy to understand problem by using Bi
\n# Please upvote my solution I am crying :-(\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEasy to understand problem by using Binary Search in an effective way:))\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1) Problem Breakdown:\n\nYou have a list of integers candies, where each integer represents the number of candies in a pile.\n\nYou need to distribute these candies to k children such that each child receives the maximum possible equal number of candies.\n\nThe goal is to determine this maximum number.\n\n\n2) Binary Search Approach:\n\nThe problem can be approached using binary search because we are looking for the maximum number of candies each child can get, which can be treated as a search for a maximum value in a sorted range.\n\n\n3) Range of Binary Search:\n\nThe minimum possible value (left) for the candies each child can receive is 1 (each child gets at least one candy if possible).\n\nThe maximum possible value (right) is the size of the largest pile of candies (*max_element(candies.begin(), candies.end())), because no child can receive more candies than the largest pile.\n\n\n4) Binary Search Execution:\n\nThe code iteratively narrows down the possible values for the maximum number of candies each child can receive (mid).\n\nFor each midpoint value (mid), the code calculates the total number of children that can be satisfied if each child gets mid candies using the formula sum += i / mid for each pile i.\n\nIf the number of children that can be satisfied (sum) is less than k, it means mid is too large, so the search space is reduced to the left half (right = mid).\n\nIf the number of children that can be satisfied is at least k, it means mid is feasible, so the search space is shifted to the right half (left = mid + 1).\n\n\n5) Result:\n\nThe loop continues until left is no longer less than right.\n\nThe result is left - 1, which is the maximum number of candies each child can receive.\n# Complexity\n- Time complexity: O(n log n)\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# Code\n```\nclass Solution {\npublic:\n    int maximumCandies(vector<int>& candies, long long k) {\n        int left = 1;\n        int right = *max_element(candies.begin(), candies.end()) + 1; // Binary search range\n        while (left < right) {\n            long long sum = 0;\n            int mid = (left + right) / 2; // Midpoint\n            for (auto i : candies) {\n                sum += i / mid; // Calculate total children satisfied with mid candies each\n            }\n            if (k > sum) { // If fewer children are satisfied\n                right = mid; // Move left\n            } else {\n                left = mid + 1; // Move right\n            }\n        }\n        return left - 1; // Result is left - 1\n    }\n};\n\n```
['Array', 'Binary Search', 'C++']