 # Find array sum

Given an array of n integers find the sum of all the elements of the array.
Note: the elements of the array might be large.

### Input format

First line contains integer t ,denoting the number of testcases.
For each testcase:
First line contains an integer n.
Second line contains n space separated integers.

### Output format

For each testcase print the sum of all the array elements on a new line.

### Constraints

1<=t<=50
1<=n<=10^2
1<=arr[i]<=10^100

1 second

### Example

2
3
10 20 30
4
100 600 320 10

#### Output

60
1030

1 Like

Python ?

1 Like

For each test case, initialise sum = 0;
While iterating through the array, do sum = sum + arr[i] that is add the current array element to the sum counter.
For large values, use ‘long long’ in C++ instead of ‘int’.

As array contains values which can’t be handled by even Unsigned Long Long Int
So u can try using Boost Library in C++ . You can read about it from here

See the constraint and time limit

Do you know how to implement a number as a array such that each of its element is a single digit of that number?
Once you can do that, just simulate addition.

Since the size of numbers is very large, you can use matrix method for finding the sum.

C++ Solution :

Accept all numbers as strings (of size at most 100), and add all the strings , code to add 2 numeric strings and you are done : https://www.geeksforgeeks.org/sum-two-large-numbers/

3 Likes

Nice !

1 Like

cpp

any language

and this is my solution,but it is not accepting all test cases

i think problem with constraints

1<=t<=50
1<=n<=10^2
1<=arr[i]<=10^100

//how to store 10 to the power of 100 values in an array help me with this problem
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n, i;
size_t t, j;
unsigned long long int input;
unsigned long long int sum;

cin>>t;

for (j =0; j < t; j++)
{
cin>>n;

``````sum[j] = 0;

for(i=0; i<n; i++)
{
cin>>input;
sum[j] += input;
}
``````

}

for (j =0; j < t; j++)
{
cout<<sum[j]<<endl;
}

return 0;
}

yea bro i posted my solution too…

```````#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
{
ll carry=0;
if(s1.length()>s2.length())
{
swap(s1,s2);
}
for(ll i=s1.size()-1,j=s2.size()-1;i>=0;i--,j--)
{
ll a=s1[i]-'0';
ll b=s2[j]-'0';
ll c=a+b+carry;
carry=c/10;
c=c%10;
s2[j]=c+'0';
}
if(carry)
{
ll st=s2.size()-s1.size();
for(ll i=st-1;i>=0;i--)
{
ll sum=s2[i]-'0'+carry;
carry=sum/10;
sum=sum%10;
s2[i]=sum+'0';
}
}
if(carry)
{
char c=carry+'0';
s2=c+s2;
}
return s2;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
string s;
cin>>s;
for(ll i=1;i<n;i++)
{
string temp;
cin>>temp;
}
cout<<s<<"\n";
}
return 0;
}   `
``````

try this code

tq bro
once check it somewhere you have mistaken

a=int(input())
l=[]
for i in range(a):
c=int(input())
b=list(map(int,input().split(’ ')))
l.append(sum(b))
for i in l:
print(i)

Runtime error

Python:
for _ in range(int(input())):
x = int(input())
arr = list(map(int,input().split()))
print (sum(arr))

can you post the question link…

(if above link does not work)

prepbytes

in personalised-plan switch to strings
there you will see sum of elements problem number 13.