-
Notifications
You must be signed in to change notification settings - Fork 20.3k
Added New Java file in Java/Misc #2005
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0172c29
Added New Java file in Java/Misc
AzadN ee34da4
Update largestRange.java
AzadN 4bee137
Update largestRange.java
AzadN 603e010
Update largestRange.java
AzadN a6f88cd
Update largestRange.java
AzadN 653fc60
Update largestRange.java
AzadN 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
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,46 @@ | ||
import java.util.*; | ||
|
||
public class largestRange { | ||
|
||
// Finds the length of longest occurring consecutive numbers range in an array | ||
public static int longestRange(int[] nums) { | ||
int longestRange = 0; | ||
HashMap<Integer, Boolean> num = new HashMap<>(); | ||
|
||
/** | ||
* Stores a mapping of a number to whether the current number is part of a particular | ||
* consecutive range or not. | ||
*/ | ||
for (int x : nums) num.put(x, true); | ||
for (int x : nums) { | ||
if (!num.get(x)) continue; | ||
num.replace(x, false); | ||
int currentRange = 1; | ||
int left = x - 1; | ||
int right = x + 1; | ||
while (num.containsKey(left)) { // Search leftwards for consecutive range | ||
num.replace(left, false); | ||
currentRange += 1; | ||
left--; | ||
} | ||
while (num.containsKey(right)) { // Search rightwards for consecutive range | ||
num.replace(right, false); | ||
currentRange += 1; | ||
right++; | ||
} | ||
if (currentRange > longestRange) | ||
longestRange = currentRange; // Store longest range at every interation | ||
} | ||
return longestRange; | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Testcases | ||
assert longestRange(new int[] {1, 2, 3, 4, -1, 11, 10}) == 4; | ||
// The longest consecutive number range is of length 4 i.e. {1, 2, 3, 4} | ||
assert longestRange(new int[] {-1, 1, 3, 5, 7}) == 1; | ||
// The longest consecutive number range is of length 1 i.e. any of the elements alone | ||
assert longestRange(new int[] {0, 1, 2, 3, 4, 7, 6, 5}) == 8; | ||
// The longest consecutive number range is of length 8 i.e. {0, 1, 2, 3, 4, 5, 6, 7} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm not sure but can't we just use lookahead type of approach over here?
For example,
If I have misunderstood requirements or this does not work as intended (I haven't tested it, so it is be possible), please let me know.
If above works just as fine, please can you explain this approach's necessity?
This is just a suggestion, not a requirement.
Please do needful and let me know.