Skip to content
Closed
Empty file added 2
Empty file.
Empty file added clang-format
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.thealgorithms.bitmanipulation;

/**
* Count the total number of set bits in binary representations of all numbers from 1 to N.
*
* <p>This implementation uses bit manipulation and mathematical observation to
* efficiently calculate the total number of set bits in O(log N) time.
*
* <p>Example:
* N = 3 -> Binary(1):01, Binary(2):10, Binary(3):11 => Total Set Bits = 4
*
* <p>Reference: https://www.geeksforgeeks.org/count-total-set-bits-in-all-numbers-from-1-to-n/
*/
public final class CountTotalSetBits {

private CountTotalSetBits() {
// utility class
}

/**
* Returns the total count of set bits in binary representations of all numbers from 1 to n.
*
* @param n the upper limit of the range
* @return total number of set bits from 1 to n
*/
public static int countTotalSetBits(int n) {
if (n == 0) {
return 0;
}

int x = largestPowerOf2(n);
int bitsTill2x = x * (1 << (x - 1));
int msbBits = n - (1 << x) + 1;
int rest = n - (1 << x);

return bitsTill2x + msbBits + countTotalSetBits(rest);
}

/**
* Helper function to find the largest power of 2 less than or equal to n.
*/
private static int largestPowerOf2(int n) {
int x = 0;
while ((1 << x) <= n) {
x++;
}
return x - 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* TheAlgorithms (https://github.com/TheAlgorithms/Java)
* Author: Shewale41
* This file is licensed under the MIT License.
*/

package com.thealgorithms.bitmanipulation;

/**
* Check if a given integer is a power of four using bit manipulation.
*
* <p>A number is a power of four if:
* <ul>
* <li>It is positive.</li>
* <li>It has only one set bit in its binary representation.</li>
* <li>The only set bit is in an even position (checked with 0xAAAAAAAA mask).</li>
* </ul>
*
* <p>Example:
* 4 -> true (2^2)
* 16 -> true (4^2)
* 8 -> false (not power of 4)
*/
public final class PowerOfFour {

private PowerOfFour() {
// Utility class
}

/**
* Checks whether a given integer is a power of four.
*
* @param n number to check
* @return true if n is a power of four, false otherwise
*/
public static boolean isPowerOfFour(int n) {
return n > 0 && (n & (n - 1)) == 0 && (n & 0xAAAAAAAA) == 0;
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/ElGamalEncryption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* TheAlgorithms (https://github.com/TheAlgorithms/Java)
* Author: Shewale41
* This file is licensed under the MIT License.
*/

package com.thealgorithms.ciphers;

import java.math.BigInteger;
import java.security.SecureRandom;

/**
* Implementation of the ElGamal Encryption Algorithm.
*
* <p>This algorithm is based on the Diffie–Hellman key exchange and provides secure
* public-key encryption and decryption using modular arithmetic.
*
* <p>Reference:
* https://en.wikipedia.org/wiki/ElGamal_encryption
*/
public final class ElGamalEncryption {

private static final SecureRandom RANDOM = new SecureRandom();

/** Private constructor to prevent instantiation of utility class. */
private ElGamalEncryption() {
throw new UnsupportedOperationException("Utility class");
}

/**
* Demonstrates ElGamal encryption and decryption for a given message.
*
* @param message the message to encrypt
* @param bitLength the bit length for the prime number used
*/
public static void runElGamal(String message, int bitLength) {
BigInteger p = BigInteger.probablePrime(bitLength, RANDOM); // prime modulus
BigInteger g = new BigInteger("2"); // primitive root
BigInteger x = new BigInteger(bitLength - 2, RANDOM); // private key
BigInteger y = g.modPow(x, p); // public key

// Encryption
BigInteger k = new BigInteger(bitLength - 2, RANDOM);
BigInteger a = g.modPow(k, p);
BigInteger m = new BigInteger(message.getBytes());
BigInteger b = (y.modPow(k, p).multiply(m)).mod(p);

// Decryption
BigInteger aInverse = a.modPow(p.subtract(BigInteger.ONE).subtract(x), p);
BigInteger decrypted = (b.multiply(aInverse)).mod(p);

System.out.println("Prime (p): " + p);
System.out.println("Public Key (y): " + y);
System.out.println("Ciphertext: (" + a + ", " + b + ")");
System.out.println("Decrypted Message: " + new String(decrypted.toByteArray()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* TheAlgorithms (https://github.com/TheAlgorithms/Java)
* Author: Shewale41
* This file is licensed under the MIT License.
*/

package com.thealgorithms.datastructures.trees;

import java.util.ArrayList;
import java.util.List;

/**
* Threaded binary tree implementation that supports insertion and
* in-order traversal without recursion or stack by using threads.
*
* <p>In this implementation, a node's null left/right pointers are used
* to point to the in-order predecessor/successor respectively. Two flags
* indicate whether left/right pointers are real children or threads.
*
* @see <a href="https://en.wikipedia.org/wiki/Threaded_binary_tree">Wikipedia:
* Threaded binary tree</a>
*/
public final class ThreadedBinaryTree {

private Node root;

private static final class Node {
int value;
Node left;
Node right;
boolean leftIsThread;
boolean rightIsThread;

Node(int value) {
this.value = value;
this.left = null;
this.right = null;
this.leftIsThread = false;
this.rightIsThread = false;
}
}

public ThreadedBinaryTree() {
this.root = null;
}

/**
* Inserts a value into the threaded binary tree. Duplicate values are inserted
* to the right subtree (consistent deterministic rule).
*
* @param value the integer value to insert
*/
public void insert(int value) {
Node newNode = new Node(value);
if (root == null) {
root = newNode;
return;
}

Node current = root;
Node parent = null;

while (true) {
parent = current;
if (value < current.value) {
if (!current.leftIsThread && current.left != null) {
current = current.left;
} else {
break;
}
} else { // value >= current.value
if (!current.rightIsThread && current.right != null) {
current = current.right;
} else {
break;
}
}
}

if (value < parent.value) {
// attach newNode as left child
newNode.left = parent.left;
newNode.leftIsThread = parent.leftIsThread;
newNode.right = parent;
newNode.rightIsThread = true;

parent.left = newNode;
parent.leftIsThread = false;
} else {
// attach newNode as right child
newNode.right = parent.right;
newNode.rightIsThread = parent.rightIsThread;
newNode.left = parent;
newNode.leftIsThread = true;

parent.right = newNode;
parent.rightIsThread = false;
}
}

/**
* Returns the in-order traversal of the tree as a list of integers.
* Traversal is done without recursion or an explicit stack by following threads.
*
* @return list containing the in-order sequence of node values
*/
public List<Integer> inorderTraversal() {
List<Integer> result = new ArrayList<>();
Node current = root;
if (current == null) {
return result;
}

// Move to the leftmost node
while (current.left != null && !current.leftIsThread) {
current = current.left;
}

while (current != null) {
result.add(current.value);

// If right pointer is a thread, follow it
if (current.rightIsThread) {
current = current.right;
} else {
// Move to leftmost node in right subtree
current = current.right;
while (current != null && !current.leftIsThread && current.left != null) {
current = current.left;
}
}
}

return result;
}

/**
* Helper: checks whether the tree is empty.
*
* @return true if tree has no nodes
*/
public boolean isEmpty() {
return root == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thealgorithms.bitmanipulation;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* Test cases for {@link CountTotalSetBits}.
*/
public class CountTotalSetBitsTest {

@Test
void testSmallNumbers() {
assertEquals(4, CountTotalSetBits.countTotalSetBits(3)); // 1->1,2->1,3->2
assertEquals(5, CountTotalSetBits.countTotalSetBits(4)); // 1,2,3,4 -> total 5
}

@Test
void testPowerOfTwo() {
assertEquals(12, CountTotalSetBits.countTotalSetBits(7)); // from 1 to 7
}

@Test
void testLargerNumber() {
assertEquals(17, CountTotalSetBits.countTotalSetBits(10)); // verified manually
}

@Test
void testZero() {
assertEquals(0, CountTotalSetBits.countTotalSetBits(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* TheAlgorithms (https://github.com/TheAlgorithms/Java)
* Author: Shewale41
* This file is licensed under the MIT License.
*/

package com.thealgorithms.bitmanipulation;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link PowerOfFour}.
*/
public class PowerOfFourTest {

@Test
void testPowerOfFourTrueCases() {
assertTrue(PowerOfFour.isPowerOfFour(1));
assertTrue(PowerOfFour.isPowerOfFour(4));
assertTrue(PowerOfFour.isPowerOfFour(16));
assertTrue(PowerOfFour.isPowerOfFour(64));
assertTrue(PowerOfFour.isPowerOfFour(256));
}

@Test
void testPowerOfFourFalseCases() {
assertFalse(PowerOfFour.isPowerOfFour(0));
assertFalse(PowerOfFour.isPowerOfFour(2));
assertFalse(PowerOfFour.isPowerOfFour(8));
assertFalse(PowerOfFour.isPowerOfFour(12));
assertFalse(PowerOfFour.isPowerOfFour(-4));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* TheAlgorithms (https://github.com/TheAlgorithms/Java)
* Author: Shewale41
* This file is licensed under the MIT License.
*/

package com.thealgorithms.ciphers;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import org.junit.jupiter.api.Test;

/**
* Tests for {@link ElGamalEncryption}.
*/
public class ElGamalEncryptionTest {

@Test
void testEncryptionDecryption() {
String message = "Hello";
assertDoesNotThrow(() -> ElGamalEncryption.runElGamal(message, 32));
}

@Test
void testWithDifferentBitLengths() {
assertDoesNotThrow(() -> ElGamalEncryption.runElGamal("Test", 16));
assertDoesNotThrow(() -> ElGamalEncryption.runElGamal("Secure", 64));
}
}
Loading
Loading