Skip to content

Conversation

@Balaviknesh
Copy link

…inimum in rotated sorted array

@super30admin
Copy link
Owner

Your solution for "Find the First and Last Position of an Element in given Sorted Array" is correct and meets the required time and space complexity. Well done!

Here are some suggestions to improve the code quality:

  1. Use descriptive function names: Instead of binarySearch1 and binarySearch2, consider naming them findFirstOccurrence and findLastOccurrence to make the code more readable.
  2. Maintain consistency in parameter order: Both binary search functions should have the same parameter order, preferably (nums: IntArray, target: Int).
  3. Simplify the binary search conditions: In the else block (when nums[mid] != target), you can directly check:
    • For binarySearch1 (first occurrence): if nums[mid] < target then low = mid + 1 else high = mid - 1. Actually, the current code does this correctly but with a condition that is a bit redundant. You can write:
    } else if (nums[mid] < target) {
        low = mid + 1
    } else {
        high = mid - 1
    }
    
    Similarly for binarySearch2.
  4. Consider adding comments to explain the purpose of each function and the key conditions.

Also, note that you included solutions for other problems in the same submission. While it's great that you are practicing multiple problems, when submitting a solution for a specific problem, it's best to only include the relevant code to avoid confusion.

Keep up the good work!

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