Gordon-Freeman's blog

By Gordon-Freeman, history, 5 hours ago, In English

If i have a sorted vector like this v={ 5,6,7,8,9,10,11 } how can i get the number of elements that are bigger than x? lets say x is 9 so the number of elements bigger than 9 equals 2 how can i code that?

  • Vote: I like it
  • +1
  • Vote: I do not like it

»
5 hours ago, # |
Rev. 2   Vote: I like it +12 Vote: I do not like it

Upper bound

code
  • »
    »
    19 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    lol , i did the exact same but when i compile it , it didn't give me any output and the program ended so i submitted the code anyway to ask what's wrong with it , and it got AC!! any idea why did that happen with my compiler? thanks.

»
5 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

if the array is sorted you can use binary search to get the result in $$$O(log n)$$$ time, also C++ has a prebuilt function named upper_bound which does the same thing, if the array isn't sorted you can sort it first in $$$O(n log n)$$$ time and then use binary search or, iterate over the array and calculate it with a for/while loop in $$$O(n)$$$

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

You can do binary search or use built-in functions in C++ like upper_bound (strictly bigger) or lower_bound (equal or bigger).