This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

A Very Big Sum - Solution

Words
80
Reading
1 min
Listen
Play
8y

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 function aVeryBigSum in the editor below. It has two parameters:

  • Integer, n, denoting the number of elements in the input array
  • Long Integer Array, ar , denoting array elements whose sum needs to be computed

The function must return a long integer denoting sum of all array elements.

Problem of HankerRank: https://www.hackerrank.com/challenges/a-very-big-sum/problem

Solution in Golang

package main

import "fmt"

func main() {
    var a int
    var sum uint64
    fmt.Scanf("%v", &a)

    x := make([]uint64, a)
    y := make([]interface{}, len(x))
    for i := range x {
        y[i] = &x[i]
    }

    n, _ := fmt.Scanln(y...)
    x = x[:n]

    sum = 0
    for _, v := range x {
        sum += v
    }

    fmt.Println(sum)
}
A Very Big Sum - Solution | Ecency