File tree Expand file tree Collapse file tree 2 files changed +34
-3
lines changed Expand file tree Collapse file tree 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) {
1818 int maxGap = 0 ;
1919 while (n > 0 ) {
2020 if ((n & 1 ) == 1 ) {
21- n = n >>> 1 ;
22- while (n > 0 && (n & 1 ) == 0 ) {
21+ while (n >>> 1 > 0 && (n >>> 1 & 1 ) == 0 ) {
2322 gap ++;
2423 n = n >>> 1 ;
2524 }
2625 if (gap > maxGap ) {
2726 maxGap = gap ;
28- gap = 0 ;
2927 }
28+ gap = 0 ;
3029 }
3130 n = n >>> 1 ;
3231 }
@@ -45,5 +44,7 @@ public static void main(String[] args) {
4544 System .out .println (findBinaryGap (16 ));
4645 System .out .println (findBinaryGap (17 ));
4746 System .out .println (findMaxNoOf0sBetweenTwo1s (121 ));
47+ System .out .println (findMaxNoOf0sBetweenTwo1s (1041 ));
48+ System .out .println (findMaxNoOf0sBetweenTwo1s (2_147_483_64889L ));
4849 }
4950}
You can’t perform that action at this time.
0 commit comments