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 : Sum of two large numbers - GeeksforGeeks
Nice !
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[100];
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
string add(string s1,string s2)
{
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;
s=add(s,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.
i edited the above code check it again i am having problem in opening the link.
actually i forgot to add the last carry in the first program.
tq bro its working
Bro can you explain that …
so that i can understood easily
for _ in range(int(input())):
elements=input()
sumOfElements=sum(list(map(int,input().split())))
print(sumOfElements)