0

how to find the next( bigger than the number i have given as input) bigger num?

vector<int>v={23,68,2,5,8}; // suppose : I give 7 as input //output should be: 8 what would be the code for that?

22nd Aug 2018, 8:09 PM
Tincture
5 Réponses
+ 2
You can sort the vector and use std::find_if(): int range = 7; std::sort(v.begin(),v.end()); std::cout<<*(std::find_if(v.begin(),v.end(),[range](int c){return c>range;})); www.cplusplus.com/reference/algorithm/find_if
23rd Aug 2018, 12:38 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
i made this code for this purpose. Hope it helps you https://code.sololearn.com/cxG1ENsGcM5O/?ref=app
22nd Aug 2018, 9:14 PM
Data
Data - avatar
0
Iterate through the vector and look for smallest number that's bigger than input.
22nd Aug 2018, 8:24 PM
BlazingMagpie
BlazingMagpie - avatar
0
loop in every item, calculate value of difference between current item and input and if result is positive store it in 'd' var... Continue in this way replacing 'd' if current result is lower (and positive) of 'd'
22nd Aug 2018, 8:28 PM
KrOW
KrOW - avatar
0
you can sort the array, then for loop and return or cout the first number > input.
22nd Aug 2018, 8:59 PM
Mooaholic
Mooaholic - avatar