Java || Like leetcode 954 and 2007 || HashMap || With comments
// b <=============2122. Recover the Original Array ============>\n    // https://leetcode.com/problems/recover-the-original-array/description/\n\n    // Consid
// b <=============2122. Recover the Original Array ============>\n    // https://leetcode.com/problems/recover-the-original-array/description/\n\n    // Consider [a,b,c,d] to be the original array.\n    // [a-k,b-k,c-k,d-k] will be the lower array\n    // [a+k,b+k,c+k,d+k] will be the higher array.\n\n    // Humne agar array ko sort kiya, to hume pata hai ki nums[0] humesha lower\n    // array mai he aayega, kyunki wo smallest value hogi.\n\n    // So consider a-k to be the smallest element in the sorted array.\n    // then (a+k)- (a-k)=2k.\n\n    // We know that (a-k) is nums[0].\n\n    // So the formula becomes (nums[i] - nums[0]) /2 for getting the various values\n    // of k.\n\n    // Now for each values of k, check if all the pairs are formed. If yes, then\n    // return the array.\n\n```\npublic int[] recoverArray(int[] nums) {\n\n        HashMap<Integer, Integer> map = new HashMap<>();\n        int n = nums.length;\n        for (int ele : nums) {\n            map.put(ele, map.getOrDefault(ele, 0) + 1); // getting the frequency.\n        }\n\n        Arrays.sort(nums);\n\n        int[] ans = new int[n / 2];\n        for (int i = 1; i < n; i++) {\n            int k = (nums[i] - nums[0]) / 2; // getting all the values of k.\n\n            if (k <= 0)\n                continue; // k cannot be -ve or 0.\n\n            HashMap<Integer, Integer> cmap = new HashMap<>(map);\n            int idx = 0;\n            for (int ele : nums) {\n                if (cmap.get(ele) == 0) // Agar kisi element ki frequency 0 hai, to wo already paired hai.\n                    continue;\n\n                int higher = ele + 2 * k;\n                int lower = ele - 2 * k;\n\n                // If a elements gets paired, then we are decreasing the frequency of both\n                // elements of the pair.\n                if (cmap.getOrDefault(higher, 0) > 0) {\n                    cmap.put(ele, cmap.get(ele) - 1);\n                    cmap.put(higher, cmap.get(higher) - 1);\n                    ans[idx++] = ele + k;\n                } else if (cmap.getOrDefault(lower, 0) > 0) {\n                    cmap.put(ele, cmap.get(ele) - 1);\n                    cmap.put(lower, cmap.get(lower) - 1);\n                    ans[idx++] = ele - k;\n                } else {\n                    // if the higher or lower cease to exist in the map, then the k value is wrong.\n                    break;\n                }\n            }\n\n            // if all the values are paired, then idx will reach to n/2.\n            if (idx == n / 2)\n                return ans;\n        }\n\n        return new int[] {};\n    }\n```\n\n\n // b <==========954. Array of Doubled Pairs ==========>\n    // https://leetcode.com/problems/array-of-doubled-pairs/description/\n\n    // [0,0,2,4,1,8], [-16,-2,-8,-4] : Test cases to dry run\n\n    // Hume basically yahan pe pucha hai ki can we rearrange array such that array\n    // ` becomes like [a,2a,b,2b,c,2c,d,2d].\n\n    // So hume basically har ek element ke liye uska pair dhundhna hai. Aur agar har\n    // ek element ke liye uska pair mil jata hai to wo rearrange ho sakta hai.\n\n    // Humne hashmap mai har ek element ki frequency nikali\n    // Ab agar humne array ko normally sort kiya to hume array aise milega.\n    // -16,-8,-4,-2,0,0,1,2,4,8,. Jisme hume -ve elements ke liye unka half check\n    // karna padega aur +ve elements ke liye unka double.\n\n    // Ab agar mai chahta hun ki mujhe har element ka double he check karna page, to\n    // isiliye maine Arr ko apne hisab se sorrt karunga. To do this, mujhe Apna ek\n    // Integer class ka array banana padega since int is a primitive type.\n\n    // So now the array will be sorted like treating the -ve number as +ve.\n\n    // 0,0,1,2,-2,4,-4,-8,8,-16.\n\n```\npublic boolean canReorderDoubled(int[] arr) {\n\n        int n = arr.length;\n        Integer[] Arr = new Integer[n];\n        HashMap<Integer, Integer> map = new HashMap<>();\n\n        for (int i = 0; i < n; i++) {\n            map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);\n            Arr[i] = arr[i];\n        }\n\n        Arrays.sort(Arr, (a, b) -> { // Sorting the array as if the -ve number are +ve.\n            return Math.abs(a) - Math.abs(b);\n        });\n\n        for (int ele : Arr) {\n            if (map.get(ele) == 0) // If frequency is zero, the element has already been paired.\n                continue;\n            if (map.getOrDefault(ele * 2, 0) <= 0) // If we cannot find a 2*ele, we cannot pair this element. Hence\n                                                   // returning false.\n                return false;\n\n            // Since a pairing requires both elements, decreasing the frequency of both the\n            // elements of the pair.\n            map.put(ele, map.get(ele) - 1);\n            map.put(2 * ele, map.get(ele * 2) - 1);\n        }\n\n        return true;\n    }\n```\n\n    \n// b <=========== 2007. Find Original Array From Doubled Array ==========>\n    // https://leetcode.com/problems/find-original-array-from-doubled-array/description/\n\n    // # Logic is same as above.\n    // # But here no -ve elements, so just normally sort the array.\n\n```\npublic int[] findOriginalArray(int[] arr) {\n\n        int n = arr.length;\n        if (n % 2 == 1)\n            return new int[] {};\n        HashMap<Integer, Integer> map = new HashMap<>();\n        for (int ele : arr)\n            map.put(ele, map.getOrDefault(ele, 0) + 1);\n\n        Arrays.sort(arr);\n\n        int[] ans = new int[n / 2];\n        int i = 0;\n        for (int ele : arr) {\n            if (map.get(ele) == 0)\n                continue;\n\n            if (map.getOrDefault(ele * 2, 0) <= 0)\n                return new int[] {};\n\n            map.put(ele, map.get(ele) - 1);\n            map.put(2 * ele, map.get(ele * 2) - 1);\n            ans[i++] = ele;\n        }\n        return ans;\n    }\n\n```
['Java']