Skip to content

Commit 1db94ed

Browse files
committed
Bit insert done
1 parent 27bad3d commit 1db94ed

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.ctci.bitmanipulation;
2+
3+
/**
4+
* @author rampatra
5+
* @since 2019-03-14
6+
*/
7+
public class Insertion {
8+
9+
/**
10+
* You are given two 32-bit numbers, N and M, and two bit positions, startIndex and endIndex. Write a method to
11+
* insert M into N such that M starts at bit startIndex and ends at bit endIndex. You can assume that the bits
12+
* startIndex through endIndex have enough space to fit all of M. That is, if M = 10011, you can assume that there
13+
* are at least 5 bits between j and i. You would not, for example, have startIndex = 3 and endIndex = 2, because
14+
* M could not fully fit between bit 3 and bit 2.
15+
* <p>
16+
* EXAMPLE
17+
* Input: N = 10000000000, M = 10011, startIndex = 6, endIndex = 2
18+
* Output: 10001001100
19+
*
20+
* @param n
21+
* @param m
22+
* @param startIndex
23+
* @param endIndex
24+
* @return
25+
*/
26+
private static int insertMIntoN(int n, int m, int startIndex, int endIndex) {
27+
// create a mask with only one bit set
28+
int mask = 1;
29+
// shift the set bit so that it starts with endIndex
30+
mask <<= endIndex;
31+
32+
// unset the bits in 'n' from endIndex to startIndex
33+
for (int i = endIndex; i <= startIndex; i++) {
34+
n = n & ~mask; // ~mask will make the bit at ith index 0 but the rest of the bits will be 1
35+
mask <<= 1;
36+
}
37+
38+
// shift 'm' so that it lines up with bits from startIndex to endIndex
39+
m <<= endIndex;
40+
41+
// finally, return the xor of both as we know that 0 ^ a = a
42+
return n ^ m;
43+
}
44+
45+
public static void main(String[] args) {
46+
System.out.println(Integer.toBinaryString(insertMIntoN(Integer.parseInt("10000000000", 2), Integer.parseInt("10011", 2), 6, 2)));
47+
System.out.println(Integer.toBinaryString(insertMIntoN(Integer.parseInt("10110110111", 2), Integer.parseInt("11101", 2), 7, 3)));
48+
}
49+
}

