C++ | O(N) solution with explanation | Binary Search
Code:\nTC: O(N), SC: O(1)\n\nclass Solution {\npublic:\n    \n    bool isPossible(vector<int>& candies, int mid, long long k)\n    {\n        //it contains pile
**Code:**\n**TC: O(N), SC: O(1)**\n```\nclass Solution {\npublic:\n    \n    bool isPossible(vector<int>& candies, int mid, long long k)\n    {\n        //it contains piles of candies that tells whether we can distribute or not assign to children equally \n        long count=0;\n        for(auto it:candies)\n        {\n            count += it/mid;\n        \n            //whenever count is greater than or equal to k it tells we can distribute mid to k children and return true;\n            if(count>=k) return true;\n        }\n        //when we can\'t distribute mid piles to k children, we return false;\n        return false;\n    }\n    \n    int maximumCandies(vector<int>& candies, long long k) \n    {\n        //initialize two pointers, 1 for minimum no. of candy, 1 for maximum no. of candy\n        int min_candy = 1;\n        int max_candy = *max_element(begin(candies), end(candies)); //we can also use for(auto it:candies) max_candy = max(max_candy, it)\n        \n        //use binary search for finding candies that can get by each child\n        while(min_candy<=max_candy)\n        {\n            //find mid value\n            long mid = (min_candy+max_candy)>>1;\n            \n            //if we can distribute mid piles of candies to k children successfully\n            //we check for next greater candies\n            if(isPossible(candies, mid, k))\n            {\n                min_candy = mid+1;\n            }\n            \n            //if we can\'t distribute mid piles of candies to k children\n            //we check for lesser candies\n            else\n            {\n                max_candy = mid-1;\n                \n            }\n        }\n        \n        //return maxi_candies that can assign to each child\n        return max_candy;\n        \n    \n    }\n};\n```
['C', 'Binary Tree', 'C++']