Upper Bound and Lower Bound

1 Test case passed

@Kinjal what is the error on submission?

there is no error, just wrong answer. Only 1 test case passed and in other test cases gives wrong answer.
I don’t know, if my approach is right or wrong!

hi @Kinjal this approach is not working when the key is present at the last index.
for eg if the array is 3 3 3 3 3 and you are searching for 3, the output should be 0 4 but it is coming out to be 0 -1
Take care of this corner case as well! (check if itr is at v.end()).

You can also try to implement a binary search method of your own.

Can you tell me, any corner case still failing in my code, because I’m still getting wrong answer.

@Kinjal for the array

5
3 3 3 4 5

key = 1

output = 0 -1 this is wrong

I am trying to do it by STL, still I couldn’t get the answers.

@Kinjal you have to check for all corner cases whichever method you choose to do it with

Can you please check this one? Still I am failing some corner which you haven’t mentioned or may be I don’t knw about them at all.

@Kinjal see for this input

10 
1 2 3 4 5 6 7 8 9 12
1
10

you must ensure that the key is not present in the vector. You are not checking for it. It will work okay if the key that is not present is greater than all the elements or smaller than all the elements but fail if the key could be somwhere in the middle but is not there. (because then upperbound and lowerbound will exist)
you can use the stl function find() for this purpose.

1 Like

You are awesome. I did it. Thanks for the corner cases and giving your useful advice.

1 Like

@Kinjal don’t forget to mark the doubt as resolved if you don’t have any further doubts!

Yes. I marked doubt as resolved at first hand.
can I message you, if I stuck in any problems or any concept regarding Competitive Programming?

hi @Kinjal you can use the ask doubt section, you’d be able to get a quicker response from the platfrom.

Okay. Got it…

1 Like