- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between the largest and the smallest primes in an array in Java
Problem Statement
With a given array of integers where all elements are less than 1000000. Find the difference between the largest and the smallest primes in an array.
Example
Array: [ 1, 2, 3, 4, 5 ] Largest Prime Number = 5 Smallest Prime Number = 2 Difference = 5 - 3 = 2.
Solution
Use Sieve of Eratosthenes approach, which is an efficient way to find out all prime numbers smaller than a given number. Then we will figure out the largest and smallest prime number to get the required difference.
Example
Following is the program in Java to find the required output.
public class JavaTester { static int MAX = 1000000; static boolean prime[] = new boolean[MAX + 1]; public static void runSieveOfEratosthenes(){ //reset prime flags to be true for(int i=0; i< MAX+1; i++) prime[i] = true; //set 1 as non-prime prime[1] = false; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not modified, then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } } public static int difference(int arr[]){ int min = MAX + 2; int max = -1; for (int i = 0; i < arr.length; i++) { // check if the number is prime or not if (prime[arr[i]] == true) { // set the max and min values if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; } } return max - min; } public static void main(String args[]){ // run the sieve runSieveOfEratosthenes(); int arr[] = { 1, 2, 3, 4, 5 }; System.out.println(difference(arr)); } }
Output
3
- Related Articles
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Find the difference of largest and the smallest number in an array without sorting it in JavaScript
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- C program to find the second largest and smallest numbers in an array
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Find the difference between the largest 3 digit number and the smallest 6 digit number.
- Find the difference between the largest 8-digit number and the smallest 6-digit number.
- What is the difference between the largest and smallest numbers formed by the digits ( 4,6,9,8 ? )
- Find the difference between the smallest 3 digit prime number and largest 1digit prime number
- Find the difference between the smallest number of 7 digits and the largest number of digits.
- Find the difference between the smallest number of 7 digits and the largest number of 4 digits.
- Find the smallest and second smallest elements in an array in C++
- Program to find minimum difference between largest and smallest value in three moves using Python
- Java program to find the smallest number in an array
- Java program to find the largest number in an array

Advertisements