the-kth-factor-of-n.dart (541B)
1 //Given an input n return k which is the kth factor of n. 2 //The time complexity of this code is O(n), but with constants 3 //it is O((n/2) + 1) which is pretty good. 4 //Time: 242ms Beats: 100% 5 //Memory: 138.6MB Beats: 100% 6 7 class Solution { 8 int kthFactor(int n, int k) { 9 List<int> factors = []; 10 for(int i = 1 ; i <= n / 2 + 1 ; ++i){ 11 if(n % i == 0){ 12 factors.add(i); 13 } 14 } 15 factors.add(n); 16 if(factors.length > k - 1){ 17 return factors[k - 1]; 18 } 19 return -1; 20 } 21 }