commit b653b141188de62478165ac4a4c8ddf11f6f6794 parent a5e78114aa048720e22dc83e869df132aa06af65 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Fri, 19 May 2023 19:39:51 -0500 Completed kth factor of n problem using dart Diffstat:
| A | the-kth-factor-of-n/the-kth-factor-of-n.dart | | | 21 | +++++++++++++++++++++ |
1 file changed, 21 insertions(+), 0 deletions(-)
diff --git a/the-kth-factor-of-n/the-kth-factor-of-n.dart b/the-kth-factor-of-n/the-kth-factor-of-n.dart @@ -0,0 +1,21 @@ +//Given an input n return k which is the kth factor of n. +//The time complexity of this code is O(n), but with constants +//it is O((n/2) + 1) which is pretty good. +//Time: 242ms Beats: 100% +//Memory: 138.6MB Beats: 100% + +class Solution { + int kthFactor(int n, int k) { + List<int> factors = []; + for(int i = 1 ; i <= n / 2 + 1 ; ++i){ + if(n % i == 0){ + factors.add(i); + } + } + factors.add(n); + if(factors.length > k - 1){ + return factors[k - 1]; + } + return -1; + } +}