-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path50890818.cpp
76 lines (75 loc) · 1.43 KB
/
50890818.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include<algorithm>
using namespace std;
int searchit(int arr[],int n,int val);
int saveus(int arr[],int n,int k);
int main()
{
// get inputs
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
// sort it
sort(arr,arr+n);
int mini=arr[0]+arr[n/2]-1;
int maxi=arr[n-1]+arr[n/2]+1;
int result=0;
for(int i=mini;i<=maxi;i++)
{
int alaki=saveus(arr,n,i);
//cout<<"alaki="<<alaki<<endl;
if(alaki>result)
result=alaki;
}
cout<<result<<endl;
return 0;
}
// binary search
int searchit(int arr[],int n,int val)// n = size arr
{
int first,last;
first=0;
last=n-1;
int i;
while(first<last)
{
//cout<<"f="<<first<<" l="<<last<<endl;
i=first+(last-first)/2;
if(arr[i]==val) return i;
if(arr[i]>val)
{
last=i-1;
}
else if(arr[i]<val)
{
first=i+1;
}
}
if(arr[first]==val)
{
return first;
}
return -1;
}
int saveus(int arr[],int n,int k)
{
int sum=0;
// cout<<"\tk="<<k<<endl;
for(int i=0;arr[i]<(float)k/2;i++)
{
if(searchit(arr,n,k-arr[i])>-1)
{
//cout<<"justhere"<<endl;
if(arr[i]!=k/2 || n%2==0)
{
sum++;
//cout<<"here"<<endl;
}
}
}
return sum;
}