Ninja was planning to propose to his crush, Nina, with his spectacular martial arts moves. But Nina was more interested in numbers and divisors, so she gave Ninja a challenge to complete. If Ninja solves it, only then she will date him.
Nina gave him an array of positive integers, ‘ARR’ and asked him to find the sum of divisors of the integers in ‘ARR’ with exactly four divisors. In case there is no such integer with exactly four divisors, then the answer is 0. Ninja has been struggling for a very long time, so he needs your help to solve the problem.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains an integer ‘N’ representing the number of integers in the array, ‘ARR’.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the ‘ARR’ array.
Output Format:
For each test case, return the sum of divisors of the integers in ‘ARR’ with exactly four divisors. In case there is no such integer with exactly four divisors, then return 0.
The output of each test case will be printed in a separate line.
1 <= T <= 5
1 <= N <= 2000
1 <= ARR[ i ] <= 10 ^ 5
Where ‘T’ is the number of test cases, ‘N’ is the number of integers in the array, ‘ARR’ and ‘ARR[ i ]’ is the ‘i’th element in the ‘ARR’ array.
Time limit: 1 second
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
2
4
2 5 6 15
3
4 18 21
36
32
Test Case 1 :
Divisors of 2 are 1 and 2.
Divisors of 5 are 1 and 5.
Divisors of 6 are 1, 2, 3 and 6.
Divisors of 15 are 1, 3, 5 and 15.
Since 6 and 15 have exactly four divisors. Sum of their divisors is (1 + 2 + 3 + 6) + (1 + 3 + 5 + 15) = 36.
Test Case 2 :
Divisors of 4 are 1, 2 and 4.
Divisors of 18 are 1, 2, 3, 6, 9 and 18.
Divisors of 21 are 1, 3, 7 and 21.
Since only 21 has exactly four divisors. Sum of its divisors is (1 + 3 + 7 + 21) = 32.
2
5
7 35 64 11 8
6
27 13 42 25 25 25
63
40
Use brute force approach and find divisors of each element in the array, ‘ARR’.
The idea is to use the brute force approach. We will find the divisors for every element in the ‘ARR’. If the count of divisors becomes greater than 4, we will move to the next element. And if after finding all the divisors of an element, the number of divisors is exactly 4 then we will sum the divisors.
Algorithm:
O(N * M), where ‘N’ is the number of integers in the array, ‘ARR’ and ‘M’ is the maximum value of the integer in the array, ‘ARR’.
The outer loop runs for every element of the array hence takes O(N) time. The inner loop runs till the value of every element and hence will take O(M) time in the worst case. Therefore, overall time complexity = O(N) * O(M) = O(N * M).
O(1).
Since no auxiliary space is required. Hence, the overall space complexity is O(1).