Skip to content

Commit 468f490

Browse files
committed
Some refactorings
1 parent 849fb16 commit 468f490

File tree

62 files changed

+147
-140
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+147
-140
lines changed

src/main/java/com/hackerrank/algorithms/strings/MakingAnagrams.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MakingAnagrams {
1818
* @return
1919
*/
2020
public static int makeAnagrams(String a, String b) {
21-
21+
2222
int i = 0, j = 0, c = 0;
2323
char[] s1 = a.toCharArray();
2424
char[] s2 = b.toCharArray();

src/main/java/com/hackerrank/interviewpreparation/arrays/DS2DArrayProblem.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public class DS2DArrayProblem {
1616
private static int hourglassSum(int[][] arr) {
1717
int maxSum = Integer.MIN_VALUE;
1818
int hourglassSum;
19-
for (int i=0;i<arr.length-2;i++) {
20-
for(int j=0;j<arr[0].length-2;j++) {
21-
hourglassSum = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] +
22-
arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
19+
for (int i = 0; i < arr.length - 2; i++) {
20+
for (int j = 0; j < arr[0].length - 2; j++) {
21+
hourglassSum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] +
22+
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
2323
if (hourglassSum > maxSum) {
2424
maxSum = hourglassSum;
2525
}

src/main/java/com/hackerrank/projecteuler/MultiplesOf3and5.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class MultiplesOf3and5 {
1313

1414
public static void main(String a[]) {
1515
Scanner in = new Scanner(System.in);
16-
16+
1717
int t = Integer.parseInt(in.nextLine());
18-
18+
1919
}
2020
}

src/main/java/com/rampatra/arrays/CelebrityProblem.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes,
1414
* (s)he doesn’t know anyone in the party. We can only ask questions like “does A know B? “. Find the stranger
1515
* (celebrity) in minimum number of questions.
16-
*
16+
* <p>
1717
* TODO: Not tested.
1818
*/
1919
public class CelebrityProblem {
@@ -44,7 +44,7 @@ public static boolean haveAcquaintance(int[][] peoples, int a, int b) {
4444
* - Push the remained person onto stack.
4545
* - Repeat step 2 and 3 until only one person remains in the stack.
4646
* - Check the remained person in stack does not have acquaintance with anyone else.
47-
*
47+
*
4848
* @param peoples
4949
* @return
5050
*/

src/main/java/com/rampatra/arrays/EqualProbabilityRandomNoGenerator.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Given a random number generator f(n) which generates a random number
88
* from {@code 0} (inclusive) to {@code n} (exclusive), design a method
9-
* which uses f(n) to generate non repeating random numbers from
9+
* which uses f(n) to generate non repeating random numbers from
1010
* {@code 0} (inclusive) to {@code n} (exclusive)?
1111
*
1212
* @author: ramswaroop
@@ -19,9 +19,9 @@ public class EqualProbabilityRandomNoGenerator {
1919
static int size;
2020

2121
/**
22-
* The algorithm is to create a bucket of numbers and then to keep on
22+
* The algorithm is to create a bucket of numbers and then to keep on
2323
* removing the elements from the bucket which are returned.
24-
*
24+
*
2525
* @return
2626
*/
2727
public static int getRandom() {
@@ -37,12 +37,12 @@ public static int f(int n) {
3737

3838
public static void main(String a[]) {
3939
Scanner in = new Scanner(System.in);
40-
40+
4141
System.out.println("How many random numbers you would like to generate?");
4242
size = in.nextInt();
43-
43+
4444
bucket = new int[size];
45-
45+
4646
for (int i = 0; i < bucket.length; i++) {
4747
bucket[i] = i;
4848
}

src/main/java/com/rampatra/arrays/IntersectionAndUnionOf2SortedArrays.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
public class IntersectionAndUnionOf2SortedArrays {
1313

1414
/**
15-
* Returns a 2-D array consisting of intersection and union of
15+
* Returns a 2-D array consisting of intersection and union of
1616
* two sorted arrays {@param a} and {@param b} respectively.
17-
*
17+
*
1818
* @param a
1919
* @param b
2020
* @return
@@ -24,7 +24,7 @@ public static int[][] getIntersectionAndUnionOf2SortedArrays(int[] a, int[] b) {
2424
int[] intersection = new int[length], union = new int[length];
2525

2626
for (int i = 0, j = 0; i < a.length || j < b.length; ) {
27-
27+
2828
// if either of the arrays runs out first
2929
if (i == a.length) {
3030
union[y++] = b[j++];

src/main/java/com/rampatra/arrays/InversionsInArray.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static int getInversionCountNaiveApproach(int[] a) {
4040

4141
/**
4242
* Optimized approach.
43-
*
43+
* <p>
4444
* Explanation: In merge() if a[i] > b[j] then all elements in array a starting
4545
* from i are greater than b[j] which equals to the number of inversions for
4646
* the two sub-arrays.

src/main/java/com/rampatra/arrays/KLargestElements.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ public class KLargestElements {
1515

1616
/**
1717
* Finds {@param k} largest elements in array {@param a}.
18-
*
18+
* <p>
1919
* Algorithm:
2020
* 1) Build a Min Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. This takes O(k) time.
21-
*
21+
* <p>
2222
* 2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with root of MH.
2323
* ……a) If the element is greater than the root then make it root and call buildHeap for MH
2424
* ……b) Else ignore it.
2525
* This step takes (n-k) * O(k) time.
26-
*
26+
* <p>
2727
* 3) Finally, MH has k largest elements and root of the MH is the kth largest element.
28-
*
28+
* <p>
2929
* Therefore, the total time complexity of the above algorithm is: O(k) + (n-k) * O(k).
3030
*
3131
* @param a

src/main/java/com/rampatra/arrays/KthLargestElement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static int getKthLargestElement(int[] a, int k) {
4444
maxHeap.buildMaxHeap();
4545
while (true) {
4646
if (k == 1) break;
47-
47+
4848
maxHeap.extractMax();
4949
k--;
5050
}

src/main/java/com/rampatra/arrays/LargestProductContiguousSubArray.java

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
public class LargestProductContiguousSubArray {
1111

1212
/**
13-
*
1413
* @param a
1514
* @return
1615
*/

src/main/java/com/rampatra/arrays/MaxInAllSubArrays.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public static int[] maxInAllSubArraysOfSizeKNaive(int[] a, int k) {
4646
/**
4747
* Finds the maximum element in each and every sub-array
4848
* in {@param a} of size {@param k}.
49-
*
49+
* <p>
5050
* Time complexity: O(n)
5151
* Auxiliary Space: O(k)
52-
*
52+
*
5353
* @param a
5454
* @param k
5555
* @return

src/main/java/com/rampatra/arrays/MaximumSumNonAdjacentSubSequence.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class MaximumSumNonAdjacentSubSequence {
1717
* Example:
1818
* 1) 3 2 7 10 should return 13 (sum of 3 and 10)
1919
* 2) 3 2 5 10 7 should return 15 (sum of 3, 5 and 7).
20-
*
20+
* <p>
2121
* Here we maintain 2 variables incl and excl which is max sum till now (satisfying the constraint)
2222
* including the current element and excluding the current element respectively.
2323
*
@@ -26,7 +26,7 @@ public class MaximumSumNonAdjacentSubSequence {
2626
*/
2727
public static int maximumSumNonAdjacentSubSequence(int[] a) {
2828
int incl = a[0], excl = 0, prevIncl = incl; // incl is max sum including the current element
29-
// and excl is max sum excluding the current element
29+
// and excl is max sum excluding the current element
3030
for (int i = 1; i < a.length; i++) {
3131
incl = excl + a[i]; // because we have to exclude the previous element if we consider the current element
3232
excl = Math.max(prevIncl, excl); // we are excluding the current element so we can consider the previous element or dont

src/main/java/com/rampatra/arrays/NextLargerNumber.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static int findNextLargerNumber(Integer n) {
4444

4545
// digits are already in descending order, so return
4646
if (i <= 0) return -1;
47-
47+
4848
// find index of smallest no. greater than a[i-1]
4949
minIndex = i;
5050
int j = len - 1;

src/main/java/com/rampatra/arrays/NumberOccurringOddTimes.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Given an array of positive integers. All numbers occur
1515
* even number of times except one number which occurs odd
1616
* number of times. Find the number in O(n) time & constant space.
17-
*
17+
* <p>
1818
* See {@link TwoNonRepeatingElements} for a more
1919
* complex problem which is solved in a similar approach.
2020
*/

src/main/java/com/rampatra/arrays/ReservoirSampling.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class ReservoirSampling {
2222
* a) Generate a random number from 0 to i where i is index of current item in
2323
* stream[]. Let the generated random number is j.
2424
* b) If j is in range 0 to k-1, replace reservoir[j] with arr[i].
25-
*
25+
* <p>
2626
* In the above procedure, we are computing random number for each of the indexes greater than k
2727
* thereby giving all items an equal probability.
28-
*
28+
* <p>
2929
* NOTE: When {@param k} is small enough we can use a simpler method as follows:
3030
* Create an array reservoir[] of maximum size k. One by one randomly select an
3131
* item from stream[0..n-1]. If the selected item is not previously selected, then

src/main/java/com/rampatra/arrays/Segregate0s1sAnd2s.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
public class Segregate0s1sAnd2s {
1313

1414
/**
15-
* Segregates an array {@param a} consisting of only 0s, 1s and 2s. Based on
15+
* Segregates an array {@param a} consisting of only 0s, 1s and 2s. Based on
1616
* Dutch National Flag (DNF) problem {@see: http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/}.
17-
*
17+
*
1818
* @param a
1919
*/
2020
public static void segregate0s1sAnd2s(int[] a) {
2121
// assume low points to 0 and mid to 1 and high to 2
2222
int low = 0, mid = 0, high = a.length - 1;
23-
23+
2424
while (mid <= high) {
2525
switch (a[mid]) {
2626
case 0: // mid points to 0 but it should point to 1 so swap it with low

src/main/java/com/rampatra/arrays/SortedSubSequence.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SortedSubSequence {
1313
* Finds 3 elements such that a[i] < a[j] < a[k] and i < j < k in O(n) time
1414
* in an array of n integers. If there are multiple such triplets, then prints any
1515
* one of them.
16-
*
16+
* <p>
1717
* Algorithm:
1818
* 1) Create an auxiliary array smaller[0..n-1]. smaller[i] should store the index of a number which is smaller than arr[i] and is on left side of arr[i]. smaller[i] should contain -1 if there is no such element.
1919
* 2) Create another auxiliary array greater[0..n-1]. greater[i] should store the index of a number which is greater than arr[i] and is on right side of arr[i]. greater[i] should contain -1 if there is no such element.

src/main/java/com/rampatra/arrays/SubsetOfArray.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class SubsetOfArray {
1717
* Explanation: The below method uses sorting + merge method of merge sort. Time
1818
* complexity is O(mlogm + nlogn) where m and n are lengths of array a and b resp.
1919
* You could also have used sorting + binary search but this fails when array
20-
* {@param b} has repeating elements for example, a={1,4,2} and b={1,4,4,2}. Time
20+
* {@param b} has repeating elements for example, a={1,4,2} and b={1,4,4,2}. Time
2121
* complexity would be O(mlogm + nlogm).
2222
*
2323
* @param a

src/main/java/com/rampatra/arrays/TwoRepeatingElements.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public static int[] getTwoRepeatingElements(int[] a) {
6969
* Once we encounter a element lets say 2 then we make the element in 2nd index -ve just
7070
* to mark that we have encountered 2. When we encounter 2 again and see that 2nd index
7171
* is already -ve we conclude that 2 is repeated.
72-
*
72+
* <p>
7373
* Similar to {@link DuplicatesInArray#findDuplicatesInArray(int[])}.
74-
*
74+
*
7575
* @param a
7676
* @return
7777
*/

src/main/java/com/rampatra/arrays/TwoStacksInOneArray.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* Created by IntelliJ IDEA.
7-
*
7+
* <p>
88
* Implement two stacks using a single array with efficient use of space.
99
* We could do this by dividing the array into two equal halves or storing stack
1010
* elements alternatively in the array but that wouldn't utilize the space fully.

src/main/java/com/rampatra/arrays/UnsortedSubArray.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static int[] getUnsortedSubArray(int[] a) {
4040
int[] unsortedArray;
4141

4242
// 1(a)
43-
for (int i = 0; i < a.length-1; i++) {
43+
for (int i = 0; i < a.length - 1; i++) {
4444
if (a[i] > a[i + 1]) {
4545
start = i;
4646
break;

src/main/java/com/rampatra/arrays/sorting/BubbleSort.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public class BubbleSort {
1515
* of the array until it is sorted. In doing so, the smaller items
1616
* slowly "bubble" up to the beginning of the list and in each inner
1717
* iteration the largest element is sorted. Ergo, the inner loop runs
18-
* until {@code length - i - 1} times. To learn more:
19-
* {@see https://youtu.be/6Gv8vg0kcHc}
18+
* until {@code length - i - 1} times. Time complexity: O(n^2). Space
19+
* complexity: O(1), in place. To learn more: {@see https://youtu.be/6Gv8vg0kcHc}
2020
*
21-
* @param ar
21+
* @param ar to be sorted
2222
*/
2323
private static void bubbleSort(int[] ar) {
2424
for (int i = 0; i < ar.length - 1; i++) {

src/main/java/com/rampatra/arrays/sorting/SelectionSort.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class SelectionSort {
1919
* element is chosen and placed at the appropriate position and then again the
2020
* logic is applied on rest of the elements till the entire array is sorted.
2121
* <p/>
22-
* Time complexity: O(n*n) for all cases.
22+
* Time complexity: O(n^2) for all cases. Space complexity: O(1).
2323
* <p/>
2424
* NOTE: Advantage of this sort is that it requires minimum number of memory writes
2525
* like Cycle sort.

src/main/java/com/rampatra/bits/CountSetBits.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ static int countSetBits(int number) {
3030

3131
/**
3232
* Optimized version. Works for -ve numbers as well.
33-
*
33+
* <p>
3434
* Uses BRIAN KERNIGAN'S bit counting. Acc. to this, the right most/least significant set bit is unset
3535
* in each iteration. The time complexity is proportional to the number of bits set.
3636
*
37-
* @link http://stackoverflow.com/questions/12380478/bits-counting-algorithm-brian-kernighan-in-an-integer-time-complexity
38-
* @link http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive
39-
*
4037
* @param n
4138
* @return
39+
* @link http://stackoverflow.com/questions/12380478/bits-counting-algorithm-brian-kernighan-in-an-integer-time-complexity
40+
* @link http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive
4241
*/
4342
static int countSetBits(long n) {
4443
int count = 0;
@@ -62,8 +61,6 @@ public static void main(String[] a) {
6261
}
6362

6463
/**
65-
*
6664
* Learn more:
6765
* http://javarevisited.blogspot.in/2014/06/how-to-count-number-of-set-bits-or-1s.html
68-
*
6966
*/

src/main/java/com/rampatra/bits/CountSetBitsFromMinusNtoN.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class CountSetBitsFromMinusNtoN {
1111

1212
/**
1313
* Explanation:
14-
*
14+
* <p>
1515
* -3: 101
1616
* -2: 110
1717
* -1: 111
1818
* 0: 000
1919
* 1: 001
2020
* 2: 010
2121
* 3: 110
22-
*
22+
* <p>
2323
* If you fold the above representation between -1 and 0, the total no. of set bits from -3 to 2
2424
* will be equal to the total no. of bits in nos. from -3 to 2.
2525
*

src/main/java/com/rampatra/bits/ElementOccurringOnce.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public static void main(String a[]) {
4848
* 6 = 0110
4949
* -------------
5050
* res = 0010
51-
*
51+
* <p>
5252
* 1st bit of res = (0+0+0+0+0+0+0) % 3 = 0
5353
* 2nd bit of res = (0+0+1+1+1+0+1) % 3 = 1
5454
* 3rd bit of res = (1+1+1+1+0+1+1) % 3 = 0
5555
* 4th bit of res = (1+1+0+0+0+1+0) % 3 = 0
56-
*
56+
* <p>
5757
* NOTE: Sum of bits at a particular position will not be divisible
5858
* by 3 if the no. occurring once has a set bit at that position.
5959
*/

src/main/java/com/rampatra/bits/FlippingBits.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public static void main(String a[]) {
3939

4040
/**
4141
* EXPLANATION:
42-
*
42+
* <p>
4343
* For input: 5
44-
*
44+
* <p>
4545
* Binary: 000.....101
4646
* Inverted: 111.....010 (which is the 2's compliment of -6)
47-
*
47+
* <p>
4848
* Therefore, result is -6.
4949
*/

src/main/java/com/rampatra/bits/Modulo.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static void main(String a[]) {
3333

3434
/**
3535
* Consider example, for 18 % 8
36-
*
36+
* <p>
3737
* 18 = 10010
38-
* 7 = 00111 (8 = 2 ^ 3, therefore mask has to have three 1's)
39-
* 2 = 00010 (remainder = 18 & (8-1))
38+
* 7 = 00111 (8 = 2 ^ 3, therefore mask has to have three 1's)
39+
* 2 = 00010 (remainder = 18 & (8-1))
4040
*/

0 commit comments

Comments
 (0)