Given a quantity N, the duty is to search out the distinct Prime Elements of N.
Enter: N = 12
Output: 2 3
Clarification: The elements of 12 are 1, 2, 3, 4, 6, 12.
Amongst these the distinct prime elements are 2 and three.
Enter: N = 39
Output: 3 13
Strategy: The method is to make use of a map to verify whether or not a given issue of the quantity has occurred earlier or not. Now comply with the beneath steps to resolve this drawback:
- Create a map visited to maintain monitor of all earlier prime elements.
- Create a variable C, and initialise it with 2.
- Whereas N is divisible by C, print C if C will not be current within the map. Now divide N by C. Additionally increment C by 1.
Under is the implementation of the above method:
Time Complexity: O(N)
Auxiliary Area: O(N1/2)
Consideration reader! Don’t cease studying now. Pay money for all of the essential mathematical ideas for aggressive programming with the Important Maths for CP Course at a student-friendly value. To finish your preparation from studying a language to DS Algo and lots of extra, please refer Full Interview Preparation Course.