-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandidateCode1.java
More file actions
197 lines (143 loc) · 3.5 KB
/
CandidateCode1.java
File metadata and controls
197 lines (143 loc) · 3.5 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
PROBLEM STATEMENT
Prime Game
Rax, a school student, was bored at home in the pandemic. He wanted to play but there was no one to play with. He was doing some mathematics questions including prime numbers and thought of creating a game using the same. After a few days of work, he was ready with his game. He wants to play the game with you.
GAME:
Rax will randomly provide you a range [ L , R ] (both inclusive) and you have to tell him the maximum difference between the prime numbers in the given range. There are three answers possible for the given range.
There are two distinct prime numbers in the given range so the maximum difference can be found.
There is only one distinct prime number in the given range. The maximum difference in this case would be 0.
There are no prime numbers in the given range. The output for this case would be -1.
To win the game, the participant should answer the prime difference correctly for the given range.
Example:
Range: [ 1, 10 ]
The maximum difference between the prime numbers in the given range is 5.
Difference = 7 - 2 = 5
Range: [ 5, 5 ]
There is only one distinct prime number so the maximum difference would be 0.
Range: [ 8 , 10 ]
There is no prime number in the given range so the output for the given range would be -1.
Can you win the game?
Input Format
The first line of input consists of the number of test cases, T
Next T lines each consists of two space-separated integers, L and R
Constraints
1<= T <=10
2<= L<= R<=10^6
Output Format
For each test case, print the maximum difference in the given range in a separate line.
Sample TestCase 1
Input
5
5 5
2 7
8 10
10 20
4 5
Output
0
5
-1
8
0
Explanation
Test Case 1: [ 5 - 5 ] = 0
Test Case 2: [ 7 - 2 ] = 5
Test Case 3: No prime number in the given range. Output = -1
Test Case 4: [ 19 - 11 ] = 8
Test Case 5: The difference would be 0 since there is only one prime number in the given range.
*/
import java.util.Scanner;
public class CandidateCode1
{
public int primeR(int l,int r)
{
for (int i = r; i >= l; i--)
{
int m = 0, flag = 0;
int n = i;
m = n/2;
if(n == 0 || n == 1)
{
continue;
}
else
{
for(int j = 2; j <= m; j++)
{
if(n%j == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
return n;
}
}
}
return -1;
}
public int primeL(int l, int r)
{
for (int i = l; i <= r; i++)
{
int m = 0, flag = 0;
int n = i;
m = n/2;
if(n == 0 || n == 1)
{
continue;
}
else
{
for(int j = 2; j <= m; j++)
{
if(n%j == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
return n;
}
}
}
return -1;
}
public void primeGame(int l, int r)
{
CandidateCode obj1 = new CandidateCode();
int pL = obj1.primeL(l, r);
int pR = obj1.primeR(l, r);
if(pL == -1 && pR == -1)
{
System.out.println("-1");
}
else
{
System.out.println(pR-pL);
}
}
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String [] input = new String[n];
sc.nextLine();
for (int i = 0; i < n; i++)
{
input[i] = sc.nextLine();
}
CandidateCode obj = new CandidateCode();
for (String str : input)
{
String [] num = str.split(" ");
int l = Integer.parseInt(num[0]);
int r = Integer.parseInt(num[1]);
obj.primeGame(l, r);
}
}
}