Last active
May 11, 2019 14:40
-
-
Save cdmunoz/80a38d68687a632be8465a4fee375e92 to your computer and use it in GitHub Desktop.
Solution in Kotlin for Hackerrank's Missing Numbers problem:https://www.hackerrank.com/challenges/missing-numbers/problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hakerrank | |
import java.io.* | |
import java.math.* | |
import java.security.* | |
import java.text.* | |
import java.util.* | |
import java.util.concurrent.* | |
import java.util.function.* | |
import java.util.regex.* | |
import java.util.stream.* | |
import kotlin.collections.* | |
import kotlin.comparisons.* | |
import kotlin.io.* | |
import kotlin.jvm.* | |
import kotlin.jvm.functions.* | |
import kotlin.jvm.internal.* | |
import kotlin.ranges.* | |
import kotlin.sequences.* | |
import kotlin.text.* | |
/** | |
* https://www.hackerrank.com/challenges/missing-numbers/problem | |
* | |
* Numeros the Artist had two lists that were permutations of one another. He was very proud. Unfortunately, while transporting | |
* them from one exhibition to another, some numbers were lost out of the first list. Can you find the missing numbers? | |
* As an example, the array with some numbers missing, arr=[7,2,5,3,5,3]. | |
* The original array of numbers brr=[7,2,5,4,6,3,5,3]. The numbers missing are [4,6]. | |
* | |
* 1. If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both lists is the same. | |
* If that is not the case, then it is also a missing number. | |
* 2. You have to print all the missing numbers in ascending order. | |
* 3. Print each missing number once, even if it is missing multiple times. | |
* 4. The difference between maximum and minimum number in the second list is less than or equal to 100. | |
*/ | |
// Complete the missingNumbers function below. | |
fun missingNumbers(toFind: Array<Int>, all: Array<Int>): Array<Int> { | |
val map = mapFrom(all.toList()) | |
if(map.isNotEmpty()){ | |
val missing = findMissing(map, toFind.toMutableList()) | |
Collections.sort(missing) | |
return missing.toTypedArray() | |
}else{ | |
return arrayOf() | |
} | |
} | |
private fun mapFrom(all: List<Int>): MutableMap<Int, Int> { | |
var max = Integer.MIN_VALUE | |
var min = Integer.MAX_VALUE | |
val map = mutableMapOf<Int, Int>() | |
all.forEach { nbr -> | |
if (!map.containsKey(nbr)) { | |
map[nbr] = 1 | |
} else { | |
map[nbr] = map[nbr]!!.plus(1) | |
} | |
if(nbr>max){ max = nbr} | |
if(nbr<min){ min = nbr} | |
} | |
if(max - min > 100) return mutableMapOf() | |
return map | |
} | |
private fun findMissing(all: MutableMap<Int,Int>, toFind: List<Int>): List<Int>{ | |
toFind.forEach { number -> | |
if(all.containsKey(number)){ | |
val quantity = all[number] | |
if(quantity==1){ | |
all.remove(number) | |
}else{ | |
all[number] = all[number]!! - 1 | |
} | |
} | |
} | |
return all.map { (key, value) -> key } | |
} | |
fun main(args: Array<String>) { | |
val scan = Scanner(System.`in`) | |
val n = scan.nextLine().trim().toInt() | |
val arr = scan.nextLine().split(" ").map { it.trim().toInt() }.toTypedArray() | |
val m = scan.nextLine().trim().toInt() | |
val brr = scan.nextLine().split(" ").map { it.trim().toInt() }.toTypedArray() | |
val result = missingNumbers(arr, brr) | |
println(result.joinToString(" ")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment