# Python Program for kth largest factor of N (TCS Codevita) | PrepInsta

## kth largest factor of N Problem

TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. Kth largest factor of N is one of the sample problem of this year TCS CodeVita season 9 competition. It is a simple array manipulation problem, here we have provided a python solution for this problem.

## Problem Description

Question -: A positive integer d is said to be a factor of another positive integer N if when N is divided by d, the remainder obtained is zero. For example, for number 12, there are 6 factors 1, 2, 3, 4, 6, 12. Every positive integer k has at least two factors, 1 and the number k itself.Given two positive integers N and k, write a program to print the kth largest factor of N.

Input Format: The input is a comma-separated list of positive integer pairs (N, k).

Output Format: The kth highest factor of N. If N does not have k factors, the output should be 1.

Constraints:

• 1<N<10000000000
• 1<k<600.

You can assume that N will have no prime factors which are larger than 13.

Example 1

• Input: 12,3
• Output: 4

Explanation: N is 12, k is 3. The factors of 12 are (1,2,3,4,6,12). The highest factor is 12 and the third largest factor is 4. The output must be 4.

Example 2

• Input: 30,9
• Output: 1

Explanation: N is 30, k is 9. The factors of 30 are (1,2,3,5,6,10,15,30). There are only 8 factors. As k is more than the number of factors, the output is 1.

Run
```number, k = [int(i) for i in input().split(",")]
factor = []
count = 0
for i in range(1, number+1):
if number % i == 0:
count = count + 1
factor.append(i)

if count < k:
print("1")
else:
print(factor[-k])```

```12,3
4```

## C

To find the solution of Kth largest factor of N problem in C Programming language click on the button below:

C

## C++

To find the solution of Kth largest factor of N problem in C++ Programming language click on the button below:

C++

## Java

To find the solution of Kth largest factor of N problem in Java Programming language click on the button below:

### 13 comments on “Python Program for kth largest factor of N (TCS Codevita) | PrepInsta”

• Ashish

number, k = map(int, input().split(” “))
factors = [i for i in range(1, number+1) if number % i == 0]
print(factors)
if k < len(factors):
print(factors[k])
else:
print(1)

• Archibrata

Here is my most optimized solution:

N,k=map(int,input().split())
if 1<N<10000000000 and 1<k<600:
div = [1,]
for i in range(2,N):
if N%i == 0:
div.append(i)
if i==(N-1):
div.append(N)
if k<=len(div):
print(div[k])
else:
print(1)

• Sukesh

#Here I’m tried to reduce the lines and increase the readability of the program:)
value, position = list(map(int, input().split(‘,’)))
List = []
for i in range(1, value + 1):
if value % i == 0: List.append(i)
if position <= len(List):
ans = List[-position]
print(ans)
else:print(1)

• Sukesh

#Here I’m tried to reduce the lines and increase the readability of the program:)
value, position = list(map(int, input().split(‘,’)))
List = []
for i in range(1, value + 1):
if value % i == 0: List.append(i)
if position <= len(List):print(List[-position])
else:print(1)

• SANTOSH

there is a smal problem in the last line of this program . if you run this program to find 2nd largest factor of 12 it will print 3
the correct program is
n,k=map(int,input().split(“,”))
count=0
factors=[]
for i in range(1,n+1):
if n%i==0:
count=count+1
factors.append(i)
#print(count)
#print(factors)
if count<k:
print("1")
else: print(factors[-k])

• HelpPrepInsta

Thanks for the code Santosh, we’ll surely fix this up

• Nishanth

There is a small mistake in above program:
number, k = [int(i) for i in input().split(“,”)]
factor = []
count = 0
for i in range(1, number+1):
if number % i == 0:
count = count + 1
factor.append(i)

if count < k:
print("1")
else:
factor.reverse()
print(factor[k-1])

• cvs

my_list=input(“enter the number and the factor position seperated by camma”).split(‘,’)
number=int(my_list[0])
position_of_factor=int(my_list[1])
list1=[]
for i in range(1,number+1):
if number%i==0:
list1.append(i)
print(list1)
for a in range(len(list1)):
if position_of_factor==a:
print(list1[a])
else:
print(‘1’)

• cvs

There is a small mistake. The correct code is as follows:
my_list=input(“enter the number and the factor position seperated by camma”).split(‘,’)
number=int(my_list[0])
position_of_factor=int(my_list[1])
list1=[]
for i in range(1,number+1):
if number%i==0:
list1.append(i)
print(list1)
if position_of_factor>len(list1):
print(‘1’)
else:
print(list1[position_of_factor])

• HelpPrepInsta

Thanks Neeraj, we’ll surely fix this mistake. Thank you for contributing your code