You are given a sorted array ‘arr’ of ‘n’ numbers such that every number occurred twice in the array except one, which appears only once.
Return the number that appears once.
Input: 'arr' = [1,1,2,2,4,5,5]
Output: 4
Explanation:
Number 4 only appears once the array.
Exactly one number in the array 'arr' appears once.
The first line contains a single integer ‘n’, representing the total number of elements present in the array 'arr'.
The next line contains ‘n’ single-spaced elements, representing the elements of the array 'arr'.
The only line contains the integer denoting the number that appears once in the array.
##### Note : You do not need to print anything, it has already been taken care of. Just implement the given function.
5
1 1 3 5 5
3
Given array is [1, 1, 3, 5, 5]
Here, 3 occurs once in the array. So, the answer is 3.
5
1 1 4 4 15
15
The array is [1, 1, 4, 4, 15].
Here, 15 occurs once in the array. So, the answer is 15.
Try to solve this in O(log(n)).
1 <= n <= 10^5
0 <= arr[i] <= 10^9
Time Limit: 1 sec
Use the fact that the array is sorted and for each element of the array, check if it is Shizuka’s lucky number.
The idea here is to use the fact that the array is sorted and the element of the array (‘arr[i]’) is unique if it doesn't have an adjacent element that has the same value as ‘arr[i]’.
Algorithm:
O(N), where ‘N’ is the total number of elements in the given array.
We are running a single loop here which takes O(N) time in the worst case.
O(1).
No extra space is being used.