Skip to content

Commit 0e3654d

Browse files
authored
Merge branch 'master' into master
2 parents a8093e2 + b231a72 commit 0e3654d

File tree

14 files changed

+387
-208
lines changed

14 files changed

+387
-208
lines changed

src/main/java/com/thealgorithms/backtracking/NQueens.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@ public final class NQueens {
3636
private NQueens() {
3737
}
3838

39-
public static void main(String[] args) {
40-
placeQueens(1);
41-
placeQueens(2);
42-
placeQueens(3);
43-
placeQueens(4);
44-
placeQueens(5);
45-
placeQueens(6);
39+
public static List<List<String>> getNQueensArrangements(int queens) {
40+
List<List<String>> arrangements = new ArrayList<>();
41+
getSolution(queens, arrangements, new int[queens], 0);
42+
return arrangements;
4643
}
4744

4845
public static void placeQueens(final int queens) {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Converts any Hexadecimal Number to Octal
75
*
@@ -12,64 +10,53 @@ private HexToOct() {
1210
}
1311

1412
/**
15-
* This method converts a Hexadecimal number to a decimal number
13+
* Converts a Hexadecimal number to a Decimal number.
1614
*
17-
* @param s The Hexadecimal Number
18-
* @return The Decimal number
15+
* @param hex The Hexadecimal number as a String.
16+
* @return The Decimal equivalent as an integer.
1917
*/
20-
public static int hex2decimal(String s) {
21-
String str = "0123456789ABCDEF";
22-
s = s.toUpperCase();
23-
int val = 0;
24-
for (int i = 0; i < s.length(); i++) {
25-
char a = s.charAt(i);
26-
int n = str.indexOf(a);
27-
val = 16 * val + n;
18+
public static int hexToDecimal(String hex) {
19+
String hexDigits = "0123456789ABCDEF";
20+
hex = hex.toUpperCase();
21+
int decimalValue = 0;
22+
23+
for (int i = 0; i < hex.length(); i++) {
24+
char hexChar = hex.charAt(i);
25+
int digitValue = hexDigits.indexOf(hexChar);
26+
decimalValue = 16 * decimalValue + digitValue;
2827
}
29-
return val;
28+
29+
return decimalValue;
3030
}
3131

3232
/**
33-
* This method converts a Decimal number to a octal number
33+
* Converts a Decimal number to an Octal number.
3434
*
35-
* @param q The Decimal Number
36-
* @return The Octal number
35+
* @param decimal The Decimal number as an integer.
36+
* @return The Octal equivalent as an integer.
3737
*/
38-
public static int decimal2octal(int q) {
39-
int now;
40-
int i = 1;
41-
int octnum = 0;
42-
while (q > 0) {
43-
now = q % 8;
44-
octnum = (now * (int) (Math.pow(10, i))) + octnum;
45-
q /= 8;
46-
i++;
38+
public static int decimalToOctal(int decimal) {
39+
int octalValue = 0;
40+
int placeValue = 1;
41+
42+
while (decimal > 0) {
43+
int remainder = decimal % 8;
44+
octalValue += remainder * placeValue;
45+
decimal /= 8;
46+
placeValue *= 10;
4747
}
48-
octnum /= 10;
49-
return octnum;
48+
49+
return octalValue;
5050
}
5151

5252
/**
53-
* Main method that gets the hex input from user and converts it into octal.
53+
* Converts a Hexadecimal number to an Octal number.
5454
*
55-
* @param args arguments
55+
* @param hex The Hexadecimal number as a String.
56+
* @return The Octal equivalent as an integer.
5657
*/
57-
public static void main(String[] args) {
58-
String hexadecnum;
59-
int decnum;
60-
int octalnum;
61-
Scanner scan = new Scanner(System.in);
62-
63-
System.out.print("Enter Hexadecimal Number : ");
64-
hexadecnum = scan.nextLine();
65-
66-
// first convert hexadecimal to decimal
67-
decnum = hex2decimal(hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
68-
// variable decnum
69-
70-
// convert decimal to octal
71-
octalnum = decimal2octal(decnum);
72-
System.out.println("Number in octal: " + octalnum);
73-
scan.close();
58+
public static int hexToOctal(String hex) {
59+
int decimalValue = hexToDecimal(hex);
60+
return decimalToOctal(decimalValue);
7461
}
7562
}

src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java

+7-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Scanner;
4-
53
/*
64
* @author Ojasva Jain
75
* Determinant of a Matrix Wikipedia link: https://en.wikipedia.org/wiki/Determinant
@@ -10,8 +8,13 @@ public final class DeterminantOfMatrix {
108
private DeterminantOfMatrix() {
119
}
1210

13-
// Determinant calculator
14-
//@return determinant of the input matrix
11+
/**
12+
* Calculates the determinant of a given matrix.
13+
*
14+
* @param a the input matrix
15+
* @param n the size of the matrix
16+
* @return the determinant of the matrix
17+
*/
1518
static int determinant(int[][] a, int n) {
1619
int det = 0;
1720
int sign = 1;
@@ -41,21 +44,4 @@ static int determinant(int[][] a, int n) {
4144
}
4245
return det;
4346
}
44-
45-
// Driver Method
46-
public static void main(String[] args) {
47-
Scanner in = new Scanner(System.in);
48-
// Input Matrix
49-
System.out.println("Enter matrix size (Square matrix only)");
50-
int n = in.nextInt();
51-
System.out.println("Enter matrix");
52-
int[][] a = new int[n][n];
53-
for (int i = 0; i < n; i++) {
54-
for (int j = 0; j < n; j++) {
55-
a[i][j] = in.nextInt();
56-
}
57-
}
58-
System.out.println(determinant(a, n));
59-
in.close();
60-
}
6147
}
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,65 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Arrays;
43
import java.util.Random;
54

65
/**
7-
* use quick sort algorithm to get kth largest or kth smallest element in given array
6+
* Use a quicksort-based approach to identify the k-th largest or k-th max element within the provided array.
87
*/
98
public final class FindKthNumber {
109
private FindKthNumber() {
1110
}
1211

1312
private static final Random RANDOM = new Random();
1413

15-
public static void main(String[] args) {
16-
/* generate an array with random size and random elements */
17-
int[] nums = generateArray(100);
18-
19-
/* get 3th largest element */
20-
int kth = 3;
21-
int kthMaxIndex = nums.length - kth;
22-
int targetMax = findKthMax(nums, kthMaxIndex);
23-
24-
/* get 3th smallest element */
25-
int kthMinIndex = kth - 1;
26-
int targetMin = findKthMax(nums, kthMinIndex);
14+
public static int findKthMax(int[] array, int k) {
15+
if (k <= 0 || k > array.length) {
16+
throw new IllegalArgumentException("k must be between 1 and the size of the array");
17+
}
2718

28-
Arrays.sort(nums);
29-
assert nums[kthMaxIndex] == targetMax;
30-
assert nums[kthMinIndex] == targetMin;
19+
// Convert k-th largest to index for QuickSelect
20+
return quickSelect(array, 0, array.length - 1, array.length - k);
3121
}
3222

33-
private static int[] generateArray(int capacity) {
34-
int size = RANDOM.nextInt(capacity) + 1;
35-
int[] array = new int[size];
36-
37-
for (int i = 0; i < size; i++) {
38-
array[i] = RANDOM.nextInt() % 100;
23+
private static int quickSelect(int[] array, int left, int right, int kSmallest) {
24+
if (left == right) {
25+
return array[left];
3926
}
40-
return array;
41-
}
4227

43-
private static int findKthMax(int[] nums, int k) {
44-
int start = 0;
45-
int end = nums.length;
46-
while (start < end) {
47-
int pivot = partition(nums, start, end);
48-
if (k == pivot) {
49-
return nums[pivot];
50-
} else if (k > pivot) {
51-
start = pivot + 1;
52-
} else {
53-
end = pivot;
54-
}
28+
// Randomly select a pivot index
29+
int pivotIndex = left + RANDOM.nextInt(right - left + 1);
30+
pivotIndex = partition(array, left, right, pivotIndex);
31+
32+
if (kSmallest == pivotIndex) {
33+
return array[kSmallest];
34+
} else if (kSmallest < pivotIndex) {
35+
return quickSelect(array, left, pivotIndex - 1, kSmallest);
36+
} else {
37+
return quickSelect(array, pivotIndex + 1, right, kSmallest);
5538
}
56-
return -1;
5739
}
5840

59-
private static int partition(int[] nums, int start, int end) {
60-
int pivot = nums[start];
61-
int j = start;
62-
for (int i = start + 1; i < end; i++) {
63-
if (nums[i] < pivot) {
64-
j++;
65-
swap(nums, i, j);
41+
private static int partition(int[] array, int left, int right, int pivotIndex) {
42+
int pivotValue = array[pivotIndex];
43+
// Move pivot to end
44+
swap(array, pivotIndex, right);
45+
int storeIndex = left;
46+
47+
// Move all smaller elements to the left
48+
for (int i = left; i < right; i++) {
49+
if (array[i] < pivotValue) {
50+
swap(array, storeIndex, i);
51+
storeIndex++;
6652
}
6753
}
68-
swap(nums, start, j);
69-
return j;
54+
55+
// Move pivot to its final place
56+
swap(array, storeIndex, right);
57+
return storeIndex;
7058
}
7159

72-
private static void swap(int[] nums, int a, int b) {
73-
int tmp = nums[a];
74-
nums[a] = nums[b];
75-
nums[b] = tmp;
60+
private static void swap(int[] array, int i, int j) {
61+
int temp = array[i];
62+
array[i] = array[j];
63+
array[j] = temp;
7664
}
7765
}

0 commit comments

Comments
 (0)