Single Element in a Sorted Array
LeetCode 540 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
Pair alignment (even, odd) flips after the single element.
ನಾನು Single Element in a Sorted Array (LeetCode 540) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Peak / Check Mid Sides ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Before the single element, pairs start at even indices; after it, they start at odd indices. Look at mid and its partner: if the pairing is still 'even-first', the single element is to the right, otherwise to the left.
Approach
- 1
Handle n = 1 and the two ends.
- 2
If nums[mid] differs from both neighbours, it's the answer.
- 3
Even mid paired with mid+1 (or odd mid with mid−1) → go right; else go left.
- 4
Second version reasons about which half has odd length.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ
Even/odd pairing test.
The single lives in the odd-sized half.
Parity of indexPythonO(log n) time
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
n=len(nums)
if n==1:
return nums[0]
if nums[n-1]!=nums[n-2]:
return nums[n-1]
if nums[0]!=nums[1]:
return nums[0]
#[1,1,2,3,3,4,4,8,8]
#[0,1,2,3,4,5,6,7,8]
#even,odd - the number is on the right side
#odd,even - the number is on the left side
low = 0
high = n-1
while low<=high:
mid=(low+high)//2
if nums[mid]!=nums[mid-1] and nums[mid]!=nums[mid+1]:
return nums[mid]
#even,odd
if (mid%2==0 and nums[mid]==nums[mid+1]) or (mid%2==1 and nums[mid]==nums[mid-1]):
low =mid+1
else:
high= mid-1Odd-length halfPythonO(log n) time
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
n=len(nums)
if n==1:
return nums[0]
if nums[n-1]!=nums[n-2]:
return nums[n-1]
if nums[0]!=nums[1]:
return nums[0]
low = 0
high = n-1
while low<=high:
mid=(low+high)//2
#[1,1,2,3,3,4,4,8,8]
#[0,1,2,3,4,5,6,7,8]
#check the halves, odd half has the chance of having the single element
halvesareven = (high-mid)%2==0
#check the pair of the mid if it's on the right or on the left, we need to trim the search space based on that
if nums[mid]==nums[mid+1]:
if halvesareven==True:
low = mid+2
else:
high=mid-1
elif nums[mid]==nums[mid-1]:
if halvesareven==True:
high = mid-2
else:
low=mid+1
else:
return nums[mid]
return nums[low]Single Element in a Sorted Array — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Single Element in a Sorted Array ಹಿಂದಿನ intuition ಏನು?
Before the single element, pairs start at even indices; after it, they start at odd indices. Look at mid and its partner: if the pairing is still 'even-first', the single element is to the right, otherwise to the left.
Single Element in a Sorted Array (LeetCode 540) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. Handle n = 1 and the two ends. 2. If nums[mid] differs from both neighbours, it's the answer. 3. Even mid paired with mid+1 (or odd mid with mid−1) → go right; else go left. 4. Second version reasons about which half has odd length.
Single Element in a Sorted Array ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(log n). Space: O(1). (Accepted approach: Odd-length half.)
Single Element in a Sorted Array ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
Check the boundaries first so mid ± 1 is always valid.
Single Element in a Sorted Array ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?
Parity of index (O(log n) time): Even/odd pairing test. Odd-length half (O(log n) time): The single lives in the odd-sized half.