Skip to content

Commit 4a4298c

Browse files
committed
merge failure
2 parents 1481c9b + 754bf6c commit 4a4298c

File tree

11 files changed

+258
-53
lines changed

11 files changed

+258
-53
lines changed

spotbugs-exclude.xml

-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@
114114
<Match>
115115
<Bug pattern="BL_BURYING_LOGIC" />
116116
</Match>
117-
<Match>
118-
<Bug pattern="PCOA_PARTIALLY_CONSTRUCTED_OBJECT_ACCESS" />
119-
</Match>
120117
<Match>
121118
<Bug pattern="UTWR_USE_TRY_WITH_RESOURCES" />
122119
</Match>

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* An Armstrong number is often called a Narcissistic number.
1111
*
1212
* @author satyabarghav
13+
* @modifier rahul katteda - (13/01/2025) - [updated the logic for getting total number of digits]
1314
*/
1415
public class Armstrong {
1516

@@ -20,14 +21,16 @@ public class Armstrong {
2021
* @return {@code true} if the given number is an Armstrong number, {@code false} otherwise
2122
*/
2223
public boolean isArmstrong(int number) {
24+
if (number < 0) {
25+
return false; // Negative numbers cannot be Armstrong numbers
26+
}
2327
long sum = 0;
24-
String temp = Integer.toString(number); // Convert the given number to a string
25-
int power = temp.length(); // Extract the length of the number (number of digits)
28+
int totalDigits = (int) Math.log10(number) + 1; // get the length of the number (number of digits)
2629
long originalNumber = number;
2730

2831
while (originalNumber > 0) {
2932
long digit = originalNumber % 10;
30-
sum += (long) Math.pow(digit, power); // The digit raised to the power of the number of digits and added to the sum.
33+
sum += (long) Math.pow(digit, totalDigits); // The digit raised to the power of total number of digits and added to the sum.
3134
originalNumber /= 10;
3235
}
3336

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.maths;
2+
3+
import static com.thealgorithms.maths.PrimeCheck.isPrime;
4+
5+
/**
6+
* This is a representation of the unsolved problem of Goldbach's Projection, according to which every
7+
* even natural number greater than 2 can be written as the sum of 2 prime numbers
8+
* More info: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture
9+
* @author Vasilis Sarantidis (https://github.com/BILLSARAN)
10+
*/
11+
12+
public final class GoldbachConjecture {
13+
private GoldbachConjecture() {
14+
}
15+
public record Result(int number1, int number2) {
16+
}
17+
18+
public static Result getPrimeSum(int number) {
19+
if (number <= 2 || number % 2 != 0) {
20+
throw new IllegalArgumentException("Number must be even and greater than 2.");
21+
}
22+
23+
for (int i = 0; i <= number / 2; i++) {
24+
if (isPrime(i) && isPrime(number - i)) {
25+
return new Result(i, number - i);
26+
}
27+
}
28+
throw new IllegalStateException("No valid prime sum found."); // Should not occur
29+
}
30+
}

src/main/java/com/thealgorithms/matrix/MirrorOfMatrix.java

+42
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.thealgorithms.matrix;
22

33
// Problem Statement
4+
<<<<<<< HEAD
45

56
import com.thealgorithms.matrix.utils.MatrixUtil;
67

8+
=======
9+
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
710
/*
811
We have given an array of m x n (where m is the number of rows and n is the number of columns).
912
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
@@ -20,17 +23,56 @@ public final class MirrorOfMatrix {
2023
private MirrorOfMatrix() {
2124
}
2225

26+
<<<<<<< HEAD
2327
public static double[][] mirrorMatrix(final double[][] originalMatrix) {
2428
MatrixUtil.validateInputMatrix(originalMatrix);
29+
=======
30+
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
31+
if (originalMatrix == null) {
32+
// Handle invalid input
33+
return null;
34+
}
35+
if (originalMatrix.length == 0) {
36+
return new int[0][0];
37+
}
38+
39+
checkInput(originalMatrix);
40+
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
2541

2642
int numRows = originalMatrix.length;
2743
int numCols = originalMatrix[0].length;
2844

45+
<<<<<<< HEAD
2946
double[][] mirroredMatrix = new double[numRows][numCols];
3047

3148
for (int i = 0; i < numRows; i++) {
3249
mirroredMatrix[i] = MatrixUtil.reverseRow(originalMatrix[i]);
3350
}
3451
return mirroredMatrix;
3552
}
53+
=======
54+
int[][] mirroredMatrix = new int[numRows][numCols];
55+
56+
for (int i = 0; i < numRows; i++) {
57+
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
58+
}
59+
return mirroredMatrix;
60+
}
61+
private static int[] reverseRow(final int[] inRow) {
62+
int[] res = new int[inRow.length];
63+
for (int i = 0; i < inRow.length; ++i) {
64+
res[i] = inRow[inRow.length - 1 - i];
65+
}
66+
return res;
67+
}
68+
69+
private static void checkInput(final int[][] matrix) {
70+
// Check if all rows have the same number of columns
71+
for (int i = 1; i < matrix.length; i++) {
72+
if (matrix[i].length != matrix[0].length) {
73+
throw new IllegalArgumentException("The input is not a matrix.");
74+
}
75+
}
76+
}
77+
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
3678
}

src/main/java/com/thealgorithms/matrix/matrixexponentiation/Fibonacci.java

+58
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.thealgorithms.matrix.matrixexponentiation;
22

3+
<<<<<<< HEAD
34
import java.math.BigDecimal;
45
import java.util.Scanner;
56

67
import com.thealgorithms.matrix.utils.MatrixUtil;
78

9+
=======
10+
import java.util.Scanner;
11+
12+
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
813
/**
914
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
1015
* see https://www.geeksforgeeks.org/matrix-exponentiation/
@@ -15,13 +20,49 @@ private Fibonacci() {
1520
}
1621

1722
// Exponentiation matrix for Fibonacci sequence
23+
<<<<<<< HEAD
1824
private static final BigDecimal ONE = BigDecimal.valueOf(1);
1925
private static final BigDecimal ZERO = BigDecimal.valueOf(0);
2026

2127
private static final BigDecimal[][] FIB_MATRIX = {{ONE, ONE}, {ONE, ZERO}};
2228
private static final BigDecimal[][] IDENTITY_MATRIX = {{ONE, ZERO}, {ZERO, ONE}};
2329
// First 2 fibonacci numbers
2430
private static final BigDecimal[][] BASE_FIB_NUMBERS = {{ONE}, {ZERO}};
31+
=======
32+
private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
33+
private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
34+
// First 2 fibonacci numbers
35+
private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}};
36+
37+
/**
38+
* Performs multiplication of 2 matrices
39+
*
40+
* @param matrix1
41+
* @param matrix2
42+
* @return The product of matrix1 and matrix2
43+
*/
44+
private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
45+
// Check if matrices passed can be multiplied
46+
int rowsInMatrix1 = matrix1.length;
47+
int columnsInMatrix1 = matrix1[0].length;
48+
49+
int rowsInMatrix2 = matrix2.length;
50+
int columnsInMatrix2 = matrix2[0].length;
51+
52+
assert columnsInMatrix1 == rowsInMatrix2;
53+
int[][] product = new int[rowsInMatrix1][columnsInMatrix2];
54+
for (int rowIndex = 0; rowIndex < rowsInMatrix1; rowIndex++) {
55+
for (int colIndex = 0; colIndex < columnsInMatrix2; colIndex++) {
56+
int matrixEntry = 0;
57+
for (int intermediateIndex = 0; intermediateIndex < columnsInMatrix1; intermediateIndex++) {
58+
matrixEntry += matrix1[rowIndex][intermediateIndex] * matrix2[intermediateIndex][colIndex];
59+
}
60+
product[rowIndex][colIndex] = matrixEntry;
61+
}
62+
}
63+
return product;
64+
}
65+
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
2566

2667
/**
2768
* Calculates the fibonacci number using matrix exponentiaition technique
@@ -30,6 +71,7 @@ private Fibonacci() {
3071
* Outputs the nth * fibonacci number
3172
* @return a 2 X 1 array as { {F_n+1}, {F_n} }
3273
*/
74+
<<<<<<< HEAD
3375
public static BigDecimal[][] fib(int n) {
3476
if (n == 0) {
3577
return IDENTITY_MATRIX;
@@ -40,6 +82,18 @@ public static BigDecimal[][] fib(int n) {
4082
return matrixExpResult;
4183
} else {
4284
return MatrixUtil.multiply(FIB_MATRIX, matrixExpResult).get();
85+
=======
86+
public static int[][] fib(int n) {
87+
if (n == 0) {
88+
return IDENTITY_MATRIX;
89+
} else {
90+
int[][] cachedResult = fib(n / 2);
91+
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
92+
if (n % 2 == 0) {
93+
return matrixExpResult;
94+
} else {
95+
return matrixMultiplication(FIB_MATRIX, matrixExpResult);
96+
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
4397
}
4498
}
4599
}
@@ -48,7 +102,11 @@ public static void main(String[] args) {
48102
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
49103
Scanner sc = new Scanner(System.in);
50104
int n = sc.nextInt();
105+
<<<<<<< HEAD
51106
BigDecimal[][] result = MatrixUtil.multiply(fib(n), BASE_FIB_NUMBERS).get();
107+
=======
108+
int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
109+
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
52110
System.out.println("Fib(" + n + ") = " + result[1][0]);
53111
sc.close();
54112
}

src/main/java/com/thealgorithms/scheduling/SJFScheduling.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ public class SJFScheduling {
1414
protected ArrayList<ProcessDetails> processes;
1515
protected ArrayList<String> schedule;
1616

17+
private static void sortProcessesByArrivalTime(List<ProcessDetails> processes) {
18+
for (int i = 0; i < processes.size(); i++) {
19+
for (int j = i + 1; j < processes.size() - 1; j++) {
20+
if (processes.get(j).getArrivalTime() > processes.get(j + 1).getArrivalTime()) {
21+
final var temp = processes.get(j);
22+
processes.set(j, processes.get(j + 1));
23+
processes.set(j + 1, temp);
24+
}
25+
}
26+
}
27+
}
28+
1729
/**
1830
* a simple constructor
1931
* @param processes a list of processes the user wants to schedule
@@ -22,22 +34,10 @@ public class SJFScheduling {
2234
SJFScheduling(final ArrayList<ProcessDetails> processes) {
2335
this.processes = processes;
2436
schedule = new ArrayList<>();
25-
sortByArrivalTime();
37+
sortProcessesByArrivalTime(this.processes);
2638
}
2739
protected void sortByArrivalTime() {
28-
int size = processes.size();
29-
int i;
30-
int j;
31-
ProcessDetails temp;
32-
for (i = 0; i < size; i++) {
33-
for (j = i + 1; j < size - 1; j++) {
34-
if (processes.get(j).getArrivalTime() > processes.get(j + 1).getArrivalTime()) {
35-
temp = processes.get(j);
36-
processes.set(j, processes.get(j + 1));
37-
processes.set(j + 1, temp);
38-
}
39-
}
40-
}
40+
sortProcessesByArrivalTime(processes);
4141
}
4242

4343
/**

src/main/java/com/thealgorithms/strings/Upper.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ public static void main(String[] args) {
2121
* @return the {@code String}, converted to uppercase.
2222
*/
2323
public static String toUpperCase(String s) {
24-
if (s == null || s.isEmpty()) {
24+
if (s == null) {
25+
throw new IllegalArgumentException("Input string connot be null");
26+
}
27+
if (s.isEmpty()) {
2528
return s;
2629
}
27-
char[] values = s.toCharArray();
28-
for (int i = 0; i < values.length; ++i) {
29-
if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) {
30-
values[i] = Character.toUpperCase(values[i]);
30+
StringBuilder result = new StringBuilder(s);
31+
for (int i = 0; i < result.length(); ++i) {
32+
char currentChar = result.charAt(i);
33+
if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) {
34+
result.setCharAt(i, Character.toUpperCase(currentChar));
3135
}
3236
}
33-
return new String(values);
37+
return result.toString();
3438
}
3539
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
11
package com.thealgorithms.strings.zigZagPattern;
22

33
final class ZigZagPattern {
4+
45
private ZigZagPattern() {
56
}
67

8+
/**
9+
* Encodes a given string into a zig-zag pattern.
10+
*
11+
* @param s the input string to be encoded
12+
* @param numRows the number of rows in the zigzag pattern
13+
* @return the encoded string in zigzag pattern format
14+
*/
715
public static String encode(String s, int numRows) {
816
if (numRows < 2 || s.length() < numRows) {
917
return s;
1018
}
11-
int start = 0;
12-
int index = 0;
13-
int height = 1;
14-
int depth = numRows;
15-
char[] zigZagedArray = new char[s.length()];
16-
while (depth != 0) {
17-
int pointer = start;
18-
int heightSpace = 2 + ((height - 2) * 2);
19-
int depthSpace = 2 + ((depth - 2) * 2);
20-
boolean bool = true;
21-
while (pointer < s.length()) {
22-
zigZagedArray[index++] = s.charAt(pointer);
23-
if (heightSpace == 0) {
24-
pointer += depthSpace;
25-
} else if (depthSpace == 0) {
26-
pointer += heightSpace;
27-
} else if (bool) {
28-
pointer += depthSpace;
29-
bool = false;
30-
} else {
31-
pointer += heightSpace;
32-
bool = true;
19+
20+
StringBuilder result = new StringBuilder(s.length());
21+
int cycleLength = 2 * numRows - 2;
22+
23+
for (int row = 0; row < numRows; row++) {
24+
for (int j = row; j < s.length(); j += cycleLength) {
25+
result.append(s.charAt(j));
26+
27+
if (row > 0 && row < numRows - 1) {
28+
int diagonal = j + cycleLength - 2 * row;
29+
if (diagonal < s.length()) {
30+
result.append(s.charAt(diagonal));
31+
}
3332
}
3433
}
35-
height++;
36-
depth--;
37-
start++;
3834
}
39-
return new String(zigZagedArray);
35+
36+
return result.toString();
4037
}
4138
}

0 commit comments

Comments
 (0)