using binary search on answer , explanation
instead of applying binary search directly on array we will apply binary search on the range of the answer.\n\nlow = 1 because there can be minimum 1 candy in t
instead of applying binary search directly on array we will apply binary search on the range of the answer.\n\nlow = 1 because there can be minimum 1 candy in the array\nhigh = maximum in the array because that is the maaximum amount of candies that can be present in the pile\n\n\nnow we want to validate each mid that we will get \n# Code\n```\nclass Solution {\n    public int maximumCandies (int[] candies, long k) {\n        int max = 0;\n        for (int i : candies) {\n            max = (max > i) ? max : i;\n        }\n        int start = 1, end = max, mid = 0, res = 0;\n        while (start <= end) {\n            mid = start + (end - start) / 2;\n            if (isValid(candies, k, mid)) {\n                res = mid;\n                start = mid + 1;\n            } else {\n                end = mid - 1;\n            }\n        }\n        return res;\n    }\n    private boolean isValid(int[] candies, long k, int mid) {\n        long totalPiles = 0;\n        for (int i : candies) {\n            int toAdd = i / mid;\n            totalPiles += toAdd;\n        }\n        return totalPiles >= k;\n    }\n\n}\n```\nyou can check out my github repository where i am uploading famous interview questions topic wise with solutions.\nlink-- https://github.com/Abhaydutt2003/DataStructureAndAlgoPractice \nkindly upvote if you like my solution. you can ask doubts below.\n
['Binary Search', 'Java']