Check for all possible Ks
Intuition\nCheck with all possible k.\n\n# Approach\nCheck of each possible pair what K could be. THen with one K try pairing exisitng numbers, if there\'s some
# Intuition\nCheck with all possible k.\n\n# Approach\nCheck of each possible pair what K could be. THen with one K try pairing exisitng numbers, if there\'s some numbers which are pending and can\'t be paired, this K is not possibe.\n\n# Complexity\n- Time complexity:\n$$O(n^3)$$\n\n- Space complexity:\n$$O(n)$$\n\n\n# Code\n```\nclass Solution {\n    public int[] recoverArray(int[] nums) {\n        Arrays.sort(nums);\n        ArrayBuilder builder=new ArrayBuilder();\n        for(int i=0;i<nums.length;i++){\n            for(int j=i+1;j<nums.length;j++){\n                int k=nums[j]-nums[i];\n                if(k%2!=0 || k==0){\n                    continue;\n                }\n                k/=2;\n                int[] withThisK=builder.getArray(nums,k);\n                if(withThisK!=null){\n                    return withThisK;\n                }\n            }\n        }\n        return null;\n    }\n}\nclass ArrayBuilder{\n    Pending pendingNumbers;\n    int orignalArray[];\n    int orignalArrayIndex;\n    int k;\n    int[] getArray(int nums[],int k){\n        pendingNumbers=new Pending();\n        orignalArrayIndex=0;\n        this.k=k;\n        orignalArray=new int[nums.length/2];\n        for(int i=0;i<nums.length;i++){\n            boolean canPair=pairNumber(nums[i]);\n            if(!canPair){\n                pendingNumbers.add(nums[i]);\n            }\n        }\n        if(!pendingNumbers.isEmpty()){\n            return null;\n        }\n        return orignalArray;\n    }\n    boolean pairNumber(int number){\n        return canPair(number-2*k,k) || canPair(number+2*k,-k);\n    }\n    boolean canPair(int number,int diff){\n        if(pendingNumbers.continas(number)){\n            pendingNumbers.remove(number);\n            orignalArray[orignalArrayIndex++]=number+diff;\n            return true;\n        }\n        return false;\n    }\n}\nclass Pending{\n    Map<Integer,Integer> pendingNumbers;\n    public String toString(){\n        return pendingNumbers.toString();\n    }\n    Pending(){\n        pendingNumbers=new HashMap<>();\n    }\n    boolean isEmpty(){\n        return pendingNumbers.isEmpty();\n    }\n    boolean continas(int number){\n        return pendingNumbers.containsKey(number);\n    }\n    void add(int number){\n        int oldFreq=pendingNumbers.getOrDefault(number, 0);\n        pendingNumbers.put(number,oldFreq+1);\n    }\n    void remove(int number){\n        int oldFreq=pendingNumbers.get(number);\n        if(oldFreq==1){\n            pendingNumbers.remove(number);\n        }else{\n            pendingNumbers.put(number,oldFreq-1);\n        }\n    }\n}\n```
['Java']