Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.84 KB | None | 0 0
  1. /**
  2.  * You can edit, run, and share this code.
  3.  * play.kotlinlang.org
  4.  */
  5.  
  6. fun main() {    
  7.     val a = listOf(2,3,9,2,5,1,3,7,10)
  8.     val b = listOf(2,1,3,4,3,10,6,6,1,7,10,10,10)
  9.    
  10.     val result = mutableListOf<Int>()
  11.    
  12.     a.forEach { item ->
  13.         val count = b.count { item == it }
  14.         if (!isPrime(count)) {
  15.             result.add(item)
  16.         }
  17.     }
  18.    
  19.     println(result)
  20. }
  21.  
  22. fun isPrime(num: Int): Boolean {    
  23.     if (num < 2) return false
  24.    
  25.     for (i in 2..num / 2) {
  26.         if (num % i == 0) {
  27.             return false
  28.         }
  29.     }
  30.  
  31.     return true
  32. }
  33.  
  34. Time complexity:
  35. If I'm understanding this correctly, the time complexity will be O (n * (m/2))
  36. Where
  37. n is the length of list a
  38. m currently evaluated item from the list a  
  39.  
  40. Aww it's been a while since I did this O notations thing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement