-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindElementThatAppearsOnce.cpp
50 lines (42 loc) · 1.01 KB
/
FindElementThatAppearsOnce.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Find the element that appears once
// Given a sorted array A[] of N positive integers having all the numbers occurring exactly twice, except for one number
// which will occur only once. Find the number occurring only once.
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
int search(int a[], int n){
//code
int mid,begin=0,end=n;
while(begin<=end){
mid=begin+(end-begin)/2;
if(a[mid]==a[mid+1] && mid%2==0 || a[mid]==a[mid-1] && mid%2!=0)
begin=mid+1;
else
end=mid-1;
}
return a[begin];
}
};
// { Driver Code Starts.
// Driver program
int main()
{
int t,len;
cin>>t;
while(t--)
{
cin>>len;
int arr[len];
for(int i=0;i<len;i++){
cin>>arr[i];
}
Solution ob;
cout<<ob.search(arr, len)<<'\n';
}
return 0;
}
// } Driver Code Ends