A Very Big Sum - Hacker Rank

Calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.
Function Description
Complete the aVeryBigSum function in the editor below. It must return the sum of all array elements.
aVeryBigSum has the following parameter(s):
  • ar: an array of integers .
Input Format
The first line of the input consists of an integer .
The next line contains  space-separated integers contained in the array.
Output Format
Print the integer sum of the elements in the array.
Constraints

Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
5000000015
Note:
The range of the 32-bit integer is .
When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.

SOURCE CODE



#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char** split_string(char*);

// Complete the aVeryBigSum function below.
long aVeryBigSum(int ar_count, long* ar) {
    long long sum=0;
    for(int i=0;i<ar_count;i++)
    {     
        sum=sum+ar[i];
    }
    return sum;
}

int main()
{
    FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    char* ar_count_endptr;
    char* ar_count_str = readline();
    int ar_count = strtol(ar_count_str, &ar_count_endptr, 10);

    if (ar_count_endptr == ar_count_str || *ar_count_endptr != '\0') { exit(EXIT_FAILURE); }

    char** ar_temp = split_string(readline());

    long* ar = malloc(ar_count * sizeof(long));

    for (int i = 0; i < ar_count; i++) {
        char* ar_item_endptr;
        char* ar_item_str = *(ar_temp + i);
        long ar_item = strtol(ar_item_str, &ar_item_endptr, 10);

        if (ar_item_endptr == ar_item_str || *ar_item_endptr != '\0') { exit(EXIT_FAILURE); }

        *(ar + i) = ar_item;
    }

    long result = aVeryBigSum(ar_count, ar);

    fprintf(fptr, "%ld\n", result);

    fclose(fptr);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) { break; }

        data_length += strlen(cursor);

        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);

        if (!data) { break; }

        alloc_length = new_length;
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }

    data = realloc(data, data_length);

    return data;
}

char** split_string(char* str) {
    char** splits = NULL;
    char* token = strtok(str, " ");

    int spaces = 0;

    while (token) {
        splits = realloc(splits, sizeof(char*) * ++spaces);
        if (!splits) {
            return splits;
        }

        splits[spaces - 1] = token;

        token = strtok(NULL, " ");
    }

    return splits;
}




Click here to Download

OUTPUT





If you have any question then leave a comment below I will do my best to answer that question.



Comments

  1. How to solve this question in C.

    ReplyDelete
  2. Solution.java:14: error: bad operand types for binary operator '<'
    for(int i=0;i<ar;i++)
    ^
    first type: int
    second type: long[]
    1 error



    Yor code shows this error!!!!

    ReplyDelete
    Replies
    1. i have rechecked it there is no such kind of error. You might be doing something wrong. you should have to copy my logic not my code and still you wanna copy it then copy it after downloading it from the given link at the last of the post

      Delete

Post a Comment

Popular posts from this blog

Pattern of your own name.

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

ISBN Number.