Hiking around HackerRank — 01

07/12/2020

Note:

  1. All problems are published by HackerRank, credit to HackerRank
  2. The solutions are coded by myself, and open source just for study.

Java BigInteger

Obviously, if we follow the traditional mindset, we will get a “Runtime Error.”

What should we do?

import java.io.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;public class Solution {public static void main(String[] args) {/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scanner input = new Scanner(System.in);BigInteger a = new BigInteger(input.next());BigInteger b = new BigInteger(input.next());BigInteger bigAdd, bigMulti;bigAdd = a.add(b);bigMulti = a.multiply(b);System.out.println(bigAdd);System.out.println(bigMulti);}}

Well, now it had been solved. I think it’s necessary to talk about BigInteger later, but let’s move on first!

Java Map

A Java map should store “key/value” pairs, and you can access them with the index of another type. You can refer to w3schools for more information.

//Complete this code or write your own from scratch
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh)
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
Map<String, Integer> map = new HashMap<>(n);
for(int i=0;i<n;i++)
{
String name=in.nextLine().trim();
int phone=in.nextInt();
in.nextLine();
map.put(name, phone);
}
while(in.hasNext())
{
String s=in.nextLine().trim();
if (map.containsKey(s)){
System.out.println(s + "="+ map.get(s));
}
else {
System.out.println("Not found");
}
}
in.close();
}
}

Well, it’s easier than I thought. But do you have a more robust method to solve it? If so, please let me know!

Java 1D Array

Well, this is so easy that I didn’t trust myself. But that’s what we need:

import java.util.*;public class Solution {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i ++){
a[i] = scan.nextInt();
}
scan.close();
// Prints each sequential element in array a
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}

Done.

Java 2D Array

Well, it is easier than it looked to be (at least for me). We just need some loops and conditional checks. To be honest, I don’t think the default set of this problem is helpful.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) {
int[][] arr = new int[6][6];
int max = -10000;
int sum;
Scanner input = new Scanner(System.in);
for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) {
arr[i][j] = input.nextInt();
if ((i > 1) && (j > 1)){
sum = arr[i][j] + arr[i-2][j] + arr[i-1][j-1] + arr[i-2][j-1] + arr[i-2][j-2]+ arr[i][j-1] + arr[i][j-2];
if (sum > max){
max = sum;
}
}
}
}
System.out.println(max);
scanner.close();
}
}
scanner.close();}}

Java Subarray

The trap is: it’s different from the set in mathematics, you cannot arrange the numbers randomly. However, this had made this problem easier, as well (maybe?).

We just need to generate inputs and get negative-sum counts.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution { public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
int count = 0;
for (int k = 0; k < n; k++ ){
arr[k] = input.nextInt();
}
for (int i = 0; i < n; i ++){
int sum = 0;
for (int j = i; j < n; j ++){
sum += arr[j];
if (sum < 0) {
count ++;
}
}
}
System.out.println(count);
input.close();
}
}

It worked!

Conclusion:

It’s inspiring to solve problems in a programming language. Be excited about the upcoming challenges! Cheer!

Undergraduate student / Research assistant/ Always curious / Opinions are mine