Given an array arr of size n, the task is to find the sum of all the elements in the array.
Input: n=5, arr = [1,2,3,4,5]
Output: 15
Explanation: Sum of all the elements is 1+2+3+4+5 = 15
Input: n=6, arr = [1,2,1,1,5,1]
Output: 11
Explanation: Sum of all the elements is 1+2+1+1+5+1 = 11
Input: n=3, arr = [2,1,1]
To sum an array, begin by initializing the sum to zero, representing the starting point with no accumulated value. Then, sequentially add each element of the array to this sum. This approach ensures that, by the end of the process, the sum reflects the total value of all elements in the array.
sum
to 0. This variable will hold the total sum of the array elements.sum
variable.sum
variable will contain the total sum of all the elements in the array.#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int sum(int arr[], int n) {
// Initialize sum to 0
int sum = 0;
// Iterate through each element in the array
for (int i = 0; i < n; i++) {
// Add each element to the sum
sum += arr[i];
}
return sum;
}
};
// The main function
int main() {
Solution sol;
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of array elements: " << sol.sum(arr, n) << endl;
return 0;
}
class Solution {
public int sum(int[] arr, int n) {
// Initialize sum to 0
int sum = 0;
// Iterate through each element in the array
for (int i = 0; i < n; i++) {
// Add each element to the sum
sum += arr[i];
}
return sum;
}
// The main method
public static void main(String[] args) {
Solution sol = new Solution();
int[] arr = {1, 2, 3, 4, 5};
int n = arr.length;
System.out.println("Sum of array elements: " + sol.sum(arr, n));
}
}
class Solution:
def sum(self, arr, n):
# Initialize sum to 0
total_sum = 0
# Iterate through each element in the array
for num in arr:
# Add each element to the sum
total_sum += num
return total_sum
#The main method
if __name__ == "__main__":
sol = Solution()
arr = [1, 2, 3, 4, 5]
n = len(arr)
print("Sum of array elements:", sol.sum(arr, n))
class Solution {
sum(arr, n) {
// Initialize sum to 0
let total_sum = 0;
// Iterate through each element in the array
for (let i = 0; i < n; i++) {
// Add each element to the sum
total_sum += arr[i];
}
return total_sum;
}
}
// Main method
const sol = new Solution();
const arr = [1, 2, 3, 4, 5];
const n = arr.length;
console.log("Sum of array elements:", sol.sum(arr, n));
Time Complexity: O(N) – Array with N elements is traversed, once
Space Complexity: O(1) – A single variable is used to store the sum, regardless of the array size.