Wednesday, 4 October 2017

Simple Array Sum

problem-

Given an array of  integers, can you find the sum of its elements?
Input Format
The first line contains an integer, , denoting the size of the array. 
The second line contains  space-separated integers representing the array's elements.
Output Format
Print the sum of the array's elements as a single integer.
Sample Input
6
1 2 3 4 10 11
Sample Output
31
Explanation
We print the sum of the array's elements, which is:  .


solution-


#include <bits/stdc++.h>
using namespace std;
/*
*
* Ratnadeep Sen
* National Institute Of Technology Silchar -India (NITS)
*
*/
int main() {
int n,sum=0;
cin >> n;
vector<int> ar(n);
for(int ar_i = 0; ar_i < n; ar_i++){
cin >> ar[ar_i];
sum+=ar[ar_i];
}
cout<<sum;
}

No comments:

Post a Comment

Between two sets

problem- Consider two sets of positive integers,   and  . We say that a positive integer,  , is  between  sets   and   if the followi...