Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a30ac9e

Browse files
committed
Minor improvements/corrections
1 parent 43c7782 commit a30ac9e

File tree

6 files changed

+35
-39
lines changed

6 files changed

+35
-39
lines changed

‎src/main/java/com/rampatra/arrays/ArrangeNosToFormBiggestNo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public class ArrangeNosToFormBiggestNo {
1717
* <p/>
1818
* For example,
1919
* I/P: {54, 546, 548, 60}
20-
* O/P: {60, 548, 546, 54} i.e, 6054854654
20+
* O/P: {60, 548, 546, 54}, i.e, 6054854654
2121
* <p/>
2222
* I/P: {1, 34, 3, 98, 9, 76, 45, 4}
23-
* O/P: {9, 98, 76, 45, 4, 34, 3, 1} i.e, 998764543431
23+
* O/P: {9, 98, 76, 45, 4, 34, 3, 1}, i.e, 998764543431
2424
*
2525
* @param numbers input integer array
2626
* @return integer array where if you concatenate all its elements, you will get the largest number
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.rampatra.arrays;
22

33
/**
4-
* Created by rampatra on 31/05/2016.
4+
* @author rampatra
5+
* @since 31/05/2016
56
*/
67
public class CountDivisors {
78

89
/**
9-
* Counts the number of integers in the range {@param begin} and
10-
* {@param end} that are divisible by {@param n}.
10+
* Counts the number of integers in the range {@code begin}
11+
* and {@code end} that are divisible by {@code n}.
1112
*
1213
* @param begin
1314
* @param end
1415
* @param n
1516
* @return
1617
*/
17-
public static int countDivisorsInRange(int begin, int end, int n) {
18+
private static int countDivisorsInRange(int begin, int end, int n) {
1819
int b = end / n + 1; // From 0 to end the integers divisible by n
1920
int a = begin / n + 1; // From 0 to begin the integers divisible by n
2021

@@ -25,6 +26,10 @@ public static int countDivisorsInRange(int begin, int end, int n) {
2526
}
2627

2728
public static void main(String[] args) {
28-
countDivisorsInRange(0, 2000000000, 5);
29+
System.out.println(countDivisorsInRange(0, 0, 5));
30+
System.out.println(countDivisorsInRange(1, 1, 5));
31+
System.out.println(countDivisorsInRange(0, 1, 5));
32+
System.out.println(countDivisorsInRange(0, 10, 5));
33+
System.out.println(countDivisorsInRange(0, 2000000000, 5));
2934
}
30-
}
35+
}

‎src/main/java/com/rampatra/arrays/DuplicatesInArray.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
*
88
* @author rampatra
99
* @since 8/21/15
10-
* @time: 9:41 AM
1110
*/
1211
public class DuplicatesInArray {
1312

1413
/**
1514
* Given an array of n elements which contains elements from 0 to n-1, with any of
1615
* these numbers appearing any number of times. Find these repeating numbers in O(n)
17-
* and using only constant memory space.
16+
* time complexity.
1817
* <p/>
1918
* For example, let n be 7 and array be {1, 2, 3, 1, 3, 6, 6}, the answer should be
2019
* 1, 3 and 6.

‎src/main/java/com/rampatra/arrays/DuplicatesInArrayWithinKDistance.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
*
99
* @author rampatra
1010
* @since 10/18/15
11-
* @time: 8:40 PM
1211
*/
1312
public class DuplicatesInArrayWithinKDistance {
1413

1514
/**
16-
* Finds duplicates in an unsorted array {@param a} which are
15+
* Finds duplicates in an unsorted array {@code a} which are
1716
* only k distance apart from each other.
1817
*
1918
* @param a

‎src/main/java/com/rampatra/arrays/MajorityElement.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
package com.rampatra.arrays;
22

3-
/**
4-
* Created by IntelliJ IDEA.
5-
*
6-
* @author rampatra
7-
* @since 5/20/15
8-
* @time: 2:36 PM
9-
*/
10-
11-
123
/**
134
* The algorithm for finding a possible candidate
145
* works in O(n) which is known as Moore’s Voting Algorithm.
156
* Basic idea of the algorithm is if we cancel out each
167
* occurrence of an element e with all the other elements
17-
* that are different from e then e will exist till end
8+
* that are different from e then e will exist until end
189
* if it is a majority element.
1910
* <p/>
2011
* Time Complexity: O(n)
2112
* Auxiliary Space : O(1)
13+
*
14+
* @author rampatra
15+
* @since 5/20/15
2216
*/
2317
public class MajorityElement {
2418

@@ -29,7 +23,7 @@ public class MajorityElement {
2923
* @param a
3024
* @return
3125
*/
32-
public static int findCandidate(inta[]) {
26+
public static int findCandidate(int[] a) {
3327
int candidate = a[0], count = 1;
3428
for (int i = 1; i < a.length; i++) {
3529
if (candidate == a[i]) {
@@ -45,7 +39,7 @@ public static int findCandidate(int a[]) {
4539
return candidate;
4640
}
4741

48-
public static void majorityElement(inta[]) {
42+
public static void majorityElement(int[] a) {
4943
int candidate = findCandidate(a),
5044
count = 0;
5145

@@ -65,4 +59,4 @@ public static void majorityElement(int a[]) {
6559
public static void main(String[] args) {
6660
majorityElement(new int[]{1, 6, 2, 2, 2, 1, 2, 2, 7, 2});
6761
}
68-
}
62+
}

‎src/main/java/com/rampatra/arrays/MajorityElementInSortedArray.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,27 @@
55
*
66
* @author rampatra
77
* @since 7/31/15
8-
* @time: 10:02 AM
98
*/
109
public class MajorityElementInSortedArray {
1110

1211
/**
13-
* Checks if {@param n} is a majority element in array {@param a}
12+
* Checks if {@code n} is a majority element in array {@code arr}
1413
* by performing a binary search.
1514
* <p/>
1615
* Time complexity: O(log n)
1716
*
18-
* @param a
17+
* @param arr
1918
* @param n
2019
* @return
2120
*/
22-
public static boolean isMajorityElement(int[] a, int n) {
23-
int l = a.length;
24-
int startIndex = getFirstIndexOf(a, n, 0, l - 1);
21+
public static boolean isMajorityElement(int[] arr, int n) {
22+
int l = arr.length;
23+
int startIndex = getFirstIndexOf(arr, n, 0, l - 1);
2524

2625
// element not found
2726
if (startIndex == -1) return false;
2827

29-
if (startIndex + l / 2 < l && a[startIndex + l / 2] == n) {
28+
if (startIndex + l / 2 < l && arr[startIndex + l / 2] == n) {
3029
return true;
3130
} else {
3231
return false;
@@ -35,15 +34,15 @@ public static boolean isMajorityElement(int[] a, int n) {
3534
}
3635

3736
/**
38-
* Returns the index of first occurrence of {@param n} in array {@param a}.
37+
* Returns the index of first occurrence of {@code n} in array {@code arr}.
3938
*
40-
* @param a
39+
* @param arr
4140
* @param low
4241
* @param high
4342
* @param n
4443
* @return
4544
*/
46-
public static int getFirstIndexOf(int[] a, int n, int low, int high) {
45+
public static int getFirstIndexOf(int[] arr, int n, int low, int high) {
4746
if (low <= high) {
4847
int mid = (low + high) / 2;
4948
/**
@@ -53,12 +52,12 @@ public static int getFirstIndexOf(int[] a, int n, int low, int high) {
5352
* (i) mid == 0 and a[mid] == n
5453
* (ii) n > a[mid-1] and a[mid] == n
5554
*/
56-
if (a[mid] == n && (mid == 0 || n > a[mid - 1])) {
55+
if (arr[mid] == n && (mid == 0 || n > arr[mid - 1])) {
5756
return mid;
58-
} else if (n <= a[mid]) {
59-
return getFirstIndexOf(a, n, low, mid - 1);
57+
} else if (n <= arr[mid]) {
58+
return getFirstIndexOf(arr, n, low, mid - 1);
6059
} else {
61-
return getFirstIndexOf(a, n, mid + 1, high);
60+
return getFirstIndexOf(arr, n, mid + 1, high);
6261
}
6362
}
6463
return -1;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /