702730. Sum of Array

Basic

Here is the detailed Markdown blog post for the "Sum of Array" problem:

Sum of Array Slug: sum-of-array2326 Difficulty: Basic Id: 702730 Topic Tags: Arrays, Data Structures Company Tags: None

Summary

The problem involves calculating the sum of an array. Given a numerical array, write a program to calculate its total sum. This problem is based on basic data structures and algorithms.

Detailed Explanation

To solve this problem, we can use a simple algorithm that iterates through each element in the array and adds them up. Here's a step-by-step breakdown of the solution:

  1. Start with an initialized variable sum set to 0.
  2. Iterate through each element arr[i] in the array.
  3. For each iteration, add the current element arr[i] to the sum.
  4. After the loop finishes, return the final value of sum.

The time complexity for this algorithm is O(n), where n is the size of the input array, since we need to iterate through each element once. The space complexity is O(1), as we only use a single variable to store the sum.

Here's a pseudocode representation of the solution:

View on GeeksforGeeks
public int sumArray(int[] arr) {
    int sum = 0;
    for (int i : arr) {
        sum += i;
    }
    return sum;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.