This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Day 30 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#1492. The kth Factor of n(Medium/JavaScript)

Intro: I’m a former accountant turned software program engineer graduated from coding bootcamp. Algorithms and Knowledge Construction is an unavoidable a part of interviews for a lot of the tech corporations now. And one among my pals instructed me that it’s essential resolve a medium leetcode downside beneath 60 seconds with the intention to get into the highest tech corporations.So I assumed I might begin studying the right way to do it whereas job looking.

Since I’ve no clue on the right way to resolve any of the issues (even the simple ones), I assumed there is no such thing as a level for me to waste hours and might’t get it found out. Right here is my strategy:

  • Decide a leetcode downside randomly or On-line Evaluation from focused corporations.
  • Examine 1-2 options from Youtube or LeetCode dialogue part. One brute drive resolution, one other another optimum.
  • Write a weblog submit with detailed rationalization and do a verbal stroll by to assist perceive the options higher.
  • Code out the answer in LeetCode with out trying on the options
  • Fight the forgetting curve: Re-do the query for the following three days. And are available again usually to revisit the issue.

1492. The kth Issue of n
Problem: Medium Language: JavaScript

You might be given two constructive integers n and ok. An element of an integer n is outlined as an integer i the place n % i == 0.

Take into account a listing of all components of n sorted in ascending order, return the kth issue on this listing or return -1 if n has lower than ok components.

Instance 1:

Enter: n = 12, ok = 3
Output: 3
Rationalization: Elements listing is [1, 2, 3, 4, 6, 12], the third issue
is 3.
Enter fullscreen mode

Exit fullscreen mode

Instance 2:

Enter: n = 7, ok = 2
Output: 7
Rationalization: Elements listing is [1, 7], the 2nd issue is 7.
Enter fullscreen mode

Exit fullscreen mode

Instance 3:

Enter: n = 4, ok = 4
Output: -1
Rationalization: Elements listing is [1, 2, 4], there may be solely 3 components.
We should always return -1.
Enter fullscreen mode

Exit fullscreen mode


My first thought was to get a listing of all components and retailer them in a brand new array; then discover out if kth issue exists. The answer under did not have to new array, which improved house complexity from O(n) to O(1).

var kthFactor = operate(n, ok) {
    for(let i = 1; i <= n; i++) {

//Iterating (notice 1) each constructive integer between 1 and n.

        if((n % i) === 0) {

//for each issue discovered (an element is discovered when the rest is 0)
//(notice 2 & 3).


//scale back the worth of ok


        if(ok === 0) return i

//if ok is 0, then that is the goal issue we're on the lookout for

    return -1

//in any other case return -1

Enter fullscreen mode

Exit fullscreen mode

Time Complexity: O(n)
House Complexity: O(1)

LeetCode Problem Link
LeetCode Discussion: Jeanstaquet
Note 1: for loop
Note 2: remainder%
Note 3: strict equality (===)
Blog Cover Image Credit

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?