Easy Tricky Binary Search Approach Explained Along with Code.
This problem is a kind of Upper Bond problem of Binary search.\nHere we just simply have to make an binary search call by setting the min and max variables whic
# This problem is a kind of Upper Bond problem of Binary search.\nHere we just simply have to make an binary search call by setting the min and max variables which is the range in which we have to be get the correct answer. Since we can give as minimum as 0 candies to k children because there are not many candies for everyone so our min = 0. and the maximum candies which we can give is the maximum pile of candie which is available with us because there is no way we can distribute more candies than what we have available.\nTo Calculate mid we will use this formula **mid = min + (max-min)/2** this will give us the exact possible value of mid everytime.\nAnd then we need a condtion to verify if the current **mid** is a right input so for that we have a condition that the number of candie pile can be divided such that all the **k** childeren should get same number of candy so we will make a boolean function to check the exact same thing which we  discussed.\n\n\n```\nclass Solution {\n    public int maximumCandies(int[] candies, long k) {\n        int min = 1;\n        int max = Integer.MIN_VALUE;\n        for(int candie : candies){\n            max = Math.max(candie,max);\n        }\n        \n        int answer = 0;\n        while(min <= max){\n            int mid = min + (max - min)/2;\n            if(checkIfTrue(candies,k,mid)){\n                answer = mid;\n                min = mid+1;\n            }else{\n                max = mid-1;\n            }\n        }\n        return answer;\n    }\n    \n    public boolean checkIfTrue(int[] candies,long k,int divid){\n        long cnt = 0;\n        for(int candie : candies){\n            cnt += (long) Math.floor(candie/divid);\n        }\n        if(cnt >= k) return true;\n        return false;\n    }\n}\n```\n
['Binary Tree', 'Java']