Added 20 solutions

This commit is contained in:
rshaghoulian
2017-02-16 23:51:56 -06:00
parent 5ac5cfba70
commit 34e66564f9
26 changed files with 859 additions and 57 deletions
@@ -0,0 +1,35 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.math.BigDecimal;
import java.util.*;
class Solution{
public static void main(String []args){
//Input
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String []s=new String[n+2];
for(int i=0;i<n;i++){
s[i]=sc.next();
}
sc.close();
Comparator<String> customComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
BigDecimal a = new BigDecimal(s1);
BigDecimal b = new BigDecimal(s2);
return b.compareTo(a); // descending order
}
};
Arrays.sort(s, 0, n, customComparator);
//Output
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
}
@@ -0,0 +1,16 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.math.BigInteger;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
BigInteger a = new BigInteger(scan.next());
BigInteger b = new BigInteger(scan.next());
System.out.println(a.add(b));
System.out.println(a.multiply(b));
}
}
@@ -0,0 +1,27 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.math.BigInteger;
/*
I use isProbablePrime() with certainty = 10 to achieve 99.9% accuracy.
However, even certainty = 1 (which achieves 50% accuracy) is enough to
pass the HackerRank test cases.
isProbablePrime() is always 100% certain if it tells you if a number
is "not prime". If it says "prime", it's only 99.9% certain. It may be
the case that it just hasn't found a prime number number that divides
the BigInteger yet.
*/
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
BigInteger n = scan.nextBigInteger();
scan.close();
System.out.println(n.isProbablePrime(10) ? "prime" : "not prime");
}
}
@@ -0,0 +1,44 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.HashMap;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
for (int testCase = 0; testCase < T; testCase++) {
int n = scan.nextInt();
int m = scan.nextInt();
/* Create array of values */
int [] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scan.nextInt();
}
System.out.println(solvable(array, m, 0) ? "YES" : "NO");
}
}
/* Basically a depth-first search (DFS).
Marks each array value as 1 when visiting (Side-effect: alters array) */
private static boolean solvable(int [] array, int m, int i) {
/* Base Cases */
if (i < 0 || array[i] == 1) {
return false;
} else if (i + 1 >= array.length || i + m >= array.length) {
return true;
}
array[i] = 1; // marks as visited
/* Recursive Cases */
return solvable(array, m, i - 1) ||
solvable(array, m, i + 1) ||
solvable(array, m, i + m);
}
}
@@ -0,0 +1,26 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
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]);
}
}
}
@@ -0,0 +1,39 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
public class Solution {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int arr[][] = new int[6][6];
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 6; col++) {
arr[row][col] = scan.nextInt();
}
}
scan.close();
System.out.println(maxHourglass(arr));
}
public static int maxHourglass(int [][] arr) {
int max = Integer.MIN_VALUE;
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
int sum = findSum(arr, row, col);
max = Math.max(max, sum);
}
}
return max;
}
private static int findSum(int [][] arr, int r, int c) {
int sum = arr[r+0][c+0] + arr[r+0][c+1] + arr[r+0][c+2]
+ arr[r+1][c+1] +
arr[r+2][c+0] + arr[r+2][c+1] + arr[r+2][c+2];
return sum;
}
}
@@ -0,0 +1,42 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.ArrayList;
/* We need a 2-D array to solve this problem. We can't use an int[][] since we
don't know the initial dimensions. So, we use ArrayList<ArrayList<Integer>> */
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
/* Save numbers in 2-D ArrayList */
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for (int row = 0; row < n; row++) {
int d = scan.nextInt();
ArrayList<Integer> numbers = new ArrayList<>();
for (int col = 0; col < d; col++) {
numbers.add(scan.nextInt());
}
list.add(numbers);
}
/* Answer the queries */
int q = scan.nextInt();
for (int i = 0; i < q; i++) {
int x = scan.nextInt();
int y = scan.nextInt();
try {
int num = list.get(x-1).get(y-1);
System.out.println(num);
} catch (IndexOutOfBoundsException e) {
System.out.println("ERROR!");
}
}
scan.close();
}
}
@@ -0,0 +1,37 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read and save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int [] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scan.nextInt();
}
scan.close();
System.out.println(negativeSubarrays(array));
}
/* O(n^2) runtime */
private static int negativeSubarrays(int [] array) {
int count = 0;
int sum = 0;
for (int i = 0; i < array.length; i++) {
for (int j = i; j < array.length; j++) {
sum += array[j];
if (sum < 0) {
count++;
}
}
sum = 0;
}
return count;
}
}
@@ -0,0 +1,31 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
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){
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
while(testCases>0){
String username = in.nextLine();
//String pattern = Complete this line in the editable area below
String pattern = "^[a-zA-Z]\\w{7,29}$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(username);
if (m.find( )) {
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
testCases--;
}
}
}
@@ -1,7 +1,3 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@@ -13,7 +9,7 @@ public class Solution {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int testCases = Integer.parseInt(scan.nextLine());
while (testCases > 0){
while (testCases-- > 0){
String pattern = scan.nextLine();
try {
Pattern.compile(pattern);
@@ -0,0 +1,31 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* Solution assumes we can't have the symbol "<" as text between tags */
public class Solution{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int testCases = Integer.parseInt(scan.nextLine());
while (testCases-- > 0) {
String line = scan.nextLine();
boolean matchFound = false;
Pattern r = Pattern.compile("<(.+)>([^<]+)</\\1>");
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println(m.group(2));
matchFound = true;
}
if ( ! matchFound) {
System.out.println("None");
}
}
}
}