Can anyone help why this bruteforce approach giving wrong result
Can anyone help why this code gives 12 instead of 6 for testcase\nnums = [6,3,8,1,3,1,2,2], k = 4\n\n\nclass Solution {\npublic:\n\tvoid find(int curind, vector
Can anyone help why this code gives 12 instead of 6 for testcase\nnums = [6,3,8,1,3,1,2,2], k = 4\n\n```\nclass Solution {\npublic:\n\tvoid find(int curind, vector<int> &arr, vector<bool> &used, unordered_set<int> &curbucket, int n_rem_bucket, int &best, int bucketsize, int cursum)\n\t{\n\t\tint n = arr.size();\n\t\tif (n_rem_bucket == 1)\n\t\t{\n\t\t\tint mini = INT_MAX;\n\t\t\tint maxi = INT_MIN;\n            curbucket.clear();\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t{\n\t\t\t\tif (!used[i])\n\t\t\t\t{\n\t\t\t\t\tcurbucket.insert(arr[i]);\n                    maxi = max(maxi, arr[i]);\n                    mini = min(mini, arr[i]);\n\t\t\t\t}\n\t\t\t}\n            if (curbucket.size() != bucketsize)\n            {\n                curbucket.clear();\n                return;\n            }\n\t\t\tbest = min(best, cursum + maxi - mini);\n\t\t\tcurbucket.clear();\n\t\t\treturn;\n\t\t}\n\n\t\tint p = curbucket.size();\n\t\tif (p == bucketsize)\n\t\t{\n\t\t\tint mini = INT_MAX;\n\t\t\tint maxi = INT_MIN;\n\t\t\tunordered_set<int>::iterator it;\n\t\t\tfor (it = curbucket.begin(); it != curbucket.end(); it++)\n\t\t\t{\n\t\t\t\tmini = min(mini, *it);\n\t\t\t\tmaxi = max(maxi, *it);\n\t\t\t}\n\n\t\t\tcurbucket.clear();\n\t\t\tfind(0, arr, used, curbucket, n_rem_bucket-1, best, bucketsize, cursum+maxi-mini);\n\t\t\treturn;\n\t\t}\n\n\t\tfor (int i = curind; i < n; i++)\n\t\t{\n\t\t\tif (!used[i])\n\t\t\t{\n\t\t\t\tunordered_set<int>::iterator it;\n\t\t\t\tit = curbucket.find(arr[i]);\n\t\t\t\tif (it == curbucket.end())\n\t\t\t\t{\n\t\t\t\t\tused[i] = true;\n\t\t\t\t\tcurbucket.insert(arr[i]);\n\t\t\t\t\tfind(i+1, arr, used, curbucket, n_rem_bucket, best, bucketsize, cursum);\n\t\t\t\t\tcurbucket.erase(arr[i]);\n\t\t\t\t\tused[i] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n    int minimumIncompatibility(vector<int>& arr, int k) {\n    \tint n = arr.size();\n    \tif (n % k) return -1;\n        int best = INT_MAX;\n        int cursum = 0;\n        int bucketsize = n/k;\n        vector<bool> used(n, false);\n        unordered_set<int> curbucket;\n        find(0, arr, used, curbucket, k, best, bucketsize, 0);\n\n        if (best == INT_MAX) return -1;\n        return best;\n    }\n};\n```\nThanks in advance.
[]