Skip to content

Commit 559c860

Browse files
author
Ram swaroop
committed
code refactoring
1 parent 5e3c4c6 commit 559c860

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

src/me/ramswaroop/bits/Addition.java

+40-41
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,25 @@
1010
public class Addition {
1111

1212
/**
13-
* Better solution.
13+
* Best method.
14+
* <p/>
15+
* -n = ~n + 1.
16+
* ~n = -(n+1). Therefore, n+1 = -(~n).
17+
* <p/>
18+
* Works for -ve numbers.
19+
* <p/>
20+
* Note: This method works only if the numbers
21+
* are stored in 2’s complement form.
22+
*
23+
* @param n
24+
* @return
25+
*/
26+
public static int add(int n) {
27+
return -(~n);
28+
}
29+
30+
/**
31+
* Good solution.
1432
* <p/>
1533
* Adds two numbers without using any
1634
* arithmetic operators.
@@ -39,7 +57,7 @@ public static int add(int x, int y) {
3957
* @param y
4058
* @return sum of {@param x} and {@param y}
4159
*/
42-
public static int add_V1(int x, int y) {
60+
public static int addNaive(int x, int y) {
4361
int carry = 0, sum = 0, c = 0, xLSB, yLSB;
4462
while (c < 32) {
4563
xLSB = x & 1;
@@ -57,25 +75,6 @@ public static int add_V1(int x, int y) {
5775
return sum;
5876
}
5977

60-
61-
/**
62-
* Best method.
63-
* <p/>
64-
* -n = ~n + 1.
65-
* ~n = -(n+1). Therefore, n+1 = -(~n).
66-
* <p/>
67-
* Works for -ve numbers.
68-
* <p/>
69-
* Note: This method works only if the numbers
70-
* are stored in 2’s complement form.
71-
*
72-
* @param n
73-
* @return
74-
*/
75-
public static int add1(int n) {
76-
return -(~n);
77-
}
78-
7978
/**
8079
* Idea is to flip all the bits of {@param n} till
8180
* rightmost 0 bit in {@param n}.
@@ -85,7 +84,7 @@ public static int add1(int n) {
8584
* @param n
8685
* @return
8786
*/
88-
public static int add1_V1(int n) {
87+
public static int addByFlip(int n) {
8988
int mask = 1;
9089
// flip all bits in n until rightmost 0 bit
9190
while ((n & mask) != 0) {
@@ -106,26 +105,26 @@ public static void main(String a[]) {
106105
System.out.println(add(456, 982348234)); // 982348690
107106
System.out.println(add(1, 0xffffffff)); // 0
108107
System.out.println("------");
109-
System.out.println(add_V1(0, 0)); //0
110-
System.out.println(add_V1(12, 12)); //24
111-
System.out.println(add_V1(12, 5)); //17
112-
System.out.println(add_V1(3, 5)); //8
113-
System.out.println(add_V1(8, 5)); //13
114-
System.out.println(add_V1(13, 256)); // 269
115-
System.out.println(add_V1(456, 982348234)); // 982348690
116-
System.out.println(add_V1(1, 0xffffffff)); // 0
108+
System.out.println(addNaive(0, 0)); //0
109+
System.out.println(addNaive(12, 12)); //24
110+
System.out.println(addNaive(12, 5)); //17
111+
System.out.println(addNaive(3, 5)); //8
112+
System.out.println(addNaive(8, 5)); //13
113+
System.out.println(addNaive(13, 256)); // 269
114+
System.out.println(addNaive(456, 982348234)); // 982348690
115+
System.out.println(addNaive(1, 0xffffffff)); // 0
117116
System.out.println("------");
118-
System.out.println(add1_V1(0));
119-
System.out.println(add1_V1(1));
120-
System.out.println(add1_V1(2));
121-
System.out.println(add1_V1(3));
122-
System.out.println(add1_V1(4));
123-
System.out.println(add1_V1(5));
124-
System.out.println(add1_V1(7));
117+
System.out.println(addByFlip(0));
118+
System.out.println(addByFlip(1));
119+
System.out.println(addByFlip(2));
120+
System.out.println(addByFlip(3));
121+
System.out.println(addByFlip(4));
122+
System.out.println(addByFlip(5));
123+
System.out.println(addByFlip(7));
125124
System.out.println("------");
126-
System.out.println(add1(1));
127-
System.out.println(add1(5));
128-
System.out.println(add1(-0));
129-
System.out.println(add1(-5));
125+
System.out.println(add(1));
126+
System.out.println(add(5));
127+
System.out.println(add(-0));
128+
System.out.println(add(-5));
130129
}
131130
}

src/me/ramswaroop/dynamicprogramming/MinimumJumpsToReachEnd.java

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public static int getMinimumJumpsToReachEndNaive(int[] a, int l, int h) {
4040
}
4141
return minJumps;
4242
}
43+
44+
// TODO dp approach
4345

4446
public static void main(String a[]) {
4547
int[] ar = new int[]{1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9};

0 commit comments

Comments
 (0)