Skip to content

Commit 96113a4

Browse files
authored
Merge branch 'master' into reverse_stack_improve
2 parents da695a6 + b0cef5b commit 96113a4

File tree

11 files changed

+364
-66
lines changed

11 files changed

+364
-66
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
* [IntegerToEnglish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java)
101101
* [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java)
102102
* [IPConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IPConverter.java)
103+
* [IPv6Converter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IPv6Converter.java)
103104
* [MorseCodeConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/MorseCodeConverter.java)
104105
* [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java)
105106
* [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java)
@@ -784,6 +785,7 @@
784785
* [IntegerToEnglishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java)
785786
* [IntegerToRomanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java)
786787
* [IPConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IPConverterTest.java)
788+
* [IPv6ConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java)
787789
* [MorseCodeConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/MorseCodeConverterTest.java)
788790
* [OctalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java)
789791
* [OctalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java)

src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Is number power of 2
4+
* Utility class for checking if a number is a power of two.
5+
* A power of two is a number that can be expressed as 2^n where n is a non-negative integer.
6+
* This class provides a method to determine if a given integer is a power of two using bit manipulation.
7+
*
58
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
69
*/
7-
810
public final class IsPowerTwo {
911
private IsPowerTwo() {
1012
}
13+
14+
/**
15+
* Checks if the given integer is a power of two.
16+
*
17+
* A number is considered a power of two if it is greater than zero and
18+
* has exactly one '1' bit in its binary representation. This method
19+
* uses the property that for any power of two (n), the expression
20+
* (n & (n - 1)) will be zero.
21+
*
22+
* @param number the integer to check
23+
* @return true if the number is a power of two, false otherwise
24+
*/
1125
public static boolean isPowerTwo(int number) {
1226
if (number <= 0) {
1327
return false;

src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find Non Repeating Number
4+
* A utility class to find the non-repeating number in an array where every other number repeats.
5+
* This class contains a method to identify the single unique number using bit manipulation.
6+
*
7+
* The solution leverages the properties of the XOR operation, which states that:
8+
* - x ^ x = 0 for any integer x (a number XORed with itself is zero)
9+
* - x ^ 0 = x for any integer x (a number XORed with zero is the number itself)
10+
*
11+
* Using these properties, we can find the non-repeating number in linear time with constant space.
12+
*
13+
* Example:
14+
* Given the input array [2, 3, 5, 2, 3], the output will be 5 since it does not repeat.
15+
*
516
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
617
*/
7-
818
public final class NonRepeatingNumberFinder {
919
private NonRepeatingNumberFinder() {
1020
}
1121

22+
/**
23+
* Finds the non-repeating number in the given array.
24+
*
25+
* @param arr an array of integers where every number except one appears twice
26+
* @return the integer that appears only once in the array or 0 if the array is empty
27+
*/
1228
public static int findNonRepeatingNumber(int[] arr) {
1329
int result = 0;
1430
for (int num : arr) {
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find the Number Appearing Odd Times in an array
4+
* This class provides a method to find the element that appears an
5+
* odd number of times in an array. All other elements in the array
6+
* must appear an even number of times for the logic to work.
7+
*
8+
* The solution uses the XOR operation, which has the following properties:
9+
* - a ^ a = 0 (XOR-ing the same numbers cancels them out)
10+
* - a ^ 0 = a
11+
* - XOR is commutative and associative.
12+
*
13+
* Time Complexity: O(n), where n is the size of the array.
14+
* Space Complexity: O(1), as no extra space is used.
15+
*
16+
* Usage Example:
17+
* int result = NumberAppearingOddTimes.findOddOccurrence(new int[]{1, 2, 1, 2, 3});
18+
* // result will be 3
19+
*
520
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
621
*/
722

823
public final class NumberAppearingOddTimes {
924
private NumberAppearingOddTimes() {
1025
}
26+
27+
/**
28+
* Finds the element in the array that appears an odd number of times.
29+
*
30+
* @param arr the input array containing integers, where all elements
31+
* except one appear an even number of times.
32+
* @return the integer that appears an odd number of times.
33+
*/
1134
public static int findOddOccurrence(int[] arr) {
1235
int result = 0;
13-
14-
// XOR all elements in the array
1536
for (int num : arr) {
1637
result ^= num;
1738
}
18-
1939
return result;
2040
}
2141
}

src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Numbers Different Signs
4+
* This class provides a method to determine whether two integers have
5+
* different signs. It utilizes the XOR operation on the two numbers:
6+
*
7+
* - If two numbers have different signs, their most significant bits
8+
* (sign bits) will differ, resulting in a negative XOR result.
9+
* - If two numbers have the same sign, the XOR result will be non-negative.
10+
*
11+
* Time Complexity: O(1) - Constant time operation.
12+
* Space Complexity: O(1) - No extra space used.
13+
*
514
* @author Bama Charan Chhandogi
615
*/
7-
816
public final class NumbersDifferentSigns {
917
private NumbersDifferentSigns() {
1018
}
1119

20+
/**
21+
* Determines if two integers have different signs using bitwise XOR.
22+
*
23+
* @param num1 the first integer
24+
* @param num2 the second integer
25+
* @return true if the two numbers have different signs, false otherwise
26+
*/
1227
public static boolean differentSigns(int num1, int num2) {
1328
return (num1 ^ num2) < 0;
1429
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
import java.util.Arrays;
6+
7+
/**
8+
* A utility class for converting between IPv6 and IPv4 addresses.
9+
*
10+
* - Converts IPv4 to IPv6-mapped IPv6 address.
11+
* - Extracts IPv4 address from IPv6-mapped IPv6.
12+
* - Handles exceptions for invalid inputs.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class IPv6Converter {
17+
private IPv6Converter() {
18+
}
19+
20+
/**
21+
* Converts an IPv4 address (e.g., "192.0.2.128") to an IPv6-mapped IPv6 address.
22+
* Example: IPv4 "192.0.2.128" -> IPv6 "::ffff:192.0.2.128"
23+
*
24+
* @param ipv4Address The IPv4 address in string format.
25+
* @return The corresponding IPv6-mapped IPv6 address.
26+
* @throws UnknownHostException If the IPv4 address is invalid.
27+
* @throws IllegalArgumentException If the IPv6 address is not a mapped IPv4 address.
28+
*/
29+
public static String ipv4ToIpv6(String ipv4Address) throws UnknownHostException {
30+
if (ipv4Address == null || ipv4Address.isEmpty()) {
31+
throw new UnknownHostException("IPv4 address is empty.");
32+
}
33+
34+
InetAddress ipv4 = InetAddress.getByName(ipv4Address);
35+
byte[] ipv4Bytes = ipv4.getAddress();
36+
37+
// Create IPv6-mapped IPv6 address (starts with ::ffff:)
38+
byte[] ipv6Bytes = new byte[16];
39+
ipv6Bytes[10] = (byte) 0xff;
40+
ipv6Bytes[11] = (byte) 0xff;
41+
System.arraycopy(ipv4Bytes, 0, ipv6Bytes, 12, 4);
42+
43+
// Manually format to "::ffff:x.x.x.x" format
44+
StringBuilder ipv6String = new StringBuilder("::ffff:");
45+
for (int i = 12; i < 16; i++) {
46+
ipv6String.append(ipv6Bytes[i] & 0xFF);
47+
if (i < 15) {
48+
ipv6String.append('.');
49+
}
50+
}
51+
return ipv6String.toString();
52+
}
53+
54+
/**
55+
* Extracts the IPv4 address from an IPv6-mapped IPv6 address.
56+
* Example: IPv6 "::ffff:192.0.2.128" -> IPv4 "192.0.2.128"
57+
*
58+
* @param ipv6Address The IPv6 address in string format.
59+
* @return The extracted IPv4 address.
60+
* @throws UnknownHostException If the IPv6 address is invalid or not a mapped IPv4 address.
61+
*/
62+
public static String ipv6ToIpv4(String ipv6Address) throws UnknownHostException {
63+
InetAddress ipv6 = InetAddress.getByName(ipv6Address);
64+
byte[] ipv6Bytes = ipv6.getAddress();
65+
66+
// Check if the address is an IPv6-mapped IPv4 address
67+
if (isValidIpv6MappedIpv4(ipv6Bytes)) {
68+
byte[] ipv4Bytes = Arrays.copyOfRange(ipv6Bytes, 12, 16);
69+
InetAddress ipv4 = InetAddress.getByAddress(ipv4Bytes);
70+
return ipv4.getHostAddress();
71+
} else {
72+
throw new IllegalArgumentException("Not a valid IPv6-mapped IPv4 address.");
73+
}
74+
}
75+
76+
/**
77+
* Helper function to check if the given byte array represents
78+
* an IPv6-mapped IPv4 address (prefix 0:0:0:0:0:ffff).
79+
*
80+
* @param ipv6Bytes Byte array representation of the IPv6 address.
81+
* @return True if the address is IPv6-mapped IPv4, otherwise false.
82+
*/
83+
private static boolean isValidIpv6MappedIpv4(byte[] ipv6Bytes) {
84+
// IPv6-mapped IPv4 addresses are 16 bytes long, with the first 10 bytes set to 0,
85+
// followed by 0xff, 0xff, and the last 4 bytes representing the IPv4 address.
86+
if (ipv6Bytes.length != 16) {
87+
return false;
88+
}
89+
90+
for (int i = 0; i < 10; i++) {
91+
if (ipv6Bytes[i] != 0) {
92+
return false;
93+
}
94+
}
95+
96+
return ipv6Bytes[10] == (byte) 0xff && ipv6Bytes[11] == (byte) 0xff;
97+
}
98+
}

src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,49 @@
33
import static org.junit.jupiter.api.Assertions.assertFalse;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

6-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
710

811
/**
912
* Test case for IsPowerTwo class
1013
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
1114
*/
1215

1316
public class IsPowerTwoTest {
14-
@Test
15-
public void testIsPowerTwo() {
16-
// test some positive powers of 2
17-
assertTrue(IsPowerTwo.isPowerTwo(1));
18-
assertTrue(IsPowerTwo.isPowerTwo(2));
19-
assertTrue(IsPowerTwo.isPowerTwo(4));
20-
assertTrue(IsPowerTwo.isPowerTwo(16));
21-
assertTrue(IsPowerTwo.isPowerTwo(1024));
2217

23-
// test some negative numbers
24-
assertFalse(IsPowerTwo.isPowerTwo(-1));
25-
assertFalse(IsPowerTwo.isPowerTwo(-2));
26-
assertFalse(IsPowerTwo.isPowerTwo(-4));
18+
@ParameterizedTest
19+
@MethodSource("provideNumbersForPowerTwo")
20+
public void testIsPowerTwo(int number, boolean expected) {
21+
if (expected) {
22+
assertTrue(IsPowerTwo.isPowerTwo(number));
23+
} else {
24+
assertFalse(IsPowerTwo.isPowerTwo(number));
25+
}
26+
}
2727

28-
// test some numbers that are not powers of 2
29-
assertFalse(IsPowerTwo.isPowerTwo(0));
30-
assertFalse(IsPowerTwo.isPowerTwo(3));
31-
assertFalse(IsPowerTwo.isPowerTwo(5));
32-
assertFalse(IsPowerTwo.isPowerTwo(15));
33-
assertFalse(IsPowerTwo.isPowerTwo(1000));
28+
private static Stream<Arguments> provideNumbersForPowerTwo() {
29+
return Stream.of(Arguments.of(1, Boolean.TRUE), // 2^0
30+
Arguments.of(2, Boolean.TRUE), // 2^1
31+
Arguments.of(4, Boolean.TRUE), // 2^2
32+
Arguments.of(8, Boolean.TRUE), // 2^3
33+
Arguments.of(16, Boolean.TRUE), // 2^4
34+
Arguments.of(32, Boolean.TRUE), // 2^5
35+
Arguments.of(64, Boolean.TRUE), // 2^6
36+
Arguments.of(128, Boolean.TRUE), // 2^7
37+
Arguments.of(256, Boolean.TRUE), // 2^8
38+
Arguments.of(1024, Boolean.TRUE), // 2^10
39+
Arguments.of(0, Boolean.FALSE), // 0 is not a power of two
40+
Arguments.of(-1, Boolean.FALSE), // Negative number
41+
Arguments.of(-2, Boolean.FALSE), // Negative number
42+
Arguments.of(-4, Boolean.FALSE), // Negative number
43+
Arguments.of(3, Boolean.FALSE), // 3 is not a power of two
44+
Arguments.of(5, Boolean.FALSE), // 5 is not a power of two
45+
Arguments.of(6, Boolean.FALSE), // 6 is not a power of two
46+
Arguments.of(15, Boolean.FALSE), // 15 is not a power of two
47+
Arguments.of(1000, Boolean.FALSE), // 1000 is not a power of two
48+
Arguments.of(1023, Boolean.FALSE) // 1023 is not a power of two
49+
);
3450
}
3551
}

src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@
22

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

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
68

79
/**
810
* Test case for Non Repeating Number Finder
11+
* This test class validates the functionality of the
12+
* NonRepeatingNumberFinder by checking various scenarios.
13+
*
914
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
1015
*/
11-
1216
class NonRepeatingNumberFinderTest {
1317

14-
@Test
15-
void testNonRepeatingNumberFinder() {
16-
int[] arr = {1, 2, 1, 2, 6};
17-
assertEquals(6, NonRepeatingNumberFinder.findNonRepeatingNumber(arr));
18-
int[] arr1 = {1, 2, 1, 2};
19-
assertEquals(0, NonRepeatingNumberFinder.findNonRepeatingNumber(arr1));
20-
int[] arr2 = {12};
21-
assertEquals(12, NonRepeatingNumberFinder.findNonRepeatingNumber(arr2));
18+
@ParameterizedTest
19+
@MethodSource("testCases")
20+
void testNonRepeatingNumberFinder(int[] arr, int expected) {
21+
assertEquals(expected, NonRepeatingNumberFinder.findNonRepeatingNumber(arr));
22+
}
23+
24+
private static Arguments[] testCases() {
25+
return new Arguments[] {
26+
Arguments.of(new int[] {1, 2, 1, 2, 6}, 6), Arguments.of(new int[] {1, 2, 1, 2}, 0), // All numbers repeat
27+
Arguments.of(new int[] {12}, 12), // Single non-repeating number
28+
Arguments.of(new int[] {3, 5, 3, 4, 4}, 5), // More complex case
29+
Arguments.of(new int[] {7, 8, 7, 9, 8, 10, 10}, 9), // Non-repeating in the middle
30+
Arguments.of(new int[] {0, -1, 0, -1, 2}, 2), // Testing with negative numbers
31+
Arguments.of(new int[] {Integer.MAX_VALUE, 1, 1}, Integer.MAX_VALUE), // Edge case with max int
32+
Arguments.of(new int[] {2, 2, 3, 3, 4, 5, 4}, 5), // Mixed duplicates
33+
Arguments.of(new int[] {}, 0) // Edge case: empty array (should be handled as per design)
34+
};
2235
}
2336
}

0 commit comments

Comments
 (0)