File tree 2 files changed +34
-3
lines changed
2 files changed +34
-3
lines changed Original file line number Diff line number Diff line change
1
+ package me .ramswaroop .arrays ;
2
+
3
+ /**
4
+ * Created by ramswaroop on 31/05/2016.
5
+ */
6
+ public class CountDivisors {
7
+
8
+ /**
9
+ * Counts the number of integers in the range {@param begin} and
10
+ * {@param end} that are divisible by {@param n}.
11
+ *
12
+ * @param begin
13
+ * @param end
14
+ * @param n
15
+ * @return
16
+ */
17
+ public static int countDivisorsInRange (int begin , int end , int n ) {
18
+ int b = end / n + 1 ; // From 0 to end the integers divisible by n
19
+ int a = begin / n + 1 ; // From 0 to begin the integers divisible by n
20
+
21
+ if (begin % n == 0 ) { // "begin" is inclusive; if divisible by n then
22
+ --a ; // remove 1 from "a"
23
+ }
24
+ return b - a ; // return integers in range
25
+ }
26
+
27
+ public static void main (String [] a ) {
28
+ countDivisorsInRange (0 , 2000000000 , 5 );
29
+ }
30
+ }
Original file line number Diff line number Diff line change @@ -18,15 +18,14 @@ public static int findBinaryGap(long n) {
18
18
int maxGap = 0 ;
19
19
while (n > 0 ) {
20
20
if ((n & 1 ) == 1 ) {
21
- n = n >>> 1 ;
22
- while (n > 0 && (n & 1 ) == 0 ) {
21
+ while (n >>> 1 > 0 && (n >>> 1 & 1 ) == 0 ) {
23
22
gap ++;
24
23
n = n >>> 1 ;
25
24
}
26
25
if (gap > maxGap ) {
27
26
maxGap = gap ;
28
- gap = 0 ;
29
27
}
28
+ gap = 0 ;
30
29
}
31
30
n = n >>> 1 ;
32
31
}
@@ -45,5 +44,7 @@ public static void main(String[] args) {
45
44
System .out .println (findBinaryGap (16 ));
46
45
System .out .println (findBinaryGap (17 ));
47
46
System .out .println (findMaxNoOf0sBetweenTwo1s (121 ));
47
+ System .out .println (findMaxNoOf0sBetweenTwo1s (1041 ));
48
+ System .out .println (findMaxNoOf0sBetweenTwo1s (2_147_483_64889L ));
48
49
}
49
50
}
You can’t perform that action at this time.
0 commit comments