Skip to content

Conversation

@rajyalakshmimannem
Copy link

Binary-Search-2

Explain your approach in three sentences only at top of your code

Problem 1: (https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

Problem 2: (https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

Find the minimum element.

You may assume no duplicate exists in the array.

Example 1:
Input: [3,4,5,1,2]
Output: 1

Example 2:
Input: [4,5,6,7,0,1,2]
Output: 0

Problem 3: (https://leetcode.com/problems/find-peak-element/)

A peak element is an element that is greater than its neighbors.

Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that nums[-1] = nums[n] = -∞.

Example 1:

Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:

Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2,

         or index number 5 where the peak element is 6.

Note:

Your solution should be in logarithmic complexity.

@super30admin
Copy link
Owner

  1. Correctness: Your solution for "FindFirstAndLastPositionofElementInSortedArray" is correct and handles edge cases well. Good job!
  2. Time and Space Complexity: Your solution matches the optimal time and space complexity. Well done.
  3. Code Quality: The code is clean, readable, and well-commented. The variable names are descriptive, and the logic is easy to follow.
  4. Efficiency: The solution is efficient, but you could consider adding a check for the target being outside the range of the array (as you did) to potentially save unnecessary searches.
  5. Irrelevant Code: Please avoid submitting solutions for unrelated problems in the same file. Focus on the problem at hand to keep your submissions clear and concise.

@super30admin
Copy link
Owner

  • The solution for "FindFirstAndLastPositionofElementInSortedArray" is well-implemented and correct. Good job on handling edge cases and using binary search effectively.
  • The code is readable and follows good practices. However, it would be better to separate unrelated problems into different files or classes to maintain clarity and focus.
  • The magic numbers (e.g., 34678, 345652) in the unrelated problems should be avoided. Instead, return a meaningful default value or throw an exception if no solution is found.
  • The comments are helpful, but ensure they are consistent and cover all critical parts of the code.

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