Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Kth largest factor of number N

  • Last Updated : 03 Sep, 2021

Given two positive integers N and K, the task is to print the Kth largest factor of N.  

Input: N = 12, K = 3
Output: 4
Explanation: The factors of 12 are {1, 2, 3, 4, 6, 12}. The largest factor is 12 and the 3rd largest factor is 4.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: N = 30, K = 2
Output: 15
Explanation: The factors of 30 are {1, 2, 3, 5, 6, 10, 15, 30} and the 2nd largest factor is 15.

 

Approach: The idea is to check for each number in the range [N, 1], and print the Kth number that divides N completely.



Iterate through the loop from N to 0. Now, for each number in this loop:

  • Check if it divides N or not.
  • If N is divisible by the current number, decrement the value of K by 1.
  • When K becomes 0, this means that the current number is the Kth largest factor of N.
  • Print the answer according to the above observation.

Below is the implementation of the above approach:

C




// C program for the above approach
 
#include <stdio.h>
 
// Function to print Kth largest
// factor of N
int KthLargestFactor(int N, int K)
{
 
    // Check for numbers
    // in the range [N, 1]
    for (int i = N; i > 0; i--) {
 
        // Check if i is a factor of N
        if (N % i == 0)
 
            // If Yes, reduce K by 1
            K--;
 
        // If K is 0, it means
        // i is the required
        // Kth factor of N
        if (K == 0) {
            return i;
        }
    }
 
    // When K is more
    // than the factors of N
    return -1;
}
 
// Driver Code
int main()
{
    int N = 12, K = 3;
    printf("%d", KthLargestFactor(N, K));
    return 0;
}

C++




// C++ program for the above approach
 
#include <iostream>
using namespace std;
 
// Function to print Kth largest
// factor of N
int KthLargestFactor(int N, int K)
{
    // Check for numbers
    // in the range [N, 1]
    for (int i = N; i > 0; i--) {
 
        // Check if i is a factor of N
        if (N % i == 0)
 
            // If Yes, reduce K by 1
            K--;
 
        // If K is 0, it means
        // i is the required
        // Kth factor of N
        if (K == 0) {
            return i;
        }
    }
 
    // When K is more
    // than the factors of N
    return -1;
}
 
// Driver Code
int main()
{
    int N = 12, K = 3;
    cout << KthLargestFactor(N, K);
}

Java




// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to print Kth largest
    // factor of N
    static int KthLargestFactor(int N, int K)
    {
        // Check for numbers
        // in the range [N, 1]
        for (int i = N; i > 0; i--) {
 
            // Check if i is a factor of N
            if (N % i == 0)
 
                // If Yes, reduce K by 1
                K--;
 
            // If K is 0, it means
            // i is the required
            // Kth factor of N
            if (K == 0) {
                return i;
            }
        }
 
        // When K is more
        // than the factors of N
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 12, K = 3;
        System.out.println(KthLargestFactor(N, K));
    }
}

Python




# Python program for the above approach
 
# Function to print Kth largest
# factor of N
def KthLargestFactor(N, K):
    for i in range(N, 0, -1):
        if N % i == 0:
            K -= 1
        if K == 0:
            return i
    return -1
 
 
# Driver Code
N = 12
K = 3
print(KthLargestFactor(N, K))

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to print Kth largest
// factor of N
static int KthLargestFactor(int N, int K)
{
 
    // Check for numbers
    // in the range [N, 1]
    for (int i = N; i > 0; i--) {
 
        // Check if i is a factor of N
        if (N % i == 0)
 
            // If Yes, reduce K by 1
            K--;
 
        // If K is 0, it means
        // i is the required
        // Kth factor of N
        if (K == 0) {
            return i;
        }
    }
 
    // When K is more
    // than the factors of N
    return -1;
}
 
// Driver Code
public static void Main()
{
    int N = 12, K = 3;
    Console.Write(KthLargestFactor(N, K));
}
}
 
// This code is contributed by ipg2016107.

Javascript




<script>
// JavaScript program for the above approach
// Function to print Kth largest
// factor of N
function KthLargestFactor(N, K)
    {
        // Check for numbers
        // in the range [N, 1]
        for (let i = N; i > 0; i--) {
 
            // Check if i is a factor of N
            if (N % i == 0)
 
                // If Yes, reduce K by 1
                K--;
 
            // If K is 0, it means
            // i is the required
            // Kth factor of N
            if (K == 0) {
                return i;
            }
        }
 
        // When K is more
        // than the factors of N
        return -1;
    }
 
// Driver Code
let N = 12, K = 3;
document.write(KthLargestFactor(N, K));
         
// This code is contributed by shivanisinghss2110
</script>

 
 

Output: 
4

 

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :