Input: K = 3 2 / \ 3 5 /\ / \ 7 11 13 17 Output :7, 11, 13, 17 Explanation: 2 / \ 3 5 /\ / \ 7 11 13 17 So primes present at level 3 : 7, 11, 13, 17 Input :K = 2 2 / \ 3 5 Output :3 5
Naive Approach: The naive approach is to build a binary tree of prime numbers and then get elements at a particular level k.
It doesn’t work well for large numbers as it takes too much time.
Efficient approach: Suppose there are n elements and the task is to build a binary tree using those n elements, then they can be built using log2n levels.
Therefore, given a level k, elements present here is from 2k-1 to 2k-1 if all the prime numbers are present in a 1D array.
Hence, the following is the algorithm:
- Find the prime numbers upto MAX_SIZE using Sieve of Eratosthenes.
- Calculate the left_index and right_index of the level as left_index = 2k-1, right_index = 2k-1.
- Output primes from left_index to right_index of prime array.
primes at level 1: 2 primes at level 2: 3 5 primes at level 3: 7 11 13 17 primes at level 4: 19 23 29 31 37 41 43 47
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Sum of all nodes at Kth level in a Binary Tree
- Nodes at Kth level without duplicates in a Binary Tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Count the Number of Binary Search Trees present in a Binary Tree
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Print nodes between two given level numbers of a binary tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Find the sum of prime numbers in the Kth array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count balanced nodes present in a binary tree
- Connect Nodes at same Level (Level Order Traversal)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.