Skip to main content

Simple Array Sum | Hackerrank Problem Solving Algorithm by Aryan

Simple Array Sum



Given an array of integers, find the sum of its elements.

Function Description

Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

  • ar: an array of integers

Sample Input:

5

1 2 3 4 5

Sample Output

15

Explanation

1+2+3+4+5=15

CODE

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
 * Complete the 'simpleArraySum' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts INTEGER_ARRAY ar as parameter.
 */

int simpleArraySum(vector<int> ar) {
    int sum=0;
    for (int i=0; i<ar.size(); i++) {
        sum+=ar[i];
    }
    return sum;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string ar_count_temp;
    getline(cin, ar_count_temp);

    int ar_count = stoi(ltrim(rtrim(ar_count_temp)));

    string ar_temp_temp;
    getline(cin, ar_temp_temp);

    vector<string> ar_temp = split(rtrim(ar_temp_temp));

    vector<int> ar(ar_count);

    for (int i = 0; i < ar_count; i++) {
        int ar_item = stoi(ar_temp[i]);

        ar[i] = ar_item;
    }

    int result = simpleArraySum(ar);

    fout << result << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<intint>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<intint>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}

THANKS FOR VISITING

Comments

Popular posts from this blog

HackerRank SQL | Revising the Select Query I | codewitharyan

Problem Statement Query all columns for all American cities in the CITY table with populations larger than 100000 . The CountryCode for America is USA . Video Solution: APPROACH 1. First select all the column 2. Then use two condition a) POPULATION > 100000 b) COUNTRYCODE="USA" MYSQL QUERY select * from city where POPULATION > 100000 and  COUNTRYCODE= "USA" ; Explanation 1. select * from city : It is used to select all the column from the table city. 2. where : It is used to describe conditions Thank You For Visiting...💖💖💖

Geeksforgeeks | Practice | Learn to Comment

  Learn to comment Code: void comment ( int a , int b , int c ) {     cout << a << endl ;     //cout<<b<<endl;     cout << c << endl ; } Input: a = 5 b = 6 c = 15 Output: 5 15

Revising the Select Query II | Hackerrank SQL Solution | MYSQL

  Problem Statement Query the  NAME  field for all American cities in the  CITY  table with populations larger than  120000 . The  CountryCode  for America is  USA . The   CITY   table is described as follows: .                                            Video Solution: APPROACH 1. First select name column 2. Then use two condition a) POPULATION > 120000 b) COUNTRYCODE="USA" MYSQL QUERY select name from city where POPULATION > 120000 and  COUNTRYCODE= "USA" ; Explanation 1. select name from city : It is used to select all the column from the table city. 2. where : It is used to describe conditions Thank You For Visiting...💖💖💖