Find the Sum of All Elements in an Array
Problem Statement
Given an array of integers, calculate the sum of all its elements.
This is one of the most common introductory array problems and forms the base for advanced concepts such as prefix sums, sliding window, and cumulative frequency problems.
Why This Problem Matters
Finding the sum of elements is often:
- The first step in array-based problem solving
- Used as a building block in:
- Average calculation
- Range sum queries
- Subarray sum problems
- Prefix sum construction
Understanding this problem ensures correctness when moving to more optimized approaches.
Input and Output Format
Input
- An array arr of size n
Output
- A single integer representing the sum of all elements
Example
Input: arr = [5, 10, 15, 20]
Output: 50
Approach 1: Iterative Traversal (Standard Method)
Idea
Traverse the array from the first index to the last and keep adding each element to a running total.
Step-by-Step Algorithm
- Initialize sum = 0
- Traverse the array from index 0 to n-1
- Add the current element to sum
- After traversal, return sum
Pseudocode
sum = 0
for i from 0 to n-1:
sum = sum + arr[i]
return sum
Dry Run Example
arr = [5, 10, 15, 20]
Step 1: sum = 0
Step 2: sum = 0 + 5 = 5
Step 3: sum = 5 + 10 = 15
Step 4: sum = 15 + 15 = 30
Step 5: sum = 30 + 20 = 50
Final Answer: 50
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |
Language-wise Implementation
C Implementation
#include
int main() {
int arr[] = {5, 10, 15, 20};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum = %d", sum);
return 0;
}
Output
Sum = 50
C++ Implementation
#include
using namespace std;
int main() {
int arr[] = {5, 10, 15, 20};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}
cout << "Sum = " << sum;
return 0;
}
Java Implementation
public class Main {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20};
int sum = 0;
for(int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("Sum = " + sum);
}
}
Python Implementation
arr = [5, 10, 15, 20]
total = 0
for num in arr:
total += num
print("Sum =", total)
C# Implementation
using System;
class Program {
static void Main() {
int[] arr = {5, 10, 15, 20};
int sum = 0;
for(int i = 0; i < arr.Length; i++) {
sum += arr[i];
}
Console.WriteLine("Sum = " + sum);
}
}
JavaScript Implementation
let arr = [5, 10, 15, 20];
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
console.log("Sum =", sum);
Approach 2: Using Built-in Functions (Conceptual)
This approach is language-dependent and not always preferred in interviews.
Examples:
- Python: sum(arr)
- JavaScript: arr.reduce((a, b) => a + b, 0)
Interviewers usually expect manual traversal first.
Edge Cases to Consider
- Empty array → Sum = 0
- Single element array
- Array with negative numbers
- Very large numbers (overflow consideration in C/C++)
- Mixed positive and negative values
Common Mistakes
- Not initializing sum
- Using incorrect loop bounds
- Integer overflow in large arrays
- Assuming array is non-empty
Interview Notes
- This problem is often used as a warm-up
- Interviewers check:
- Loop correctness
- Index handling
- Basic understanding of arrays
- Follow-up questions may include:
- Sum of subarray
- Prefix sum optimization
- Average calculation
Summary
Finding the sum of all elements in an array is a fundamental operation that strengthens understanding of array traversal and accumulation techniques. While simple, it plays a crucial role in many advanced algorithms and optimizations. A solid grasp of this problem ensures smoother progression toward more complex array challenges.
