Блог пользователя ChallaAjay

Автор ChallaAjay, история, 116 минут назад, По-английски

Round 6.

Today's code is supposed to be pretty straightforward. We have an array of positive integers, and we need to find the sum of all its elements.

Nothing fancy. No segment trees, no DP, no 200-line template.

Just a loop.

The code

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n);
    for (int &x : a) cin >> x;

    int sum = 0;
    for (int x : a)
        sum += x;

    cout << sum << '\n';
}

Looks completely fine, doesn't it?

It compiles. It runs. It gives the correct answer on small examples.

But the constraints are:

  • 1 <= n <= 200000
  • 1 <= a[i] <= 1000000000

Your mission

Find a valid input for which this code fails.

  1. Give a counterexample that makes the output incorrect.
  2. Explain exactly what goes wrong.
  3. What's the smallest change needed to fix it?
  4. Bonus: can you explain why the same bug might go unnoticed during local testing?

Try to keep the counterexample small. No need to paste 200,000 numbers when a much smaller test can expose the problem.

Let's see who catches it first.

  • Проголосовать: нравится
  • -4
  • Проголосовать: не нравится

»
111 минут назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

at least try changing the name of the blog dude

  • »
    »
    106 минут назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Sorry, What is wrong in that? please specify

    • »
      »
      »
      95 минут назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      the answer is in the name

      • »
        »
        »
        »
        94 минуты назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        So what do u guys recommend for the name?

        • »
          »
          »
          »
          »
          49 минут назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          just remove part with the answer

»
56 минут назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

I'm gonna be honest. No one's interested in these blogs. Give us a harder challenge (or maybe some juicy rewards) and we'll try

»
3 минуты назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What is the point of all this?