Back to track
#1 Easy Foundations

Array Sum

Open the editor

Problem

Given N integers, compute the sum in O(N).

Constraints

1 <= N <= 200000
-10^9 <= ai <= 10^9

Hints

  1. 1

    Single pass

  2. 2

    Use 64-bit sum

Worked solution

Reference solution

One correct implementation. Alternative approaches that satisfy the constraints are equally valid — the reviewer judges behaviour, not style.

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  if (!(cin >> n)) return 0;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    sum += x;
  }
  cout << sum << '\n';
  return 0;
}

© 2026 ChitChat