Skip to content

Commit 78db92b

Browse files
author
imkrunalkanojiya
committed
[NEW] Added new binary algorithms
1 parent 1701201 commit 78db92b

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java1.algorithms.binary;
2+
3+
public class FindNthMagicNumber {
4+
// Method to find the nth magic number
5+
public static int findNthMagicNumber(int n) {
6+
int power = 1;
7+
int magicNumber = 0;
8+
9+
while (n != 0) {
10+
power *= 5; // Increase the power of 5
11+
12+
if ((n & 1) == 1) { // Check if the least significant bit of n is 1
13+
magicNumber += power; // Add the current power of 5 to the magic number
14+
}
15+
16+
n >>= 1; // Right shift n by 1 bit to process the next bit
17+
}
18+
19+
return magicNumber;
20+
}
21+
22+
public static void main(String[] args) {
23+
int n = 3;
24+
int nthMagicNumber = findNthMagicNumber(n);
25+
System.out.println("The " + n + "rd magic number is: " + nthMagicNumber);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package java1.algorithms.binary;
2+
3+
public class SubarraysWithBitwiseOR1 {
4+
// Method to solve the problem of counting subarrays with bitwise OR 1
5+
public static long solve(int A, int[] B) {
6+
// Initialize total count of subarrays
7+
long subArrCount = 0;
8+
9+
// Iterate over array B from the end to start
10+
for (int i = A - 1; i >= 0; i--) {
11+
// If current element is 1, calculate number of subarrays ending at index i
12+
if (B[i] == 1) {
13+
int count = A - i;
14+
subArrCount += count;
15+
}
16+
// Add count of subarrays where B[i] is 0 (same as previous iteration)
17+
subArrCount += B[i] == 0 ? 0 : 1; // If B[i] is 1, add 1; if B[i] is 0, add 0
18+
}
19+
20+
return subArrCount;
21+
}
22+
23+
public static void main(String[] args) {
24+
int A = 5;
25+
int[] B = {0, 1, 0, 1, 1};
26+
long result = solve(A, B);
27+
System.out.println("Total subarrays with bitwise OR 1: " + result);
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package java1.algorithms.binary;
2+
3+
public class ToggleIthBit {
4+
// Method to toggle the B-th bit of an integer A
5+
public static int toggleIthBit(int A, int B) {
6+
// Toggle the B-th bit of A directly
7+
A = A ^ (1 << B);
8+
return A;
9+
}
10+
11+
public static void main(String[] args) {
12+
int A = 5; // Binary: 101
13+
int B = 1; // Toggle the 1st bit
14+
int result = toggleIthBit(A, B);
15+
System.out.println("The result after toggling the " + B + "th bit of " + A + " is: " + result);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Function to find the nth magic number
2+
function findNthMagicNumber(n) {
3+
let power = 1; // Initialize the power of 5
4+
let magicNumber = 0; // Initialize the magic number
5+
6+
while (n !== 0) {
7+
power *= 5; // Increase the power of 5
8+
9+
// Check if the least significant bit of n is 1
10+
if (n & 1) {
11+
magicNumber += power; // Add the current power of 5 to the magic number
12+
}
13+
14+
n >>= 1; // Right shift n by 1 bit to process the next bit
15+
}
16+
17+
return magicNumber; // Return the final magic number
18+
}
19+
20+
// Example usage
21+
const n = 3;
22+
const nthMagicNumber = findNthMagicNumber(n);
23+
console.log(`The ${n}rd magic number is: ${nthMagicNumber}`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Function to solve the problem of counting subarrays with bitwise OR 1
2+
function subarraysWithBitwiseOR1(A, B) {
3+
// Initialize total count of subarrays
4+
let subArrCount = 0;
5+
6+
// Iterate over array B from the end to start
7+
for (let i = A - 1; i >= 0; i--) {
8+
// If current element is 1, calculate number of subarrays ending at index i
9+
if (B[i] === 1) {
10+
let count = A - i;
11+
subArrCount += count;
12+
}
13+
// Add count of subarrays where B[i] is 0 (same as previous iteration)
14+
subArrCount += count || 0; // If count is undefined (i.e., first iteration), add 0
15+
}
16+
17+
return subArrCount;
18+
}
19+
20+
// Example usage
21+
const A = 5;
22+
const B = [0, 1, 0, 1, 1];
23+
const result = subarraysWithBitwiseOR1(A, B);
24+
console.log(`Total subarrays with bitwise OR 1: ${result}`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Toggle i-th bit
2+
3+
// Function to toggle the B-th bit of an integer A
4+
function toogleIthBit(A, B) {
5+
if ((1 << B) ^ A) {
6+
A = A ^ (1 << B);
7+
}
8+
return A;
9+
}
10+
11+
// Example usage
12+
const A = 5; // Binary: 101
13+
const B = 1; // Toggle the 1st bit
14+
const result = toogleIthBit(A, B);
15+
console.log(`The result after toggling the ${B}th bit of ${A} is: ${result}`);

0 commit comments

Comments
 (0)