@@ -0,0 +1,2541 @@
+package com.fishercoder.solutions;%0A%0Aimport java.util.TreeMap;%0A%0A/**%0A * 1296. Divide Array in Sets of K Consecutive Numbers%0A *%0A * Given an array of integers nums and a positive integer k,%0A * find whether it's possible to divide this array into sets of k consecutive numbers%0A * Return True if its possible otherwise return False.%0A *%0A * Example 1:%0A * Input: nums = %5B1,2,3,3,4,4,5,6%5D, k = 4%0A * Output: true%0A * Explanation: Array can be divided into %5B1,2,3,4%5D and %5B3,4,5,6%5D.%0A *%0A * Example 2:%0A * Input: nums = %5B3,2,1,2,3,4,3,4,5,9,10,11%5D, k = 3%0A * Output: true%0A * Explanation: Array can be divided into %5B1,2,3%5D , %5B2,3,4%5D , %5B3,4,5%5D and %5B9,10,11%5D.%0A *%0A * Example 3:%0A * Input: nums = %5B3,3,2,2,1,1%5D, k = 3%0A * Output: true%0A *%0A * Example 4:%0A * Input: nums = %5B1,2,3,4%5D, k = 3%0A * Output: false%0A * Explanation: Each array should be divided in subarrays of size 3.%0A *%0A * Constraints:%0A * 1 %3C= nums.length %3C= 10%5E5%0A * 1 %3C= nums%5Bi%5D %3C= 10%5E9%0A * 1 %3C= k %3C= nums.length%0A * */%0Apublic class _1296 %7B%0A    public static class Solution1 %7B%0A        public boolean isPossibleDivide(int%5B%5D nums, int k) %7B%0A            if (nums.length %25 k != 0) %7B%0A                return false;%0A            %7D%0A            TreeMap%3CInteger, Integer%3E treeMap = new TreeMap();%0A            int min = nums%5B0%5D;%0A            for (int num : nums) %7B%0A                treeMap.put(num, treeMap.getOrDefault(num, 0) + 1);%0A                min = Math.min(min, num);%0A            %7D%0A            while (!treeMap.isEmpty()) %7B%0A                if (!isConsecutiveK(treeMap, min, k)) %7B%0A                    return false;%0A                %7D%0A                min = findNextMin(treeMap);%0A                if (min == Integer.MIN_VALUE) %7B%0A                    break;%0A                %7D%0A            %7D%0A            return true;%0A        %7D%0A%0A        private int findNextMin(TreeMap%3CInteger, Integer%3E treeMap) %7B%0A            return treeMap.isEmpty() ? Integer.MIN_VALUE : treeMap.entrySet().iterator().next().getKey();%0A        %7D%0A%0A        private boolean isConsecutiveK(TreeMap%3CInteger, Integer%3E treeMap, int min, int k) %7B%0A            int count = treeMap.get(min);%0A            treeMap.remove(min);%0A            for (int i = 1; i %3C k; i++) %7B%0A                int key = min + i;%0A                if (!treeMap.containsKey(key) %7C%7C treeMap.get(key) %3C count) %7B%0A                    return false;%0A                %7D else if (treeMap.get(key) %3E count) %7B%0A                    treeMap.put(key, treeMap.get(key) - count);%0A                %7D else %7B%0A                    treeMap.remove(key);%0A                %7D%0A            %7D%0A            return true;%0A        %7D%0A    %7D%0A%7D%0A

add 1296