Skip to content

Commit da99a73

Browse files
committed
chore: Adjust result to be the smallest positive solution in ChineseRemainderTheorem
1 parent fcac2b1 commit da99a73

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ public static int solveCRT(List<Integer> remainders, List<Integer> moduli) {
3636
result += remainders.get(i) * partialProduct * inverse;
3737
}
3838

39-
// The result should be modulo product to find the smallest positive solution
40-
return result % product;
39+
// Adjust result to be the smallest positive solution
40+
result = result % product;
41+
if (result < 0) {
42+
result += product;
43+
}
44+
45+
return result;
4146
}
4247

4348
/**

0 commit comments

Comments
 (0)