Given a positive integer n, return all the divisors of n in theĀ ascendingĀ order.
Examples:
Input: n = 10
Output: [1, 2, 5, 10]
Explanation: 1, 2, 5 and 10 are the divisors of 10.Input: n = 100
Output: [1, 2, 4, 5, 10, 20, 25, 50, 100]
Explanation: 1, 2, 4, 5, 10, 20, 25, 50 and 100 are divisors of 100.
Table of Content
[Naive Approach] Iterating till n - O(n) Time and O(1) Space
The idea is to iterate over all the numbers from 1 to n and for each number check if the number divides n. If the number divides n, print it.
#include <iostream>
#include <vector>
using namespace std;
vector<int> getDivisors(int n) {
vector<int>divisors;
for (int i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i == 0) {
divisors.push_back(i);
}
}
return divisors;
}
int main() {
vector<int>divisors = getDivisors(10);
for(auto &div: divisors) {
cout << div << " " ;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int* getDivisors(int n, int* size) {
int* divisors = (int*)malloc(n * sizeof(int));
*size = 0;
for (int i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i == 0) {
divisors[(*size)++] = i;
}
}
return divisors;
}
int main() {
int size;
int* divisors = getDivisors(10, &size);
for (int i = 0; i < size; i++) {
printf("%d ", divisors[i]);
}
free(divisors);
return 0;
}
import java.util.*;
class GFG {
public static ArrayList<Integer> getDivisors(int n) {
ArrayList<Integer> divisors = new ArrayList<>();
// Iterate from 1 to n and check divisibility
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
// If 'i' divides 'n' evenly, it's a divisor
divisors.add(i);
}
}
// Return the list of divisors
return divisors;
}
public static void main(String[] args) {
int number = 10;
ArrayList<Integer> divisors = getDivisors(number);
for (int div : divisors) {
System.out.print(div + " ");
}
}
}
def getDivisors(n):
# Create a list to store divisors
divisors = []
# Iterate from 1 to n and check divisibility
for i in range(1, n + 1):
if n % i == 0:
# If 'i' divides 'n' evenly, it's a divisor
divisors.append(i)
return divisors
if __name__ == "__main__":
number = 10
divisors = getDivisors(number)
for div in divisors:
print(div, end=" ")
using System;
using System.Collections.Generic;
class GFG {
static List<int> getDivisors(int n) {
List<int> divisors = new List<int>();
for (int i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i == 0) {
divisors.Add(i);
}
}
return divisors;
}
static void Main() {
List<int> divisors = getDivisors(10);
foreach (int div in divisors) {
Console.Write(div + " ");
}
}
}
function getDivisors(n) {
let divisors = [];
for (let i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i === 0) {
divisors.push(i);
}
}
return divisors;
}
// Driver Code
let divisors = getDivisors(10);
for (let div of divisors) {
process.stdout.write(div + " ");
}
Output
1 2 5 10
[Expected Approach] Finding all factors in pairs - O(sqrt(n)) Time and O(1) Space
If we look carefully, all the divisors of a number appear in pairs.
For example, if n = 100, then the divisor pairs are:
(1, 100), (2, 50), (4, 25), (5, 20), (10, 10).We need to be careful in cases like (10, 10), when both divisors in a pair are equal. In such cases, we should include that divisor only once.
So Instead of iterating from 1 to n, we only need to iterate from 1 to ān.
Why? Because for any factor a of n, the corresponding factor b = n / a forms a pair (a, b).
At least one of the two values in any such pair must lie within the range [1, ān].
So, we can:
- Iterate from 1 to ān to find all divisors less than or equal to ān.
- For every divisor d of n, add d to the res array. If d is not the square root of n, store its paired divisor n / d in the large array. Since the paired divisors are encountered in descending order, append the elements of large to res in reverse order at the end to obtain all divisors in ascending order.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<int> getDivisors(int n) {
vector<int> res, large;
int num = 1;
while (num * num <= n) {
if (n % num == 0) {
if (num == n / num) {
res.push_back(num);
} else {
res.push_back(num);
large.push_back(n / num);
}
}
num++;
}
// add large divisors in reverse
for (int i = large.size() - 1; i >= 0; i--) {
res.push_back(large[i]);
}
return res;
}
int main() {
vector<int>divisors = getDivisors(10);
for(auto &divs: divisors) {
cout << divs << " " ;
}
return 0;
}
import java.util.ArrayList;
public class GFG {
static ArrayList<Integer> getDivisors(int n) {
ArrayList<Integer> res = new ArrayList<>();
ArrayList<Integer> large = new ArrayList<>();
int num = 1;
while (num * num <= n) {
if (n % num == 0) {
if (num == n / num) {
res.add(num);
} else {
res.add(num);
large.add(n / num);
}
}
num++;
}
// Add large divisors in reverse order
for (int i = large.size() - 1; i >= 0; i--) {
res.add(large.get(i));
}
return res;
}
public static void main(String[] args) {
ArrayList<Integer> divisors = getDivisors(10);
for (int div : divisors) {
System.out.print(div + " ");
}
}
}
import math
def getDivisors(n):
res = []
large = []
num = 1
while num * num <= n:
if n % num == 0:
if num == n // num:
res.append(num)
else:
res.append(num)
large.append(n // num)
num += 1
# add large divisors in reverse
for i in range(len(large) - 1, -1, -1):
res.append(large[i])
return res
if __name__ == "__main__":
number = 10
divisors = getDivisors(number)
for div in divisors:
print(div, end=" ")
using System;
using System.Collections.Generic;
class GFG {
static List<int> getDivisors(int n) {
List<int> res = new List<int>();
List<int> large = new List<int>();
int num = 1;
while (num * num <= n) {
if (n % num == 0) {
if (num == n / num) {
res.Add(num);
} else {
res.Add(num);
large.Add(n / num);
}
}
num++;
}
// add large divisors in reverse
for (int i = large.Count - 1; i >= 0; i--) {
res.Add(large[i]);
}
return res;
}
static void Main() {
List<int> divisors = getDivisors(10);
foreach (int divs in divisors) {
Console.Write(divs + " ");
}
}
}
function getDivisors(n) {
let res = [];
let large = [];
let num = 1;
while (num * num <= n) {
if (n % num === 0) {
if (num === n / num) {
res.push(num);
} else {
res.push(num);
large.push(n / num);
}
}
num++;
}
// add large divisors in reverse
for (let i = large.length - 1; i >= 0; i--) {
res.push(large[i]);
}
return res;
}
// Driver Code
let n = 10;
let divisors = getDivisors(n);
for (let divs of divisors) {
process.stdout.write(divs + " ");
}
Output
1 2 5 10