Frequency of an element

is this compulsory that if want to have frequency by the upper_bound - lower_bound method the array must be sorted?

yes, they have to be sorted, otherwise the answer would not be correct, these functions are useful only for sorted arrays

Yes this is because upper_bound and lower_bound are based on binary search algorithms.