Problem of the day
You are given an array ‘ARR’ consisting of ‘N’ positive integers. Your task is to find if the number has exactly 3 factors for each number in the array ‘ARR’.
You have to return an array consisting of ‘0,’ and ‘1’ where ‘0’ means that ‘ARR[index]’ does not have 3 factors and ‘1’ means ‘ARR[index]’ has exactly 3 factors.
For Example:If ‘N’ = 4 and ‘ARR’ = [3, 5, 4, 2].
3 has 2 factors, which are 1 and 3.
5 has 2 factors, which are 1 and 5.
4 has 3 factors, which are 1, 2 and 4.
2 has 2 factors, which are 1 and 2.
Hence, the answer is [0, 0, 1, 0].
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array.
The next line contains ‘N’ space-separated integers denoting the elements of array ‘ARR’.
Output format :
For each test case, print an array containing 0 and 1 where 1 is present if the number ‘ARR[index]’ has exactly 3 factors. Otherwise, 0 is present.
Print the output of each test case in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6
1 < ARR[index] <= 10^12
Time Limit: 1 second
2
3
4 5 6
4
8 9 7 13
1 0 0
0 1 0 0
In test case 1: There are 3 elements in the input array.
4 has 3 factors, which are 1, 2, and 4.
5 has 2 factors which are 1 and 5.
6 has 4 factors which are 1, 2, 3, and 6.
So the answer is [1, 0, 0].
In test case 2: There are 4 elements in the input array.
8 has 4 factors, which are 1, 2, 4, and 8
9 has 3 factors, which are 1, 3,and 9
7 has 2 factors, which are 1,and 7
13 has 2 factors, which are 1,and 13
So the answer is [0, 1, 0, 0].
1
5
13 19 21 23 25
0 0 0 0 1
Try to think of a brute force approach.
In this approach, we will implement a function in which we have to pass an integer, and for each integer passed, the function returns the number of factors of that number. So we iterate over the input array ARR, and we pass each ARR[index] to this function. If it returns 3, the result[index] is set to 1. Otherwise, it is set to 0. In the end, we will return the array result.
Algorithm:
O(N * Max(ARR)) where N is the number of elements in the input array.
For each element of the array ARR, we have to traverse from 1 to that element value. Hence, the overall time complexity is O(N * Max(ARR)).
O(N) where N is the number of elements in the input array.
The space complexity O(N) is used to make the result array. Hence, the overall space complexity is O(N).