Last active
December 15, 2016 13:50
-
-
Save soardex/abbc85dfed9f25770df750bf4a82ba80 to your computer and use it in GitHub Desktop.
HackerRank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tutorials:Cracking the Code Interview | |
=========================== | |
- Arrays: Left Rotation | |
Algorithms:Warmup | |
================= | |
- Simple Array Sum | |
- Compare the Triplets | |
- A Very Big Sum | |
- Diagonal Difference | |
- Plus Minus | |
- Staircase | |
- Time Conversion | |
- Circular Array Rotation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.math._ | |
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); | |
if (n >= 1 && n <= 10) { | |
for(arr_i <- 0 to n-1) { | |
arr(arr_i) = sc.nextInt(); | |
} | |
if (arr.length >= 0 && arr.length <= pow(10, 10).toInt) { | |
println(arr.map(_.toLong).sum) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Solution { | |
def rotateLeft[A](seq: Seq[A], i: Int): Seq[A] = { | |
val size = seq.size | |
val (first, last) = seq.splitAt(i % size) | |
last ++ first | |
} | |
def main(args: Array[String]) { | |
val sc = new java.util.Scanner (System.in); | |
var n = sc.nextInt(); | |
var k = sc.nextInt(); | |
var a = new Array[Int](n); | |
for(a_i <- 0 to n-1) { | |
a(a_i) = sc.nextInt(); | |
} | |
val b = rotateLeft(a, k) | |
for (b_i <- 0 to n-1) { | |
print(b(b_i) + " ") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.math._ | |
object Solution { | |
def main(args: Array[String]) { | |
val sc = new java.util.Scanner (System.in); | |
var n = sc.nextInt(); | |
var k = sc.nextInt(); | |
var q = sc.nextInt(); | |
var a = new Array[Int](n); | |
if (n >= 1 && n <= pow(10,5)) { | |
for(a_i <- 0 to n-1) { | |
a(a_i) = sc.nextInt(); | |
} | |
var a0 = 0; | |
if ((k >= 1 && k <= pow(10,5)) && (q >= 1 && q <= 500)) { | |
if (k > n) k = k%n | |
while(a0 < q){ | |
var m = sc.nextInt(); | |
if (m >= 0 && m <= n-1) println(a((n+m-k)%n)) | |
a0+=1; | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Solution { | |
def main(args: Array[String]) { | |
val sc = new java.util.Scanner (System.in); | |
var a0 = sc.nextInt(); | |
var a1 = sc.nextInt(); | |
var a2 = sc.nextInt(); | |
var b0 = sc.nextInt(); | |
var b1 = sc.nextInt(); | |
var b2 = sc.nextInt(); | |
val aArr = Array(a0, a1, a2) | |
val bArr = Array(b0, b1, b2) | |
var aPoints = 0 | |
var bPoints = 0 | |
for (i <- 0 to 2) { | |
if (aArr(i) > 1 && aArr(i) < 100) { | |
if (bArr(i) > 1 && bArr(i) < 100) { | |
if (aArr(i) > bArr(i)) { | |
aPoints += 1 | |
} | |
else if (aArr(i) < bArr(i)) { | |
bPoints += 1 | |
} | |
} | |
else { | |
aPoints += 1 | |
} | |
} | |
else { | |
if (bArr(i) > 1 && bArr(i) < 100) { | |
bPoints += 1 | |
} | |
} | |
} | |
println(s"$aPoints $bPoints") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.math._ | |
object Solution { | |
def main(args: Array[String]) { | |
val sc = new java.util.Scanner (System.in); | |
var n = sc.nextInt(); | |
var a = Array.ofDim[Int](n,n); | |
for(a_i <- 0 to n-1) { | |
for(a_j <- 0 to n-1){ | |
a(a_i)(a_j) = sc.nextInt(); | |
} | |
} | |
var left = 0 | |
var right = 0 | |
for(a_i <- 0 to n-1) { | |
for(a_j <- 0 to n-1){ | |
if (a_i == a_j) { | |
left += a(a_i)(a_j) | |
} | |
if (a_i+a_j == n-1) { | |
right += a(a_i)(a_j) | |
} | |
} | |
} | |
val diff = abs(left-right) | |
println(s"$diff") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.math._ | |
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); | |
var (a, b, c) = (0.0, 0.0, 0.0) | |
for(arr_i <- 0 to n-1) { | |
arr(arr_i) = sc.nextInt(); | |
if (signum(arr(arr_i)) > 0) { | |
a += 1.0 | |
} | |
else if (signum(arr(arr_i)) < 0) { | |
b += 1.0 | |
} | |
else { | |
c += 1.0 | |
} | |
} | |
val (dA, dB, dC) = (a/n, b/n, c/n) | |
println(f"$dA%.6f") | |
println(f"$dB%.6f") | |
println(f"$dC%.6f") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
println(arr.map(_.toInt).sum) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Solution { | |
def main(args: Array[String]) { | |
val sc = new java.util.Scanner (System.in); | |
var n = sc.nextInt(); | |
for (i <- 1 to n) { | |
for (j <- 1 to n) { | |
if (i+j > n) { | |
print("#") | |
} | |
else { | |
print(" ") | |
} | |
} | |
println() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text._ | |
object Solution { | |
def main(args: Array[String]) { | |
val sc = new java.util.Scanner (System.in); | |
var time = sc.next(); | |
val inf = new SimpleDateFormat("hh:mm:ssaa") | |
val ouf = new SimpleDateFormat("HH:mm:ss") | |
var date = inf.parse(time) | |
val output = ouf.format(date) | |
println(output) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment