Single Element in a Sorted Array
You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.
Return the single element that appears only once.
Your solution must run in O(log n)
time and O(1)
space.
Example 1:
Input: nums = [1,1,2,3,3,4,4,8,8] Output: 2
Example 2:
Input: nums = [3,3,7,7,10,11,11] Output: 10
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 105
Solution
Observe that the parity (even or odd) of indices ties closely with where the single element is.
We know that the numbers come in pairs before and after the one single element s
.
For the pairs that is to the left of s
: the first element takes an even index e
(as array is 0 indexed) and the second element takes an odd index e+1
.
Then the single element s
takes only one position (even), so that the pattern on the right of s
is reversed.
For the pairs that is to the right of s
: the first element takes an odd index o
, and the second element takes an even index o+1
Therefore, for an even index e
, nums[e]!=nums[e+1]
if the s
is to the left of o
.
Similar for an odd index o
, nums[o]!=nums[o-1]
means s
has already appeared.
We must also keep an eye out for out of bounds
, that is, to check whether idx
is the last index in nums
.
Implementation
1def singleNonDuplicate(self, nums):
2 def to_the_left(idx):
3 if (idx == len(nums)-1):
4 return True
5 elif (idx % 2): # odd
6 return nums[idx] != nums[idx-1]
7 else: # even
8 return nums[idx] != nums[idx+1]
9
10 left, right, ans = 0, len(nums)-1, -1
11 while left <= right:
12 mid = (left + right) // 2
13 if to_the_left(mid):
14 ans = mid
15 right = mid - 1
16 else:
17 left = mid + 1
18
19 return nums[ans]
Which algorithm is best for finding the shortest distance between two points in an unweighted graph?
How does merge sort divide the problem into subproblems?
What is the best way of checking if an element exists in a sorted array once in terms of time complexity? Select the best that applies.
A person thinks of a number between 1 and 1000. You may ask any number questions to them, provided that the question can be answered with either "yes" or "no".
What is the minimum number of questions you needed to ask so that you are guaranteed to know the number that the person is thinking?
Recommended Readings
Top Patterns to Conquer the Technical Coding Interview Should the written word bore you fear not A delightful video alternative awaits iframe width 560 height 315 src https www youtube com embed LW8Io6IPYHw title YouTube video player frameborder 0 allow accelerometer autoplay clipboard write encrypted media gyroscope picture in picture
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Got a question? Ask the Teaching Assistant anything you don't understand.
Still not clear? Ask in the Forum, Discord or Submit the part you don't understand to our editors.