UVA 10474 - “Where is the marble?” I have been solving the problem from UVA 10474. In this problem, there will be marbles with numbers written on them. For example suppose five marbles with numbers 50 , 43, 43, 43, 2, 90, 44. There will be several query with number and position has to be told in a sorted sequence. For example after sorting these looks like : 2,43,43,43,44,50,90. Now, query for 43 will give output : 43 fond at 2 and query for -3 will output "-3 not found".

Can you please review it and provide me with feedback?

#include &lt;iostream&gt;
#include&lt;stdio.h&gt;
#include&lt;vector&gt;
#include&lt;algorithm&gt;

void printMarbelPositionIfFound()
{
    int numMarbles, numQueries, aMarble, case1;
    std::vector&lt;int&gt;marbles;

    case1 = 0;

    while(true)
    {
        scanf("%d %d", &amp;numMarbles, &amp;numQueries);
        if(0 == numMarbles &amp;&amp; 0 == numQueries)return;

        case1++;

        for(int i=0; i&lt;numMarbles; i++)
        {
            scanf("%d", &amp;aMarble);
            marbles.push_back(aMarble);
        }

        std::sort(marbles.begin(), marbles.end());
        std::cout&lt;&lt;"CASE#"&lt;&lt;" "&lt;&lt;case1&lt;&lt;":"&lt;&lt;"\n";
        for(int i=0; i&lt;numQueries; i++)
        {

            scanf("%d", &amp;aMarble);

            std::vector&lt;int&gt;::const_iterator itr = std::lower_bound(marbles.begin(), marbles.end(),aMarble);
            if(*itr == aMarble)
            {
                std::cout&lt;&lt;aMarble&lt;&lt;" found at "&lt;&lt;itr - marbles.begin() + 1&lt;&lt;'\n';
            }
            else
            {
                std::cout&lt;&lt;aMarble&lt;&lt;" not found"&lt;&lt;'\n';
            }
        }

        marbles.clear();
    }
}

int main()
{
    printMarbelPositionIfFound();
    return 0;
}
You are mixing C with C++. I know these contests like speed, but it is usually unnecessary.
1. Prefer streams over scanf.
This means you can change this code:
    scanf(&quot;%d %d&quot;, &amp;numMarbles, &amp;numQueries);

Into this:
    std::cin &gt;&gt; numMarbles &gt;&gt; numQueries ;

2. Use the C++11 auto keyword when possible.
You have a C++11 tag, so I assume you have C++11 available.
You can change this:
std::vector&lt;int&gt;::const_iterator itr = std::lower_bound(marbles.begin(), marbles.end(),aMarble);

To this:
auto itr = std::lower_bound(marbles.begin(), marbles.end(),aMarble);

3. Make sure the iterator does not point past the end when you dereference it.
In this line: if(*itr == aMarble), you dereference the iterator. If aMarble is not in marbles, then std::lower_bound() will return marbles.end(). It is undefined behavior to dereference marbles.end(). Always check for this condition.
So change this line:
if(*itr == aMarble)

To this:
if (iter != marbles.end () &amp;&amp; *itr == aMarble)

4. Prefer pre-increment over post-increment.
You have:
case1++;

In the case an int, it doesn't really matter. Your compiler will optimize this correctly. But if you had a complicated object, then you would be creating an unnecessary copy here. So:
++case1;

Would be a better habit to get into.
5. Encapsulate input and output with an Object.
You could create an object that represents a single unit of input. Something like this:
struct Input
{
    int nMarbles ;
    int nQueries ;
    static int caseNumber ;

    std::vector &lt;int&gt; marbles ;
    std::vector &lt;int&gt; queries ;

    Input () ;
    void Clear () ;

    friend auto operator&gt;&gt; (std::istream &amp;is, Input &amp;input) -&gt; std::istream&amp; ;
    friend auto operator&lt;&lt; (std::ostream &amp;os, const Input &amp;input) -&gt; std::ostream&amp; ;
};

Then you could implement the member and friend functions like this:
int Input::caseNumber = 0 ;

Input::Input () : nMarbles (), nQueries ()
{
} 

void Input::Clear ()
{
    nMarbles = 0 ;
    nQueries = 0 ;
    marbles.clear () ;
    queries.clear () ;
}

auto operator&gt;&gt; (std::istream&amp; is, Input &amp;input) -&gt; std::istream&amp;
{
    ++Input::caseNumber ;

    is &gt;&gt; input.nMarbles &gt;&gt; input.nQueries ;
        
    if (input.nMarbles == 0 &amp;&amp; input.nQueries == 0) {
        is.setstate (std::ios::failbit) ;
        return is ;
    }

    auto begin = std::istream_iterator &lt;int&gt; (is) ;
    std::copy_n (begin, input.nMarbles, std::back_inserter (input.marbles)) ;
    
    begin = std::istream_iterator &lt;int&gt; (is) ;
    std::copy_n (begin, input.nQueries, std::back_inserter (input.queries)) ;

    return is ;
}

auto operator&lt;&lt; (std::ostream &amp;os, Input &amp;input) -&gt; std::ostream&amp;
{
    os &lt;&lt; &quot;CASE# &quot; &lt;&lt; Input::caseNumber &lt;&lt; &quot;:&quot; &quot;\n&quot; ;

    auto &amp;marbles = input.marbles ;

    std::sort (std::begin (marbles), std::end (marbles)) ;
    
    for (int query : input.queries) {
        auto iter = std::lower_bound (std::begin (marbles), std::end (marbles), query) ;

        if (iter != std::end (marbles) &amp;&amp; *iter == query) {
            os &lt;&lt; query &lt;&lt; &quot; found at &quot; &lt;&lt; (std::distance (std::begin (marbles), iter) + 1) &lt;&lt; &quot;\n&quot; ;
        }

        else {
            os &lt;&lt; query &lt;&lt; &quot; not found&quot; &quot;\n&quot; ;
        }
    }

    return os ;
}

Then your driver would look like this:
int main ()
{
    Input input ;

    while (std::cin &gt;&gt; input) {
        std::cout &lt;&lt; input ;
        input.Clear () ;
    }

    return 0 ;
}

With the above code, I got an accepted answer with a runtime of 0.212 units on your website. So streams are more than fast enough for this particular challenge.