File tree Expand file tree Collapse file tree 2 files changed +98
-0
lines changed
Hackerrank/Algorithms/Search Expand file tree Collapse file tree 2 files changed +98
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .math .*;
3
+ import java .security .*;
4
+ import java .text .*;
5
+ import java .util .*;
6
+ import java .util .concurrent .*;
7
+ import java .util .regex .*;
8
+
9
+ public class Solution {
10
+
11
+ // Complete the hackerlandRadioTransmitters function below.
12
+ static int hackerlandRadioTransmitters (int [] x , int k ) {
13
+ Arrays .sort (x );
14
+ int count = 0 ;
15
+ int i = 0 ;
16
+ int n = x .length ;
17
+ while (i < n ) {
18
+ count ++;
19
+ int pos = x [i ] + k ;
20
+ while (i < n && x [i ] <= pos ) {
21
+ i ++;
22
+ }
23
+
24
+ i --;
25
+ pos = x [i ] + k ;
26
+ while (i <n && x [i ] <= pos ) {
27
+ i ++;
28
+ }
29
+ }
30
+
31
+ return count ;
32
+ }
33
+
34
+ private static final Scanner scanner = new Scanner (System .in );
35
+
36
+ public static void main (String [] args ) throws IOException {
37
+ BufferedWriter bufferedWriter = new BufferedWriter (new FileWriter (System .getenv ("OUTPUT_PATH" )));
38
+
39
+ String [] nk = scanner .nextLine ().split (" " );
40
+
41
+ int n = Integer .parseInt (nk [0 ]);
42
+
43
+ int k = Integer .parseInt (nk [1 ]);
44
+
45
+ int [] x = new int [n ];
46
+
47
+ String [] xItems = scanner .nextLine ().split (" " );
48
+ scanner .skip ("(\r \n |[\n \r \u2028 \u2029 \u0085 ])?" );
49
+
50
+ for (int i = 0 ; i < n ; i ++) {
51
+ int xItem = Integer .parseInt (xItems [i ]);
52
+ x [i ] = xItem ;
53
+ }
54
+
55
+ int result = hackerlandRadioTransmitters (x , k );
56
+
57
+ bufferedWriter .write (String .valueOf (result ));
58
+ bufferedWriter .newLine ();
59
+
60
+ bufferedWriter .close ();
61
+
62
+ scanner .close ();
63
+ }
64
+ }
65
+
Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .ArrayList ;
3
+ import java .util .List ;
4
+
5
+ public class FileHandling {
6
+
7
+ public List <String > readFile (String fileName ) throws IOException {
8
+ List <String > lines = new ArrayList <>();
9
+ File file = new File (fileName );
10
+ BufferedReader br = new BufferedReader (new FileReader (file ));
11
+
12
+ String line ;
13
+ while ((line = br .readLine ()) != null ) {
14
+ lines .add (line );
15
+ }
16
+
17
+ br .close ();
18
+
19
+ return lines ;
20
+ }
21
+
22
+ public void writeToFile (List <String > data , String fileName ) throws IOException {
23
+ File file = new File (fileName );
24
+ BufferedWriter bw = new BufferedWriter (new FileWriter (file ));
25
+
26
+ for (String line : data ) {
27
+ bw .write (line + "\n " );
28
+ }
29
+
30
+ bw .close ();
31
+ }
32
+ }
33
+
You can’t perform that action at this time.
0 commit comments