-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path914_jumpingChamion.cpp
119 lines (94 loc) · 2.16 KB
/
914_jumpingChamion.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using namespace std;
#include<iostream>
#include<cstring>
#include<algorithm>
#define n 1000010
int primelist[n];
long long int k,i,j,arr[n];
void primeFIND()
{
k=0;
memset(primelist,0,sizeof(primelist));
primelist[0]=primelist[1]=1;
for(i=2; i<n ; i++)
{
if(!primelist[i])
{
for (int j = 2; i * j < n; j++)
{
primelist[i * j] = 1;
}
arr[k++]=i;
}
}
}
long long int BS(long long int x)
{
long long int low=0,high=k-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]>x)
high=mid-1;
else if(arr[mid]<x)
low=mid+1;
else
return mid;
}
return 0;
}
int main()
{
long long int T,l,h,m,index1,index2,result[1000],temp[100000],max,Findex,temp2,salt;
primeFIND();
cin>>T;
while(T--)
{
m=0;
cin>>l>>h;
while(primelist[l])
l++;
while(primelist[h])
h--;
index1=BS(l);
index2=BS(h);
//cout<<index1<<" "<<index2<<endl;
if(index2-index1<1)
cout<<"No jumping champion"<<endl;
else
{
for(i=index1+1;i<=index2;i++)
{
temp[m++]=arr[i]-arr[i-1];
//cout<<temp[m-1]<<" ";
}
sort(temp,temp+m);
salt = m;
memset(result,0,sizeof result);
m=0;
for(i=temp[0];m<salt;i=temp[m])
{
result[i]++;
m++;
}
max = result[0];
Findex = 0;
for(i=1;i<=temp[salt-1];i++)
{
if(max<result[i])
{
max = result[i];
Findex = i;
}
else if(max == result[i])
temp2 = result[i];
// cout<<result[i]<<" ";
}
if(temp2==max)
cout<<"No jumping champion"<<endl;
else
cout<<"The jumping champion is "<<Findex<<endl;
}
}
return 0;
}