-
Notifications
You must be signed in to change notification settings - Fork 19.9k
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
Conversation
Misc/largestRange.java
Outdated
{ | ||
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) |
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.
The code may be failing prettier check because of this. According to pritter as far I know the correct code will be
for(int x : nums) num.put(x, true);
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.
Seems I was right, this is the actual format in the final format.
Now, first things first, you had sent PR in wrong branch :) . The correct branch is development. Now don`t close this PR, first lets fix the issue and then close the PR and make a new one. The above is not the only error so it is better to show how to format code. |
Misc/largestRange.java
Outdated
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 |
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.
You would face a lot of problem while formatting this comment. Rather than using //
try something like this -
/** Stores a mapping of a number to whether the current number {NEW_LINE} * is part of a particular consecutive range or not. */
These javadoc comments are formated better by google-java-format, as well as prettier.
After doing the above steps download Prettier, and install it. Now comes the trick part. What actually happens here is that usually Prettier and Google-java-format collide and undo each others code changes. What worked for is the following pattern -
For confirmation that this work go here Prettier-test. This is exactly your formatted code, which has passed all the tests. |
Misc/largestRange.java
Outdated
import java.util.*; | ||
public class largestRange | ||
{ | ||
// Finds the length of longest occurring consecutive numbers range in an array |
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.
Add javadoc comments explaining, what this code does in breif.
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.
Better do that before formatting so mentioned it right away.
Well thanks, @AkMo3 for such a detailed explanation, but the prettier check has failed again despite using the formatted code sent by you. Should I format these on my own device following all the said steps before committing here? |
I just ran through test results, the problem is not with your code. The test is failing because it is not able to fetch your repository, so the test is failing. Close this PR and try this on development branch from your development branch. Hope this things get solved then. The code is formatted properly no issue with the code. |
@AkMo3 I'm unable to send a PR from my development branch to this development branch. It doesn't allow to set more than one upstreams on my local machine :( |
No need to send from your machine. Send PR to your development branch from AzadN:Largest_Range using website only. Squash Merge them. Then send PR to TheAlgorithms development branch. |
* 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); |
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,
int[] input_arr = new int[]{0,1,2,4,5,6,0,1};
int count = 1;
int temp_count = 1;
for (int i = 1; i < input_arr.length; i++) {
if (input_arr[i] != input_arr[i - 1] + 1) {
if (temp_count > count) {
count = temp_count;
}
temp_count = 1;
} else temp_count++;
}
System.out.println(count); // Prints 3
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.
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions! |
@shellhub as discussed in PR #1990, this is the 1st separate pull request where I've added a new Java File to find the longest length of consecutive numbers occurring in an array. More to follow :)
@Kush1101 could you too review this and my other PRs too?
Describe your change:
References
Checklist:
Fixes: #{$ISSUE_NO}
.