#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main() { int count,i; long long sum=0,aray[25]; cin>>count; for(i=0;i<count;i++) { cin>>aray[i]; sum+=aray[i]; } cout<<sum; return 0; }
Take long long as data type as it will save value 10^10 times that means A[i] value can be 1000000000
Reference: http://bdclassroom.com/video/hackerrank-a-very-big-sum-algorithm-warmup-c-version/
Java solution:
import java.io.*; import java.util.*; public class Solution { public static void main(String args[])throws IOException { Scanner in=new Scanner(System.in); int n; n=in.nextInt(); long a[]=new long[n]; int i; for(i=0;i<n;i++) { a[i]=in.nextInt(); } long sum;sum=0; for(i=0;i<n;i++) { if(n>=1 && n<=10 && a[i]>=1 && a[i]<=(int)Math.pow(10,10)) { sum=sum+(long)a[i]; } } if(sum!=0) System.out.println(sum); else System.out.println(); } }
Reference:http://programmingssolution007.blogspot.com/2015/09/a-very-big-sum.html