@bernakabadayi Sure.

To make it easier to understand, suppose that all the integers in the array will be in the range of [0, 10000] rather than [-10000, 10000]. We create an array, named `hashtable`

, whose capacity should be (10000 - 0) + 1 = 10001. This array is used to keep track of the times that an element appears in the original array.

The key is to **use the element itself as the index**. For example, we traverse `nums`

, if we encounter a `5`

, just do `hashtable[5]++;`

; if we encounter a '9', do `hashtable[9]++;`

, and so on. This is what the first `for`

loop used to do.

In the second loop, we traverse `hashtable`

. If `hashtable[0] == 2`

, which means there are two `0`

in `nums`

, so the sorted array should start with two `0`

; if `hashtable[1] == 3`

, there should be three `1`

after those two `0`

, etc.

Here we set two variables, `flag`

and `count`

. Since we just have to calculate the sum of min(ai, bi), which is the same to the sum of elements in even-index positions (0, 2, 4, ...) in the sorted array. `flag`

labels whether the element is in an even or odd position, and `count`

is a sentinel that stops the loop.

Each time we encounter an element, do `--hashtable[i]`

to record we have already dealt with it. But `i`

should not increase by 1 every time. Say if we have `hashtable[5] == 2`

, after we dealt with the first `5`

, `i`

should keep being `5`

so we will encounter the second `5`

later. That's why we use:

```
if (--hashtable[i] == 0) {
i++;
}
```

In the problem, the range of elements is [-10000, 10000], so we have to create an `hashtable`

of length 10000 - (-10000) + 1 = 20001; in the first loop, use `hashtable[nums[i] + 10000]++;`

instead so that indices would not be negative; in the second loop, use `ret += i - 10000;`

to neutralize that 10000 added before.

I think this is clear enough. The time complexity should be O(n). This method is especially useful if we know the range of elements, and the range is not very large (like [INT_MIN, INT_MAX]) (or the space complexity will be huge).

Happy coding!