-
Notifications
You must be signed in to change notification settings - Fork 20
Solution #2523 - Mridul/Edited - 07/06/2025 #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mrid88
wants to merge
2
commits into
Dijkstra-Edu:master
Choose a base branch
from
mrid88:feature/closest-prime-number
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Daily Questions/2523 - Closest Prime Numbers in Range - Medium/Explanation.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
private static boolean[] isPrime = new boolean[1000001]; This boolean array is used to mark prime numbers up to 1000000. The size of the array is 1000001 to include the number 1000000. | ||
|
||
Static Block: | ||
The static block is used to initialize the isPrime array and precompute the prime numbers. | ||
|
||
Arrays.fill(isPrime, true); This method sets all elements of the isPrime array to true, assuming all numbers are prime initially. | ||
|
||
precompute(); This method is called to mark non-prime numbers in the isPrime array. | ||
|
||
Method: precompute() | ||
This method uses the Sieve of Eratosthenes algorithm to mark non-prime numbers in the isPrime array. | ||
|
||
isPrime[0] = isPrime[1] = false; Numbers 0 and 1 are not prime. | ||
|
||
A loop is used to iterate from 2 to sqrt(1000000). | ||
|
||
If isPrime[i] is true, all multiples of i are marked as false (not prime) starting from i*i. | ||
|
||
Method: closestPrimes(int left, int right) | ||
This method finds the closest prime numbers within the range [left, right]. | ||
|
||
int prev = -1, minDiff = Integer.MAX_VALUE; Variables to track the previous prime number and the minimum difference between consecutive primes. | ||
|
||
int num1 = -1, num2 = -1; Variables to store the closest prime numbers. | ||
|
||
A loop iterates through the range [left, right]. | ||
|
||
If isPrime[i] is true, the number i is a prime. | ||
|
||
If prev is not -1 and the difference (i - prev) is less than minDiff, update minDiff and set num1 to prev and num2 to i. | ||
|
||
Update prev to i. | ||
|
||
If no prime numbers are found, return new int[]{-1, -1}. Otherwise, return the closest prime numbers num1 and num2. | ||
39 changes: 39 additions & 0 deletions
39
Daily Questions/2523 - Closest Prime Numbers in Range - Medium/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
private static boolean[] isPrime = new boolean[1000001]; | ||
|
||
static { | ||
Arrays.fill(isPrime, true); | ||
precompute(); | ||
} | ||
|
||
private static void precompute() { | ||
isPrime[0] = isPrime[1] = false; | ||
for (int i = 2; i * i <= 1000000; i++) { | ||
if (isPrime[i]) { | ||
for (int j = i * i; j <= 1000000; j += i) { | ||
isPrime[j] = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public int[] closestPrimes(int left, int right) { | ||
int prev = -1, minDiff = Integer.MAX_VALUE; | ||
int num1 = -1, num2 = -1; | ||
|
||
for (int i = left; i <= right; i++) { | ||
if (isPrime[i]) { | ||
if (prev != -1 && (i - prev < minDiff)) { | ||
minDiff = i - prev; | ||
num1 = prev; | ||
num2 = i; | ||
} | ||
prev = i; | ||
} | ||
} | ||
|
||
return (num1 == -1) ? new int[]{-1, -1} : new int[]{num1, num2}; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be improved quite a bit! Use Markdown, and explain the time and space complexity as well!