Skip to content

Commit cc00807

Browse files
author
Ram swaroop
committed
two non-repeating elements done
1 parent 0813cec commit cc00807

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

src/me/ramswaroop/bits/CountSetBits.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ static int countSetBits(int number) {
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-
* {@see http://stackoverflow.com/questions/12380478/bits-counting-algorithm-brian-kernighan-in-an-integer-time-complexity}
38-
* {@see http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive}
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
3939
*
4040
* @param n
4141
* @return

src/me/ramswaroop/bits/ReverseBits.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* @author: ramswaroop
77
* @date: 6/5/15
88
* @time: 4:26 PM
9-
* @link http://stackoverflow.com/questions/746171/best-algorithm-for-bit-reversal-from-msb-lsb-to-lsb-msb-in-c
10-
* @link http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
9+
* @link: http://stackoverflow.com/questions/746171/best-algorithm-for-bit-reversal-from-msb-lsb-to-lsb-msb-in-c
10+
* @link: http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
1111
*/
1212
public class ReverseBits {
1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package me.ramswaroop.bits;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
*
8+
* @author: ramswaroop
9+
* @date: 6/7/15
10+
* @time: 2:46 PM
11+
* @link: http://www.geeksforgeeks.org/find-two-non-repeating-elements-in-an-array-of-repeating-elements/
12+
*/
13+
public class TwoNonRepeatingElements {
14+
15+
/**
16+
* Finds the 2 non-repeating elements in an array of
17+
* repeating elements (all elements repeated twice
18+
* except 2 elements).
19+
*
20+
* @param a
21+
* @return
22+
*/
23+
public static int[] getTwoNonRepeatingElementsInArray(int a[]) {
24+
int xor = 0, setBit, x = 0, y = 0;
25+
for (int i = 0; i < a.length; i++) {
26+
xor ^= a[i]; // XOR all array elements
27+
}
28+
setBit = xor & ~(xor - 1); // get the rightmost set bit in XOR
29+
for (int i = 0; i < a.length; i++) {
30+
if ((a[i] & setBit) == 0) {
31+
x ^= a[i]; // one non-repeating element
32+
} else {
33+
y ^= a[i]; // other non-repeating element
34+
}
35+
}
36+
return new int[]{x, y};
37+
}
38+
39+
public static void main(String a[]) {
40+
System.out.println(Arrays.toString(getTwoNonRepeatingElementsInArray(new int[]{2, 3, 4, 2, 3, 4, 5, 6})));
41+
}
42+
}
43+
44+
/**
45+
* EXPLANATION:
46+
* Consider input arr[] = {2, 4, 7, 9, 2, 4}
47+
* 1) Get the XOR of all the elements.
48+
* xor = 2^4^7^9^2^4 = 14 (1110)
49+
* 2) Get a number which has only one set bit of the xor.
50+
* Since we can easily get the rightmost set bit, let us use it.
51+
* set_bit_no = xor & ~(xor-1) = (1110) & ~(1101) = 0010
52+
* Now set_bit_no will have only set as rightmost set bit of xor.
53+
* 3) Now divide the elements in two sets and do xor of
54+
* elements in each set, and we get the non-repeating
55+
* elements 7 and 9. Please see implementation for this
56+
* step.
57+
*/

src/me/ramswaroop/trees/BinaryTree.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,14 @@ public boolean isChildrenSum(BinaryNode<E> node) {
758758
E leftChildValue = (E) (node.left == null ? 0 : node.left.value);
759759
E rightChildValue = (E) (node.right == null ? 0 : node.right.value);
760760

761-
boolean left = isChildrenSum(node.left);
762-
boolean right = isChildrenSum(node.right);
763-
764761
if (!node.value.toString().equals(
765762
String.valueOf(Integer.parseInt(leftChildValue.toString()) +
766763
Integer.parseInt(rightChildValue.toString()))
767764
)) {
768765
return false;
769766
}
770767

771-
return left && right;
768+
return isChildrenSum(node.left) && isChildrenSum(node.right);
772769
}
773770

774771
/**

0 commit comments

Comments
 (0)