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?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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?
Name |
---|
Upper bound
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.
The below code requires knowledge of binary search.
Hope this helps.
I also use binary search to do this
where did you submit ? Can you share the group link so I check the problem?
This was the problem 270900251 my code got AC but when i compiled it crashed for some reason that why i was confused
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)$$$
You can do binary search or use built-in functions in C++ like upper_bound (strictly bigger) or lower_bound (equal or bigger).