mirror of
https://github.com/jlengrand/hackerrank.git
synced 2026-03-10 08:21:24 +00:00
Solves a very big sum
This commit is contained in:
22
src/main/scala/algorithms/aVeryBigSum/Solution.scala
Normal file
22
src/main/scala/algorithms/aVeryBigSum/Solution.scala
Normal file
@@ -0,0 +1,22 @@
|
||||
package algorithms
|
||||
|
||||
/**
|
||||
* Created by jll on 3/26/2017.
|
||||
*/
|
||||
object Solution {
|
||||
|
||||
def main(args: Array[String]) {
|
||||
val sc = new java.util.Scanner (System.in);
|
||||
var n = sc.nextInt();
|
||||
var arr = new Array[Int](n);
|
||||
for(arr_i <- 0 to n-1) {
|
||||
arr(arr_i) = sc.nextInt();
|
||||
}
|
||||
|
||||
print(sum(arr))
|
||||
}
|
||||
|
||||
def sum(arr: Array[Int]) : Long =
|
||||
arr.map(x => x.toLong).sum
|
||||
|
||||
}
|
||||
13
src/main/scala/algorithms/aVeryBigSum/SolutionBest.scala
Normal file
13
src/main/scala/algorithms/aVeryBigSum/SolutionBest.scala
Normal file
@@ -0,0 +1,13 @@
|
||||
package algorithms.aVeryBigSum
|
||||
|
||||
/**
|
||||
* Created by jll on 3/26/2017.
|
||||
* From https://codepair.hackerrank.com/paper/PqyQ4XMD?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6IkFpcmJhbGxtYW4iLCJlbWFpbCI6ImpsZW5ncmFuZEBnbWFpbC5jb20ifQ%3D%3D
|
||||
*/
|
||||
object Solution {
|
||||
|
||||
def main(args: Array[String]) {
|
||||
val line = io.Source.stdin.getLines().drop(1)
|
||||
println(line.next().split(" ").map(_.toLong).sum)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user