Hackerrank – Missing Numbers (FP)
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Look at the Missing Numbers solution.
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Look at the Missing Numbers solution.
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I will create all posible pairs of players. That means I will try to combine every possible combination of player pairs. The topics are given in binary way. I will use OR operation on binaries. It is enough that one […]
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I will find a list of all substrings, which contains same following characters. Then I will create all weights – When a substring has lengths of n, I will create n parts. This will give me all weights of a […]
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution We find the highest candles. Then we find count of candles with max height. I created solution in: Scala Java JavaScript All solutions are also available on my GitHub profile. Scala
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
object BirthdayCakeCandles { def birthdayCakeCandles(n: Int, ar: Array[Int]): Int = { val candleHeightGroups: Map[Int, Seq[Int]] = ar.toList.groupBy(i => i) val maxHeight = candleHeightGroups.keys.max candleHeightGroups.getOrElse(maxHeight, Seq.empty).size } def main(args: Array[String]) { val sc = new java.util.Scanner (System.in) val n = sc.nextInt() val ar = new Array[Int](n) for(ar_i <- 0 until n) { ar(ar_i) = sc.nextInt() } val result = birthdayCakeCandles(n, ar) println(result) } } |
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.util.*; public class BirthdayCakeCandles { static int birthdayCakeCandles(int n, int[] ar) { int max = 0; for(int i = 0; i < ar.length; i++) { if(ar[i] > max) { max = ar[i]; } } int count = 0; for(int i = 0; i < ar.length; i++) { if(ar[i] == max) { count++; } } return count; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] ar = new int[n]; for(int ar_i = 0; ar_i < n; ar_i++){ ar[ar_i] = in.nextInt(); } int result = birthdayCakeCandles(n, ar); System.out.println(result); } } |
Javascript
|
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 |
'use strict'; process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function birthdayCakeCandles(n, ar) { let max = 0; for(let i = 0; i < ar.length; i++) { if(ar[i] > max) { max = ar[i]; } } let count = 0; for(let i = 0; i < ar.length; i++) { if(ar[i] === max) { count++; } } return count; } function main() { var n = parseInt(readLine()); const ar = readLine().split(' ').map(i => parseInt(i)); var result = birthdayCakeCandles(n, ar); process.stdout.write("" + result + "\n"); } |
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution The hint is given in problem description. One should compare if it is better to buy presents without changing colors or with changing colors. I created solution in: Scala Java JavaScript All solutions are also available on my GitHub profile. […]
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I have given count of numbers n. I will iterate over all numbers from 1 to n I will find index of each iterated number in all elements and then index of index within all elements I created solution in: […]
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I created solution in: Scala Java JavaScript All solutions are also available on my GitHub profile. Scala
|
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 |
object PriyankaAndToys extends App { val lines = Source.stdin.getLines().toList val n = Integer.parseInt(lines.head) val weights = lines.tail.head.split("\\s+").map(_.toInt).sorted var price = 1 var lastWeight = weights(0) var i = 1 while ( { i < weights.length }) { if (lastWeight + 4 >= weights(i)) { // lastWeight = toys[i]; } else { price += 1 lastWeight = weights(i) } { i += 1; i - 1 } } println(price) } |
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 |
import java.util.Arrays; import java.util.Scanner; public class PriyankaAndToys { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); String[] weights = scanner.nextLine().split("\\s+"); int[] toyWeights = new int[n]; for (int i = 0; i < n; i++) { toyWeights[i] = Integer.parseInt(weights[i]); } Arrays.sort(toyWeights); int price = 1; int lastWeight = toyWeights[0]; for (int i = 1; i < toyWeights.length; i++) { if(lastWeight + 4 >= toyWeights[i]) { // lastWeight = toys[i]; } else { price++; lastWeight = toyWeights[i]; } } System.out.println(price); scanner.close(); } } |
Javascript
|
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 |
'use strict'; const processData = input => { const lines = input.split('\n'); const n = parseInt(lines[0]); const weights = lines[1].split(" ").map(i => parseInt(i)).sort((a, b) => a - b); let price = 1; let lastWeight = weights[0]; for (let i = 1; i < weights.length; i++) { if(lastWeight + 4 >= weights[i]) { // lastWeight = toys[i]; } else { price++; lastWeight = weights[i]; } } console.log(price); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution http://stackoverflow.com/questions/24518682/count-subsequences-divisible-by-k 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 |
import java.util.Scanner; // http://stackoverflow.com/questions/24518682/count-subsequences-divisible-by-k public class ConsecutiveSubsequences { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = scanner.nextInt(); for(int l = 0; l < tests; l++) { int n = scanner.nextInt(); int k = scanner.nextInt(); long[] numbers = new long[n]; for(int i = 0; i < n; i++) { numbers[i] = scanner.nextLong(); } long[] prefixModCount = new long[k]; for(int i = 0; i < k; i++) { prefixModCount[i] = 0; } prefixModCount[0] = 1; int prefixSum = 0; for(int i = 0; i < numbers.length; i++) { prefixSum += numbers[i]; prefixSum %= k; prefixModCount[prefixSum] += 1; } long result = 0; for(int mod = 0; mod < k; mod++) { result += prefixModCount[mod] * (prefixModCount[mod] - 1) / 2; } System.out.println(result); } scanner.close(); } } |
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; } } |