Skip to content

Commit 56af956

Browse files
Merge branch 'master' into RegularExpressionMatching
2 parents bee562d + 2d34bc1 commit 56af956

File tree

11 files changed

+560
-111
lines changed

11 files changed

+560
-111
lines changed

DIRECTORY.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java)
3232
* [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java)
3333
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
34+
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
3435
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
3536
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
3637
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
@@ -228,6 +229,7 @@
228229
* divideandconquer
229230
* [BinaryExponentiation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java)
230231
* [ClosestPair](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java)
232+
* [MedianOfTwoSortedArrays](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java)
231233
* [SkylineAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java)
232234
* [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
233235
* dynamicprogramming
@@ -473,6 +475,7 @@
473475
* scheduling
474476
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
475477
* [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
478+
* [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
476479
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
477480
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
478481
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
@@ -638,6 +641,7 @@
638641
* [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java)
639642
* [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java)
640643
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
644+
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
641645
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
642646
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
643647
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
@@ -729,6 +733,7 @@
729733
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
730734
* lists
731735
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
736+
* [CreateAndDetectLoopTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java)
732737
* [QuickSortLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java)
733738
* [ReverseKGroupTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java)
734739
* [RotateSinglyLinkedListsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java)
@@ -773,12 +778,15 @@
773778
* divideandconquer
774779
* [BinaryExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/BinaryExponentiationTest.java)
775780
* [ClosestPairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/ClosestPairTest.java)
781+
* [MedianOfTwoSortedArraysTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArraysTest.java)
776782
* [SkylineAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/SkylineAlgorithmTest.java)
777783
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
778784
* dynamicprogramming
779785
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
786+
* [BruteForceKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsackTest.java)
780787
* [CatalanNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CatalanNumberTest.java)
781788
* [ClimbStairsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ClimbStairsTest.java)
789+
* [CountFriendsPairingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/CountFriendsPairingTest.java)
782790
* [EditDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EditDistanceTest.java)
783791
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
784792
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
@@ -958,6 +966,7 @@
958966
* scheduling
959967
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
960968
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
969+
* [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
961970
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
962971
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
963972
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
Lines changed: 44 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
package com.thealgorithms.datastructures.lists;
22

3-
import java.util.Scanner;
4-
53
public final class CreateAndDetectLoop {
4+
5+
// Node class representing a single node in the linked list
66
private CreateAndDetectLoop() {
7+
throw new UnsupportedOperationException("Utility class");
78
}
9+
static final class Node {
10+
int data;
11+
Node next;
812

9-
/**
10-
* Prints the linked list.
11-
*
12-
* @param head head node of the linked list
13-
*/
14-
static void printList(Node head) {
15-
Node cur = head;
16-
17-
while (cur != null) {
18-
System.out.print(cur.value + " ");
19-
cur = cur.next;
13+
Node(int data) {
14+
this.data = data;
15+
next = null;
2016
}
2117
}
2218

23-
/**
24-
* Creates a loop in the linked list.
25-
*
26-
* @see
27-
* <a href="https://www.geeksforgeeks.org/make-loop-k-th-position-linked-list/">
28-
* GeeksForGeeks: Make a loop at K-th position</a>
29-
* @param head head node of the linked list
30-
* @param k position of node where loop is to be created
19+
// Method to create a loop between two specific positions in the linked list
20+
/*
21+
* Test case that shows the Cycle(loop) in a LinkedList
22+
* Let's take this linked list:
23+
* 1->2->3->4->5->6
24+
* \______/
25+
* In this linked list we can see there is a cycle.
26+
* we can create loop by calling createLoop function in main after creating LL
27+
* createLoop(head,2,5);
28+
* to detect there is loop or not we can call detectloop function in main
29+
* detectloop(head);
3130
*/
32-
static void createLoop(Node head, int k) {
33-
if (head == null) {
31+
32+
static void createLoop(Node head, int position1, int position2) {
33+
if (position1 == 0 || position2 == 0) {
3434
return;
3535
}
36-
Node temp = head;
37-
int count = 1;
38-
while (count < k) { // Traverse the list till the kth node
39-
temp = temp.next;
40-
count++;
41-
}
4236

43-
Node connectedPoint = temp;
37+
Node node1 = head;
38+
Node node2 = head;
4439

45-
while (temp.next != null) { // Traverse remaining nodes
46-
temp = temp.next;
40+
int count1 = 1;
41+
int count2 = 1;
42+
// Traverse to find node at position1
43+
while (count1 < position1 && node1 != null) {
44+
node1 = node1.next;
45+
count1++;
4746
}
4847

49-
temp.next = connectedPoint; // Connect last node to k-th element
50-
}
48+
// Traverse to find node at position2
49+
while (count2 < position2 && node2 != null) {
50+
node2 = node2.next;
51+
count2++;
52+
}
5153

54+
// Create a loop by connecting node2's next to node1
55+
if (node1 != null && node2 != null) {
56+
node2.next = node1;
57+
}
58+
}
59+
// Method to detect a loop in the linked list
5260
/**
5361
* Detects the presence of a loop in the linked list.
5462
*
55-
* @see
56-
* <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">
57-
* Floyd's Cycle Detection Algorithm</a>
58-
*
59-
* @param head the head node of the linked list
60-
*
63+
* @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">Floyd's Cycle Detection Algorithm</a>
6164
* @return true if loop exists else false
6265
*/
6366
static boolean detectLoop(Node head) {
@@ -67,40 +70,10 @@ static boolean detectLoop(Node head) {
6770
while (fptr != null && fptr.next != null) {
6871
sptr = sptr.next;
6972
fptr = fptr.next.next;
70-
if (fptr == sptr) {
73+
if (sptr == fptr) {
7174
return true;
7275
}
7376
}
74-
7577
return false;
7678
}
77-
78-
public static void main(String[] args) {
79-
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
80-
Scanner sc = new Scanner(System.in);
81-
82-
System.out.println("Enter the number of elements to be inserted: ");
83-
int n = sc.nextInt();
84-
System.out.printf("Enter the %d elements: %n", n);
85-
while (n-- > 0) {
86-
singlyLinkedList.insert(sc.nextInt());
87-
}
88-
89-
System.out.print("Given list: ");
90-
printList(singlyLinkedList.getHead());
91-
System.out.println();
92-
93-
System.out.println("Enter the location to generate loop: ");
94-
int k = sc.nextInt();
95-
96-
createLoop(singlyLinkedList.getHead(), k);
97-
98-
if (detectLoop(singlyLinkedList.getHead())) {
99-
System.out.println("Loop found");
100-
} else {
101-
System.out.println("No loop found");
102-
}
103-
104-
sc.close();
105-
}
10679
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.divideandconquer;
2+
3+
public final class MedianOfTwoSortedArrays {
4+
5+
private MedianOfTwoSortedArrays() {
6+
}
7+
8+
/**
9+
* Finds the median of two sorted arrays in logarithmic time.
10+
*
11+
* @param nums1 the first sorted array
12+
* @param nums2 the second sorted array
13+
* @return the median of the combined sorted array
14+
* @throws IllegalArgumentException if the input arrays are not sorted
15+
*/
16+
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
17+
if (nums1.length > nums2.length) {
18+
return findMedianSortedArrays(nums2, nums1); // Ensure nums1 is the smaller array
19+
}
20+
21+
int m = nums1.length;
22+
int n = nums2.length;
23+
int low = 0;
24+
int high = m;
25+
while (low <= high) {
26+
int partition1 = (low + high) / 2; // Partition in the first array
27+
int partition2 = (m + n + 1) / 2 - partition1; // Partition in the second array
28+
29+
int maxLeft1 = (partition1 == 0) ? Integer.MIN_VALUE : nums1[partition1 - 1];
30+
int minRight1 = (partition1 == m) ? Integer.MAX_VALUE : nums1[partition1];
31+
int maxLeft2 = (partition2 == 0) ? Integer.MIN_VALUE : nums2[partition2 - 1];
32+
int minRight2 = (partition2 == n) ? Integer.MAX_VALUE : nums2[partition2];
33+
34+
// Check if partition is valid
35+
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
36+
// If combined array length is odd
37+
if (((m + n) & 1) == 1) {
38+
return Math.max(maxLeft1, maxLeft2);
39+
}
40+
// If combined array length is even
41+
else {
42+
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;
43+
}
44+
} else if (maxLeft1 > minRight2) {
45+
high = partition1 - 1;
46+
} else {
47+
low = partition1 + 1;
48+
}
49+
}
50+
51+
throw new IllegalArgumentException("Input arrays are not sorted");
52+
}
53+
}
Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,67 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
/* A Naive recursive implementation
4-
of 0-1 Knapsack problem */
3+
/**
4+
* A naive recursive implementation of the 0-1 Knapsack problem.
5+
*
6+
* <p>The 0-1 Knapsack problem is a classic optimization problem where you are
7+
* given a set of items, each with a weight and a value, and a knapsack with a
8+
* fixed capacity. The goal is to determine the maximum value that can be
9+
* obtained by selecting a subset of the items such that the total weight does
10+
* not exceed the knapsack's capacity. Each item can either be included (1) or
11+
* excluded (0), hence the name "0-1" Knapsack.</p>
12+
*
13+
* <p>This class provides a brute-force recursive approach to solving the
14+
* problem. It evaluates all possible combinations of items to find the optimal
15+
* solution, but this approach has exponential time complexity and is not
16+
* suitable for large input sizes.</p>
17+
*
18+
* <p><b>Time Complexity:</b> O(2^n), where n is the number of items.</p>
19+
*
20+
* <p><b>Space Complexity:</b> O(n), due to the recursive function call stack.</p>
21+
*/
522
public final class BruteForceKnapsack {
623
private BruteForceKnapsack() {
724
}
8-
// Returns the maximum value that
9-
// can be put in a knapsack of
10-
// capacity W
25+
26+
/**
27+
* Solves the 0-1 Knapsack problem using a recursive brute-force approach.
28+
*
29+
* @param w the total capacity of the knapsack
30+
* @param wt an array where wt[i] represents the weight of the i-th item
31+
* @param val an array where val[i] represents the value of the i-th item
32+
* @param n the number of items available for selection
33+
* @return the maximum value that can be obtained with the given capacity
34+
*
35+
* <p>The function uses recursion to explore all possible subsets of items.
36+
* For each item, it has two choices: either include it in the knapsack
37+
* (if it fits) or exclude it. It returns the maximum value obtainable
38+
* through these two choices.</p>
39+
*
40+
* <p><b>Base Cases:</b>
41+
* <ul>
42+
* <li>If no items are left (n == 0), the maximum value is 0.</li>
43+
* <li>If the knapsack's remaining capacity is 0 (w == 0), no more items can
44+
* be included, and the value is 0.</li>
45+
* </ul></p>
46+
*
47+
* <p><b>Recursive Steps:</b>
48+
* <ul>
49+
* <li>If the weight of the n-th item exceeds the current capacity, it is
50+
* excluded from the solution, and the function proceeds with the remaining
51+
* items.</li>
52+
* <li>Otherwise, the function considers two possibilities: include the n-th
53+
* item or exclude it, and returns the maximum value of these two scenarios.</li>
54+
* </ul></p>
55+
*/
1156
static int knapSack(int w, int[] wt, int[] val, int n) {
12-
// Base Case
1357
if (n == 0 || w == 0) {
1458
return 0;
1559
}
1660

17-
// If weight of the nth item is
18-
// more than Knapsack capacity W,
19-
// then this item cannot be included
20-
// in the optimal solution
2161
if (wt[n - 1] > w) {
2262
return knapSack(w, wt, val, n - 1);
23-
} // Return the maximum of two cases:
24-
// (1) nth item included
25-
// (2) not included
26-
else {
27-
return Math.max(val[n - 1] + knapSack(w - wt[n - 1], wt, val, n - 1), knapSack(w, wt, val, n - 1));
63+
} else {
64+
return Math.max(knapSack(w, wt, val, n - 1), val[n - 1] + knapSack(w - wt[n - 1], wt, val, n - 1));
2865
}
2966
}
30-
31-
// Driver code
32-
public static void main(String[] args) {
33-
int[] val = new int[] {60, 100, 120};
34-
int[] wt = new int[] {10, 20, 30};
35-
int w = 50;
36-
int n = val.length;
37-
System.out.println(knapSack(w, wt, val, n));
38-
}
3967
}

0 commit comments

Comments
 (0)