Hiking around HackerRank — 01

07/12/2020

Java BigInteger

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);}}

Java Map

//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();
}
}

Java 1D Array

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]);
}
}
}
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

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();
}
}

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store