Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to Minimum and Maximum prime numbers in an array
In C programming, finding the minimum and maximum prime numbers in an array requires two key steps: identifying prime numbers efficiently and tracking the smallest and largest primes found. This problem uses the Sieve of Eratosthenes algorithm for efficient prime number generation.
Problem Statement
Given an array of n positive integers, we need to find the prime numbers with minimum and maximum values.
If the given array is −
arr[] = {10, 4, 1, 12, 13, 7, 6, 2, 27, 33}
then minimum prime number is 2 and maximum prime number is 13
Algorithm
1. Find maximum number from given array. Let us call it maxNumber 2. Generate prime numbers from 1 to maxNumber using Sieve of Eratosthenes 3. Iterate input array and find prime numbers with minimum and maximum values 4. Display the results
Example
Here's the complete C program to find minimum and maximum prime numbers in an array −
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
int findMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
void printMinAndMaxPrimes(int arr[], int n) {
int maxNumber = findMax(arr, n);
/* Create boolean array for sieve */
bool primes[maxNumber + 1];
for (int i = 0; i <= maxNumber; i++) {
primes[i] = true;
}
primes[0] = primes[1] = false;
/* Sieve of Eratosthenes */
for (int p = 2; p * p <= maxNumber; p++) {
if (primes[p]) {
for (int i = p * p; i <= maxNumber; i += p) {
primes[i] = false;
}
}
}
int minPrime = INT_MAX;
int maxPrime = INT_MIN;
bool foundPrime = false;
/* Find min and max primes in array */
for (int i = 0; i < n; i++) {
if (arr[i] >= 2 && primes[arr[i]]) {
foundPrime = true;
if (arr[i] < minPrime) {
minPrime = arr[i];
}
if (arr[i] > maxPrime) {
maxPrime = arr[i];
}
}
}
if (foundPrime) {
printf("Prime number of min value = %d<br>", minPrime);
printf("Prime number of max value = %d<br>", maxPrime);
} else {
printf("No prime numbers found in the array<br>");
}
}
int main() {
int arr[] = {10, 4, 1, 12, 13, 7, 6, 2, 27, 33};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("<br>");
printMinAndMaxPrimes(arr, n);
return 0;
}
Output
When you compile and execute the above program, it generates the following output −
Array: 10 4 1 12 13 7 6 2 27 33 Prime number of min value = 2 Prime number of max value = 13
How It Works
- The Sieve of Eratosthenes efficiently marks all prime numbers up to the maximum value in the array.
- We iterate through the array once to find primes and track minimum and maximum values simultaneously.
- The algorithm has O(n log log m) time complexity where n is array size and m is the maximum number.
- Space complexity is O(m) for the boolean sieve array.
Conclusion
This approach efficiently finds minimum and maximum prime numbers using the Sieve of Eratosthenes for prime generation. The algorithm handles edge cases and provides optimal performance for large arrays with high maximum values.
