A Very Big Sum - Solution

in #golang7 years ago (edited)

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)
}
Sort:  

Congratulations @vale8a29! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Congratulations @vale8a29! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.039
BTC 97549.65
ETH 3484.99
USDT 1.00
SBD 3.21