

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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
- 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
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Find the smallest and second smallest elements in an array in C++
- Java program to find the smallest number in an array
- Find the average of all elements of array except the largest and smallest - JavaScript
- Program to find minimum difference between largest and smallest value in three moves using Python
- Java program to find the largest number in an array
- Queries to return the absolute difference between Lth smallest number and the R-th smallest number in C++
- Java program to find the 2nd smallest number in an array
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Program to find out the k-th smallest difference between all element pairs in an array in C++
- What is the difference between a list and an array in C#?
Advertisements