Skip to content

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Jun 22, 2023

Solved find-first-and-last-position-of-element-in-sorted-array in js.

File(s) Added: 0034-find-first-and-last-position-of-element-in-sorted-array.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/985379429/

Solved find-first-and-last-position-of-element-in-sorted-array in js.
if(target < nums[mid]) {
right = mid-1;
}
// this is the meat of the code
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Remove comment and creat descriptive variables or functions

const isTarget = (...);
if (isTarget) { ... }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

result.push(binarySearch(true));
result.push(binarySearch(false));

function binarySearch(leftBias) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: name booleans that prompt a question

isLeftBias

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a kind reminder that I updated the code as suggested.

result.push(binarySearch(true));
result.push(binarySearch(false));

function binarySearch(isLeftBias) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid nested functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


while(left <= right) {

const mid = Math.floor((left+right)/2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use bitwise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Using the bitwise operator and removing the inner function.
@aakhtar3 aakhtar3 merged commit 0d6f0ea into neetcode-gh:main Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants