Hackerrank – Project Euler+ #067 – Maximum path sum II
Hackerrank – Problem description The problem description – Hackerrank. Solution The same solution as Maximum Path Sum I.
Hackerrank – Problem description The problem description – Hackerrank. Solution The same solution as Maximum Path Sum I.
Hackerrank – Problem description The problem description – Hackerrank. Solution Steps are given in the problem description. I created solution in: Java All solutions are also available on my GitHub profile. Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class DoubleBasePalindromes { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); int base = stdin.nextInt(); long sum = 0; for(int i = 1; i < n; i++) { String decimalString = Integer.toString(i, 10); String kBaseString = Integer.toString(i, base); if(isPalindrome(decimalString) && isPalindrome(kBaseString)) { sum += i; } } System.out.println(sum); stdin.close(); } private static boolean isPalindrome(String n) { for(int i = 0; i < n.length() / 2; i++) { char first = n.charAt(i); char last = n.charAt(n.length() - i - 1); if(first != last) { return false; } } return true; } } |
Hackerrank – Problem description The problem description – Hackerrank. Solution Steps are given in the problem description. I created solution in: Java All solutions are also available on my GitHub profile. Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class NameScores { private static Map<Character, Integer> alphabet; static { alphabet = new HashMap<>(26); alphabet.put('A', 1); alphabet.put('B', 2); alphabet.put('C', 3); alphabet.put('D', 4); alphabet.put('E', 5); alphabet.put('F', 6); alphabet.put('G', 7); alphabet.put('H', 8); alphabet.put('I', 9); alphabet.put('J', 10); alphabet.put('K', 11); alphabet.put('L', 12); alphabet.put('M', 13); alphabet.put('N', 14); alphabet.put('O', 15); alphabet.put('P', 16); alphabet.put('Q', 17); alphabet.put('R', 18); alphabet.put('S', 19); alphabet.put('T', 20); alphabet.put('U', 21); alphabet.put('V', 22); alphabet.put('W', 23); alphabet.put('X', 24); alphabet.put('Y', 25); alphabet.put('Z', 26); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int namesCount = Integer.parseInt(scanner.nextLine()); List<String> namesList = new ArrayList<>(namesCount); for (int i = 0; i < namesCount; i++) { String name = scanner.nextLine(); namesList.add(name); } Collections.sort(namesList); Map<String, Integer> names = new HashMap<>(); for (int i = 1; i <= namesList.size(); i++) { names.put(namesList.get(i - 1), i); } int queries = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < queries; i++) { String query = scanner.nextLine(); int score = nameScore(names, query); System.out.println(score); } scanner.close(); } private static int nameScore(Map<String, Integer> names, String name) { int result = 0; for (int i = 0; i < name.length(); i++) { result += alphabet.get(name.charAt(i)); } result *= names.get(name); return result; } } |
Hackerrank – Problem description The problem description Hackerrank. Solution The catch is to have a support for big numbers operations in your progamming language. Do a factorial and sum the numbers. I created solution in: Ruby All solutions are also available on my GitHub profile. Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
def factorial(n) fact = 1 for i in 2..n do fact *= i end return fact end def sum_digits(number) chars = number.to_s.chars sum = 0 chars.each { |char| sum += char.to_i } return sum end gets.chomp.to_i.times do n = gets.chomp.to_i puts sum_digits(factorial(n)) end |
Hackerrank – Problem description The problem description – Hackerrank. Solution The implementation is specific based on a programming language – the features in libraries for date manipulation. I found out from given date, what is the next sunday. Then I added 7 days to the sunday’s date, to have next sunday. I was cautioned for […]
Hackerrank – Problem description The problem description – Hackerrank. Solution I read and store all number into lists by rows. I start from the bottom and take last 2 lines. I sum a number from upper row with numbers from lower row on the left and right side. I do it for every number in […]
Hackerrank – Problem description The problem description – Hackerrank. Solution For detailed informations look at the implementation. I created basic number to word translations. The I find out how many times is a given number present in basic translations. E.g. 12 324 is 12 x 1000, than “twelve thousand”. The rest is without 12 000, […]
Hackerrank – Problem description The problem description – Hackerrank. Solution The catch is to use a feature of chosen programming language, which allows us operation with big numbers (e. g. BigInteger in Java) I created solution in: Java All solutions are also available on my GitHub profile. Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.math.BigInteger; import java.util.Scanner; public class PowerDigitSum { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < tests; i++) { BigInteger base = new BigInteger("2"); int exp = Integer.parseInt(scanner.nextLine().trim()); base = base.pow(exp); long result = sumDigits(base); System.out.println(result); } scanner.close(); } private static long sumDigits(BigInteger num) { long result = 0; char[] digits = num.toString().toCharArray(); for (char digit : digits) { result += Character.digit(digit, 10); } return result; } } |
Hackerrank – Problem description The problem description – Hackerrank. Solution The solution is based on this article. I created solution in: Java All solutions are also available on my GitHub profile. Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.math.BigInteger; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class LatticePaths { // http://www.robertdickau.com/lattices.html public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < tests; i++) { String[] numbers = scanner.nextLine().split("\\s+"); int n = Integer.parseInt(numbers[0]); int m = Integer.parseInt(numbers[1]); Set<Integer> nFactorialMembers = new HashSet<>(); for (int j = 1; j <= n; j++) { nFactorialMembers.add(j); } Set<Integer> mFactorialMembers = new HashSet<>(); for (int j = 1; j <= m; j++) { mFactorialMembers.add(j); } Set<Integer> mnFactorialMembers = new HashSet<>(); for (int j = 1; j <= m + n; j++) { mnFactorialMembers.add(j); } // (m+n) chose m === (m+n)!/m!n! // divided n! mnFactorialMembers.removeAll(nFactorialMembers); BigInteger result = BigInteger.ONE; for (Integer k : mnFactorialMembers) { result = result.multiply(new BigInteger(k.toString())); } for (Integer k : mFactorialMembers) { result = result.divide(new BigInteger(k.toString())); } System.out.println(result.mod(new BigInteger("1000000007"))); } scanner.close(); } } |
Hackerrank – Problem description The problem description – Hackerrank. Solution There are given 2 rules for Collatz sequence: if n is even, then n/2 if n is odd, then 3n + 1 The given problem set of possibilities is to big and it would take too much time. My solution is to precalculate sequence values […]