Input: N = 3, arr[] = {2, 1, 0}
Output: 6
Explanation: Initially for subarray {2} the missing element is 0.
For {2, 1} the missing element is 0, and for {2, 1, 0} the missing element is 3.
In this way the answer is 0 + 0 + 3 = 3.
But if the array is rearranged like {0, 1, 2}, the answer will be 1 + 2 + 3 = 6.
Input: N = 5, arr[] = {0, 0, 0, 0, 0}
Output: 5
Consider N = 6, arr[] = {4, 2, 0, 4, 1, 0}
Initialize, ans = 0, cur = 0, count = 0
Sort the array, so arr[] = {0, 0, 1, 2, 4, 4}
For index 0:
=> arr[i] = cur,
=> thus cur would be cur + 1 = 1
=> ans would be ans + cur = 0 + 1 = 1
For index 1:
=> arr[i] = cur - 1
=> Means it is a duplicate
=> Increase the count variable, count = 0 + 1 = 1
For index 2:
=> arr[i] = cur
=> cur would be cur + 1 = 2
=> ans would be ans + cur = 1 + 2 = 3
For index 3:
=> arr[i] = cur,
=> Thus cur would be cur + 1 = 3
=> ans would be ans + cur = 3 + 3 = 6
For index 4:
=> arr[i] > cur
=> cur would be the same for all the remaining subarrays.
=> Thus, count = count + 6 - 4 = 1 + 2(remaining subarrays) = 3 and break the loop.
In the end add cur * count to ans, thus ans = 6 + 3 * 3 = 15