Skip to content

Commit 132fbee

Browse files
committed
added bitmasking
1 parent ff66400 commit 132fbee

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.company.Lecture7;
2+
3+
import java.util.Scanner;
4+
5+
public class NthMagic {
6+
public static void main(String[] args) {
7+
Scanner s = new Scanner(System.in);
8+
int n = s.nextInt();
9+
10+
System.out.println(nthMagical(n));
11+
}
12+
13+
public static int nthMagical(int n) {
14+
int res = 0;
15+
int place = 1;
16+
while (n > 0){
17+
if ((n & 1 ) == 1){
18+
res = res + (int) Math.pow(5, place);
19+
}
20+
21+
n = n >> 1;
22+
place++;
23+
}
24+
25+
return res;
26+
}
27+
}

src/com/company/Lecture7/UniqueI.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.company.Lecture7;
2+
3+
public class UniqueI {
4+
public static void main(String[] args) {
5+
int[] nums = {1,2,5,3,1,5,2};
6+
7+
System.out.println(getSingle(nums));
8+
}
9+
10+
11+
private static int getSingle(int[] nums) {
12+
int res = 0;
13+
for (int num : nums) {
14+
res = res ^ num;
15+
}
16+
17+
return res;
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.company.Lecture7;
2+
3+
import java.util.Scanner;
4+
5+
public class UniqueII {
6+
public static void main(String[] args)
7+
{
8+
Scanner s = new Scanner(System.in);
9+
int n = s.nextInt();
10+
int[] arr = new int[n];
11+
for (int i = 0; i < n; i++) {
12+
arr[i] = s.nextInt();
13+
}
14+
get2NonRepeatingNos(arr);
15+
}
16+
17+
public static void get2NonRepeatingNos(int[] arr)
18+
{
19+
int Xor = arr[0];
20+
int res;
21+
int i;
22+
int x = 0;
23+
int y = 0;
24+
25+
for(i = 1; i < arr.length; i++) {
26+
Xor ^= arr[i];
27+
}
28+
29+
res = Xor;
30+
int pos = 0;
31+
while ((Xor & 1) != 1){
32+
pos++;
33+
Xor = Xor >> 1;
34+
}
35+
36+
int mask = (1 << pos);
37+
38+
for(i = 0; i < arr.length; i++) {
39+
if ((arr[i] & mask) == 1) {
40+
x = x ^ arr[i];
41+
}
42+
}
43+
44+
y = res ^ x;
45+
System.out.println(x + " " + y);
46+
}
47+
}

0 commit comments

Comments
 (0)