Given an array arr of N positive integers. The task is to find the minimum and maximum prime elements in the given array.
Input: arr = 1, 3, 4, 5, 7 Output: Minimum : 3 Maximum : 7 Input: arr = 1, 2, 3, 4, 5, 6, 7, 11 Output: Minimum : 2 Maximum : 11
Take a variable min and max. Initialize min with INT_MAX and max with INT_MIN.Traverse the array and keep checking for every element if it is prime or not and update the minimum and maximum prime element at the same time.
Generate all primes upto maximum element of the array using sieve of Eratosthenes and store them in a hash. Now traverse the array and find the minimum and maximum element which are prime using the hash table.
Below is the implementation of above approach:
Minimum : 2 Maximum : 7
Time complexity : O(n*log(log(n)))
- Minimum and Maximum Prime Numbers of a Singly Linked List
- Maximum no. of contiguous Prime Numbers in an array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the 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
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print prime numbers with prime sum of digits in an array
- Sum of all the prime numbers with the maximum position of set bit ≤ D
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Pair of prime numbers with a given sum and minimum absolute difference
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Queries for maximum difference between prime numbers in given ranges
- Insert minimum number in array so that sum of array becomes prime
- Maximum and minimum of an array using minimum number of comparisons
- Minimum insertions to make a Co-prime array
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.