Find Duplicate in Sorted Array
EasyPractice library · +10 XPtwo pointersarraysbinary search
Given a sorted array of integers, find any element that appears at least twice. The array contains N integers, where N is at least 2. The integers in the array are between 1 and N inclusive.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers, representing the elements of the sorted array.
Output
Print a single integer: any element that appears at least twice.
Constraints
2 <= N <= 1000 1 <= arr[i] <= N for all i
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 1 2 2 3 4
Output
2
The number 2 appears twice.
Example 2
Input
7 1 2 3 4 5 6 6
Output
6
The number 6 appears twice.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 2 3 4
Example 2 input
7 1 2 3 4 5 6 6