DEV Community

Cover image for A Very Big Sum - Problem-Solving | HackerRank
Deepak Raj
Deepak Raj

Posted on • Originally published at codeperfectplus.com on

A Very Big Sum - Problem-Solving | HackerRank

A Very Big Sum is an easy-level Python problem that requires basic knowledge of Python. In this post, we will provide a Python solution for A Very Big Sum.

Problem Statement and Explanation

In this challenge, you need to compute and print the sum of the elements in an array, taking into account that some of those integers may be exceedingly large.

Input Format

  • The array β€˜arr’ includes integers β€˜a[i]’ ranging from 1 to 10^10.

Output Format

  • The sum of the elements in the array, as a single integer.

Solve Me First Solution in Python

Solve Me First Solution in C++

Explanation of Solution

  • aVeryBigSum is a function in C++ that takes a vector of integers as input and returns the sum of all the elements in the vector. The function uses the accumulate() function to calculate the sum.

  • The accumulate() function is a standard library function in C++. It takes three arguments: the beginning iterator of the range, the end iterator of the range, and an initial value. In this case, the initial value is 0.

  • The accumulate() function works by iterating over the range and adding each element to the initial value. The final value of the accumulator is the sum of all the elements in the range.

Top comments (0)