Diff for: src/main/java/com/ctci/treesandgraphs/LeastCommonAncestor.java renamed to src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* @author rampatra
99
* @since 2019-02-24
1010
*/
11-
public class LeastCommonAncestor {
11+
public class FirstCommonAncestor {
1212

1313
/**
14-
* We recurse through the entire tree with a function called findLCA(TreeNode root, TreeNode TreeNode a, TreeNode b).
14+
* We recurse through the entire tree with a function called findFCA(TreeNode root, TreeNode TreeNode a, TreeNode b).
1515
* This function returns values as follows:
1616
* - Returns p,if root's subtree includes p (and not q).
1717
* - Returns q, if root's subtree includes q (and not p).
@@ -23,20 +23,20 @@ public class LeastCommonAncestor {
2323
* @param b
2424
* @return the least common ancestor node
2525
*/
26-
private static TreeNode findLCA(TreeNode root, TreeNode a, TreeNode b) {
26+
private static TreeNode findFCA(TreeNode root, TreeNode a, TreeNode b) {
2727
if (root == null) { // validation
2828
return null;
2929
}
3030
if (root == a && root == b) { // optimization
3131
return root;
3232
}
3333

34-
TreeNode left = findLCA(root.left, a, b);
34+
TreeNode left = findFCA(root.left, a, b);
3535
if (left != null && left != a && left != b) {
3636
return left;
3737
}
3838

39-
TreeNode right = findLCA(root.right, a, b);
39+
TreeNode right = findFCA(root.right, a, b);
4040
if (right != null && right != a && right != b) {
4141
return right;
4242
}
@@ -85,14 +85,14 @@ public static void main(String[] args) {
8585
treeRoot.right.right = new TreeNode(9);
8686
treeRoot.right.left.right = new TreeNode(7);
8787

88-
System.out.println("LCA of 0 and 7 is: " + findLCA(treeRoot, treeRoot.left.left.left, treeRoot.right.left.right).val);
89-
System.out.println("LCA of 0 and 9 is: " + findLCA(treeRoot, treeRoot.left.left.left, treeRoot.right.right).val);
90-
System.out.println("LCA of 0 and 1 is: " + findLCA(treeRoot, treeRoot.left.left.left, treeRoot.left.left).val);
91-
System.out.println("LCA of 1 and 2 is: " + findLCA(treeRoot, treeRoot.left.left, treeRoot.right.left).val);
92-
System.out.println("LCA of 1 and 7 is: " + findLCA(treeRoot, treeRoot.left.left, treeRoot.right.left.right).val);
93-
System.out.println("LCA of 4 and 7 is: " + findLCA(treeRoot, treeRoot, treeRoot.right.left.right).val);
94-
System.out.println("LCA of 5 and 2 is: " + findLCA(treeRoot, treeRoot.left, treeRoot.right.left).val);
95-
System.out.println("LCA of 7 and 9 is: " + findLCA(treeRoot, treeRoot.right.left.right, treeRoot.right.right).val);
96-
System.out.println("LCA of 7 and 10 is: " + findLCA(treeRoot, treeRoot.right.left.right, new TreeNode(10)).val); // this use case does not work with the above algorithm
88+
System.out.println("FCA of 0 and 7 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.left.right).val);
89+
System.out.println("FCA of 0 and 9 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.right).val);
90+
System.out.println("FCA of 0 and 1 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.left.left).val);
91+
System.out.println("FCA of 1 and 2 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left).val);
92+
System.out.println("FCA of 1 and 7 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left.right).val);
93+
System.out.println("FCA of 4 and 7 is: " + findFCA(treeRoot, treeRoot, treeRoot.right.left.right).val);
94+
System.out.println("FCA of 5 and 2 is: " + findFCA(treeRoot, treeRoot.left, treeRoot.right.left).val);
95+
System.out.println("FCA of 7 and 9 is: " + findFCA(treeRoot, treeRoot.right.left.right, treeRoot.right.right).val);
96+
System.out.println("FCA of 7 and 10 is: " + findFCA(treeRoot, treeRoot.right.left.right, new TreeNode(10)).val); // this use case does not work with the above algorithm
9797
}
9898
}

Diff for: src/main/java/com/ctci/treesandgraphs/LeastCommonAncestorWithParentAccess.java renamed to src/main/java/com/ctci/treesandgraphs/FirstCommonAncestorWithParentAccess.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author rampatra
99
* @since 2019-02-23
1010
*/
11-
public class LeastCommonAncestorWithParentAccess {
11+
public class FirstCommonAncestorWithParentAccess {
1212

1313
/**
1414
* This is a simple approach where we start with two references, one pointing to {@code node a} and another
@@ -103,13 +103,13 @@ public static void main(String[] args) {
103103
treeRoot.right.left.right = new TreeNode(7);
104104
treeRoot.right.left.right.parent = treeRoot.right.left;
105105

106-
System.out.println("LCA of 0 and 7 is: " + findLCA(treeRoot.left.left.left, treeRoot.right.left.right).val);
107-
System.out.println("LCA of 0 and 9 is: " + findLCA(treeRoot.left.left.left, treeRoot.right.right).val);
108-
System.out.println("LCA of 0 and 1 is: " + findLCA(treeRoot.left.left.left, treeRoot.left.left).val);
109-
System.out.println("LCA of 1 and 2 is: " + findLCA(treeRoot.left.left, treeRoot.right.left).val);
110-
System.out.println("LCA of 1 and 7 is: " + findLCA(treeRoot.left.left, treeRoot.right.left.right).val);
111-
System.out.println("LCA of 4 and 7 is: " + findLCA(treeRoot, treeRoot.right.left.right).val);
112-
System.out.println("LCA of 5 and 2 is: " + findLCA(treeRoot.left, treeRoot.right.left).val);
113-
System.out.println("LCA of 7 and 9 is: " + findLCA(treeRoot.right.left.right, treeRoot.right.right).val);
106+
System.out.println("FCA of 0 and 7 is: " + findLCA(treeRoot.left.left.left, treeRoot.right.left.right).val);
107+
System.out.println("FCA of 0 and 9 is: " + findLCA(treeRoot.left.left.left, treeRoot.right.right).val);
108+
System.out.println("FCA of 0 and 1 is: " + findLCA(treeRoot.left.left.left, treeRoot.left.left).val);
109+
System.out.println("FCA of 1 and 2 is: " + findLCA(treeRoot.left.left, treeRoot.right.left).val);
110+
System.out.println("FCA of 1 and 7 is: " + findLCA(treeRoot.left.left, treeRoot.right.left.right).val);
111+
System.out.println("FCA of 4 and 7 is: " + findLCA(treeRoot, treeRoot.right.left.right).val);
112+
System.out.println("FCA of 5 and 2 is: " + findLCA(treeRoot.left, treeRoot.right.left).val);
113+
System.out.println("FCA of 7 and 9 is: " + findLCA(treeRoot.right.left.right, treeRoot.right.right).val);
114114
}
115115
}

Diff for: src/main/java/com/rampatra/threads/SimpleDeadlock.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void main(String[] args) {
3838
Object obj2 = new Object();
3939

4040
Thread thread1 = new Thread(new SimpleDeadlock(obj1, obj2));
41-
Thread thread2 = new Thread(new SimpleDeadlock(obj2, obj1));
41+
Thread thread2 = new Thread(new SimpleDeadlock(obj2, obj1)); // note here that the object order is different
4242

4343
thread1.start();
4444
thread2.start();

0 commit comments

Comments
 (0)