Facebook Pixel

Amazon Online Assessment 2021 (OA) - Pth Factor

We've got a task that we need to solve together: let's figure out the factors of a certain number. Factors are just those positive integers that can divide into our number perfectly; no remainders or fractions allowed. Once we have the full list, we've got to sort them in ascending order; from the smallest to the largest. Then, our job is to find the number that sits in the pth position of our sorted list. However, not all positions might have a number sitting there, so if there is no pth element, we simply return 0.

Relevant Amazon OA Problems:

Input

  • n: the integer whose factors are to be found
  • p: the index of the factor to be return

Output

the integer value of pth integer factor of n or, if there is no factor at that index, then 0 is returned.

Examples

Example 1:

Input:

n = 20
p = 3

Output: 4

Explanation:

The factors of 20 in ascending order are [1, 2, 4, 5, 10, 20]. Using 1-based indexing, if p=3, then 4 is returned. If p>6, 0 would be returned.

Constraints

  • 1<=n<=10^15
  • 1<=p<=10^9

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro