Input: ‘N’ = 10
Output: [1, 2, 5, 10]
1, 2, 5, and 10 are the only divisors of the number 10.
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains a natural number ‘N’.
For each test case, you don’t need to print anything just return a sorted array containing all the divisors of ‘N’.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^9
Time Limit: 1 sec
In this approach, We can iterate over all the numbers from 1 to ‘N’ and check if ‘N’ is divisible by that number or not. If it is divisible we can store it in an array. Since we are iterating from 1 to ‘N’ the divisors will be stored in the array in increasing order.
If we observe the divisors of any number ‘N’, the divisor can be written as pairs like (‘X’, ‘Y’) where ‘X’ <= ‘Y’ and ‘N’ / ‘X’ = ‘Y’.
For example divisors of 100 can be written as (1, 100), (2, 50), (4, 25), (5, 20), (10, 10). So we can just iterate from 1 to the square root of ‘N’ and check if the number is a divisor of ‘N’ or not, if it is a divisor and let the number be ‘X’ then ‘X’ and ‘N’ / ‘X’ will be new divisors.
One thing to note is that if (‘X’ and ‘N’ / ‘X’) are equal then we use only one of them. To print the answer in sorted order, at first from every pair (‘X’, ‘N’ / ‘X’) store only ‘X’ in the array and then traverse the array in reverse order then append ‘N’ / ‘X’ to the back of the array.