From 6a7bd635f189563d115c4ce227fce12a181ca018 Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 17:37:39 +0530 Subject: [PATCH 01/10] feature: Adding MinSumKSizeSubarray in Sliding Window Algorithm --- .../slidingwindow/MinSumKSizeSubarray.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java new file mode 100644 index 000000000000..ab3788dac25f --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java @@ -0,0 +1,52 @@ +package com.thealgorithms.slidingwindow; + +/** + * The Sliding Window algorithm is used to find the minimum sum of a subarray + * of a fixed size k within a given array. + * + *

+ * Worst-case performance O(n) + * Best-case performance O(n) + * Average performance O(n) + * Worst-case space complexity O(1) + * + * This class provides a static method to find the minimum sum of a subarray + * with a specified length k. + * + * @author Rashi Dashore (https://github.com/rashi07dashore) + */ +public final class MinSumKSizeSubarray { + + // Prevent instantiation + private MinSumKSizeSubarray() {} + + /** + * This method finds the minimum sum of a subarray of a given size k. + * + * @param arr is the input array where the minimum sum needs to be found + * @param k is the size of the subarray + * @return the minimum sum of the subarray of size k + */ + + public static int minSumKSizeSubarray(int[] arr, int k) { + if (arr.length < k) { + return -1; // Edge case: not enough elements + } + + int minSum; + int windowSum = 0; + + // Calculate the sum of the first window + for (int i = 0; i < k; i++) { + windowSum += arr[i]; + } + minSum = windowSum; + + // Slide the window across the array + for (int i = k; i < arr.length; i++) { + windowSum += arr[i] - arr[i - k]; + minSum = Math.min(minSum, windowSum); + } + return minSum; + } +} From 3a7ac387dbc83dcef6698f818c468be439856bee Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 17:46:06 +0530 Subject: [PATCH 02/10] slidingWindowUpdated with MinSumKSizeSubarray --- .../com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java index ab3788dac25f..eba9a721db32 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java +++ b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java @@ -18,7 +18,7 @@ public final class MinSumKSizeSubarray { // Prevent instantiation - private MinSumKSizeSubarray() {} + private MinSumKSizeSubarray() { } /** * This method finds the minimum sum of a subarray of a given size k. From 80ebb48ce5619b573d72b6bb00edafba9bd2b3f1 Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 17:50:23 +0530 Subject: [PATCH 03/10] updated the line format --- .../com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java index eba9a721db32..51eb46aaf0d6 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java +++ b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java @@ -18,7 +18,9 @@ public final class MinSumKSizeSubarray { // Prevent instantiation - private MinSumKSizeSubarray() { } + private MinSumKSizeSubarray() { + + } /** * This method finds the minimum sum of a subarray of a given size k. From c76f66247a6f7ddcc304fd11ebad7c4c1773458a Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 17:52:35 +0530 Subject: [PATCH 04/10] line updated --- .../com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java index 51eb46aaf0d6..5d86f1c14f60 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java +++ b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java @@ -18,8 +18,7 @@ public final class MinSumKSizeSubarray { // Prevent instantiation - private MinSumKSizeSubarray() { - + private MinSumKSizeSubarray() { } /** From c1f52e9788adb4762943acf07fc345ce14d7f4fe Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 18:00:06 +0530 Subject: [PATCH 05/10] lines updated --- .../com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java index 5d86f1c14f60..40a5441fa7a0 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java +++ b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java @@ -1,5 +1,4 @@ package com.thealgorithms.slidingwindow; - /** * The Sliding Window algorithm is used to find the minimum sum of a subarray * of a fixed size k within a given array. @@ -18,7 +17,7 @@ public final class MinSumKSizeSubarray { // Prevent instantiation - private MinSumKSizeSubarray() { + private MinSumKSizeSubarray() { } /** @@ -28,7 +27,6 @@ private MinSumKSizeSubarray() { * @param k is the size of the subarray * @return the minimum sum of the subarray of size k */ - public static int minSumKSizeSubarray(int[] arr, int k) { if (arr.length < k) { return -1; // Edge case: not enough elements From 53b36994752c409feb590d4fbebc8fbce321c267 Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 18:18:19 +0530 Subject: [PATCH 06/10] Added Shuffle Algorithm Fisher-Yates (Knuth) Shuffle algorithm randomly permutes an array's elements, ensuring each permutation is equally likely. --- .../com/thealgorithms/misc/ShuffleArray.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/com/thealgorithms/misc/ShuffleArray.java diff --git a/src/main/java/com/thealgorithms/misc/ShuffleArray.java b/src/main/java/com/thealgorithms/misc/ShuffleArray.java new file mode 100644 index 000000000000..9db73e8d9355 --- /dev/null +++ b/src/main/java/com/thealgorithms/misc/ShuffleArray.java @@ -0,0 +1,73 @@ +package com.thealgorithms.misc; +import java.util.Random; +import java.util.Scanner; +/** + * The Fisher-Yates (Knuth) Shuffle algorithm randomly permutes an array's + * elements, ensuring each permutation is equally likely. + * + *

+ * Worst-case performance O(n) + * Best-case performance O(n) + * Average performance O(n) + * Worst-case space complexity O(1) + * + * This class provides a static method to shuffle an array in place. + * + * @author Your Name (https://github.com/Chiefpatwal) + */ +public final class ShuffleArray { + // Prevent instantiation + private ShuffleArray() { + } + /** + * This method shuffles an array using the Fisher-Yates algorithm. + * + * @param arr is the input array to be shuffled + */ + public static void shuffle(int[] arr) { + Random random = new Random(); + for (int i = arr.length - 1; i > 0; i--) { + int j = random.nextInt(i + 1); + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + /** + * This method takes user input to create an array. + * + * @return the array created from user input + */ + public static int[] getUserInputArray() { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the size of the array: "); + int size = scanner.nextInt(); + + int[] arr = new int[size]; + System.out.println("Enter " + size + " elements:"); + for (int i = 0; i < size; i++) { + arr[i] = scanner.nextInt(); + } + return arr; + } + public static void main(String[] args) { + int[] userArray = getUserInputArray(); + System.out.println("Original Array:"); + printArray(userArray); + + shuffle(userArray); + System.out.println("Shuffled Array:"); + printArray(userArray); + } + /** + * This method prints the elements of the array. + * + * @param arr is the input array to be printed + */ + public static void printArray(int[] arr) { + for (int num : arr) { + System.out.print(num + " "); + } + System.out.println(); + } +} From eb5bfb543981b40f2702b0e4ba80bd7a10df2661 Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 20:28:29 +0530 Subject: [PATCH 07/10] Added Junit Testing --- .../com/thealgorithms/misc/ShuffleArray.java | 29 +---- .../thealgorithms/misc/ShuffleArrayTest.java | 107 ++++++++++++++++++ 2 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java diff --git a/src/main/java/com/thealgorithms/misc/ShuffleArray.java b/src/main/java/com/thealgorithms/misc/ShuffleArray.java index 9db73e8d9355..0e23b083fc71 100644 --- a/src/main/java/com/thealgorithms/misc/ShuffleArray.java +++ b/src/main/java/com/thealgorithms/misc/ShuffleArray.java @@ -1,6 +1,5 @@ package com.thealgorithms.misc; import java.util.Random; -import java.util.Scanner; /** * The Fisher-Yates (Knuth) Shuffle algorithm randomly permutes an array's * elements, ensuring each permutation is equally likely. @@ -13,7 +12,7 @@ * * This class provides a static method to shuffle an array in place. * - * @author Your Name (https://github.com/Chiefpatwal) + * @author Rashi Dashore (https://github.com/rashi07dashore) */ public final class ShuffleArray { // Prevent instantiation @@ -33,32 +32,6 @@ public static void shuffle(int[] arr) { arr[j] = temp; } } - /** - * This method takes user input to create an array. - * - * @return the array created from user input - */ - public static int[] getUserInputArray() { - Scanner scanner = new Scanner(System.in); - System.out.print("Enter the size of the array: "); - int size = scanner.nextInt(); - - int[] arr = new int[size]; - System.out.println("Enter " + size + " elements:"); - for (int i = 0; i < size; i++) { - arr[i] = scanner.nextInt(); - } - return arr; - } - public static void main(String[] args) { - int[] userArray = getUserInputArray(); - System.out.println("Original Array:"); - printArray(userArray); - - shuffle(userArray); - System.out.println("Shuffled Array:"); - printArray(userArray); - } /** * This method prints the elements of the array. * diff --git a/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java new file mode 100644 index 000000000000..231b1eecbed0 --- /dev/null +++ b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java @@ -0,0 +1,107 @@ +package com.thealgorithms.misc; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the ShuffleArray class. + * + * @author Rashi Dashore (https://github.com/rashi07dashore) + */ +class ShuffleArrayTest { + + /** + * Test for basic shuffling. + */ + @Test + void testShuffleBasic() { + int[] arr = {1, 2, 3, 4, 5}; + int[] originalArr = arr.clone(); // Clone original array for comparison + ShuffleArray.shuffle(arr); + + // Check that the shuffled array is not the same as the original + assertNotEquals(originalArr, arr); + } + + /** + * Test for an edge case with a single element. + */ + @Test + void testShuffleSingleElement() { + int[] arr = {1}; + int[] originalArr = arr.clone(); + ShuffleArray.shuffle(arr); + + // Check that the shuffled array is the same as the original + assertArrayEquals(originalArr, arr); + } + + /** + * Test for an edge case with two elements. + */ + @Test + void testShuffleTwoElements() { + int[] arr = {1, 2}; + int[] originalArr = arr.clone(); + ShuffleArray.shuffle(arr); + + // Check that the shuffled array is not the same as the original + assertNotEquals(originalArr, arr); + // Check that the shuffled array still contains the same elements + assertTrue(arr[0] == 1 || arr[0] == 2); + assertTrue(arr[1] == 1 || arr[1] == 2); + } + + /** + * Test for an empty array. + */ + @Test + void testShuffleEmptyArray() { + int[] arr = {}; + int[] originalArr = arr.clone(); + ShuffleArray.shuffle(arr); + + // Check that the shuffled array is the same as the original (still empty) + assertArrayEquals(originalArr, arr); + } + + /** + * Test for large array. + */ + @Test + void testShuffleLargeArray() { + int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int[] originalArr = arr.clone(); + ShuffleArray.shuffle(arr); + + // Check that the shuffled array is not the same as the original + assertNotEquals(originalArr, arr); + } + + /** + * Test to ensure all elements are present after shuffle. + */ + @Test + void testShuffleRetainsElements() { + int[] arr = {1, 2, 3, 4, 5}; + ShuffleArray.shuffle(arr); + + // Check that the shuffled array contains the same elements + assertTrue(arr.length == 5); + for (int i = 1; i <= 5; i++) { + assertTrue(contains(arr, i)); + } + } + + private boolean contains(int[] arr, int value) { + for (int num : arr) { + if (num == value) { + return true; + } + } + return false; + } +} From 2a60bb1b9616654cbb46b0a26fac70a3049d5781 Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 20:30:48 +0530 Subject: [PATCH 08/10] updated line format --- src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java index 231b1eecbed0..81501557f612 100644 --- a/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java +++ b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.misc; -import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -88,7 +88,7 @@ void testShuffleLargeArray() { void testShuffleRetainsElements() { int[] arr = {1, 2, 3, 4, 5}; ShuffleArray.shuffle(arr); - + // Check that the shuffled array contains the same elements assertTrue(arr.length == 5); for (int i = 1; i <= 5; i++) { From 27bc1fbe2c7bf3664dbfdbccd924bb698d42950e Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Sat, 26 Oct 2024 20:34:56 +0530 Subject: [PATCH 09/10] update --- .../thealgorithms/misc/ShuffleArrayTest.java | 27 ++----------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java index 81501557f612..915b83e376b6 100644 --- a/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java +++ b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java @@ -6,16 +6,8 @@ import org.junit.jupiter.api.Test; -/** - * Unit tests for the ShuffleArray class. - * - * @author Rashi Dashore (https://github.com/rashi07dashore) - */ -class ShuffleArrayTest { - - /** - * Test for basic shuffling. - */ +public class ShuffleArrayTest { + @Test void testShuffleBasic() { int[] arr = {1, 2, 3, 4, 5}; @@ -26,9 +18,6 @@ void testShuffleBasic() { assertNotEquals(originalArr, arr); } - /** - * Test for an edge case with a single element. - */ @Test void testShuffleSingleElement() { int[] arr = {1}; @@ -39,9 +28,6 @@ void testShuffleSingleElement() { assertArrayEquals(originalArr, arr); } - /** - * Test for an edge case with two elements. - */ @Test void testShuffleTwoElements() { int[] arr = {1, 2}; @@ -55,9 +41,6 @@ void testShuffleTwoElements() { assertTrue(arr[1] == 1 || arr[1] == 2); } - /** - * Test for an empty array. - */ @Test void testShuffleEmptyArray() { int[] arr = {}; @@ -68,9 +51,6 @@ void testShuffleEmptyArray() { assertArrayEquals(originalArr, arr); } - /** - * Test for large array. - */ @Test void testShuffleLargeArray() { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; @@ -81,9 +61,6 @@ void testShuffleLargeArray() { assertNotEquals(originalArr, arr); } - /** - * Test to ensure all elements are present after shuffle. - */ @Test void testShuffleRetainsElements() { int[] arr = {1, 2, 3, 4, 5}; From 799d7dc2227e29bdf4fb564cceaaf629b19ad230 Mon Sep 17 00:00:00 2001 From: Rashi Dashore Date: Wed, 30 Oct 2024 01:06:50 +0530 Subject: [PATCH 10/10] Updated - removed the print method --- .../com/thealgorithms/misc/ShuffleArray.java | 14 ++--- .../slidingwindow/MinSumKSizeSubarray.java | 51 ------------------- 2 files changed, 3 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java diff --git a/src/main/java/com/thealgorithms/misc/ShuffleArray.java b/src/main/java/com/thealgorithms/misc/ShuffleArray.java index 0e23b083fc71..65d38adfc37a 100644 --- a/src/main/java/com/thealgorithms/misc/ShuffleArray.java +++ b/src/main/java/com/thealgorithms/misc/ShuffleArray.java @@ -1,5 +1,7 @@ package com.thealgorithms.misc; + import java.util.Random; + /** * The Fisher-Yates (Knuth) Shuffle algorithm randomly permutes an array's * elements, ensuring each permutation is equally likely. @@ -18,6 +20,7 @@ public final class ShuffleArray { // Prevent instantiation private ShuffleArray() { } + /** * This method shuffles an array using the Fisher-Yates algorithm. * @@ -32,15 +35,4 @@ public static void shuffle(int[] arr) { arr[j] = temp; } } - /** - * This method prints the elements of the array. - * - * @param arr is the input array to be printed - */ - public static void printArray(int[] arr) { - for (int num : arr) { - System.out.print(num + " "); - } - System.out.println(); - } } diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java deleted file mode 100644 index 40a5441fa7a0..000000000000 --- a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.thealgorithms.slidingwindow; -/** - * The Sliding Window algorithm is used to find the minimum sum of a subarray - * of a fixed size k within a given array. - * - *

- * Worst-case performance O(n) - * Best-case performance O(n) - * Average performance O(n) - * Worst-case space complexity O(1) - * - * This class provides a static method to find the minimum sum of a subarray - * with a specified length k. - * - * @author Rashi Dashore (https://github.com/rashi07dashore) - */ -public final class MinSumKSizeSubarray { - - // Prevent instantiation - private MinSumKSizeSubarray() { - } - - /** - * This method finds the minimum sum of a subarray of a given size k. - * - * @param arr is the input array where the minimum sum needs to be found - * @param k is the size of the subarray - * @return the minimum sum of the subarray of size k - */ - public static int minSumKSizeSubarray(int[] arr, int k) { - if (arr.length < k) { - return -1; // Edge case: not enough elements - } - - int minSum; - int windowSum = 0; - - // Calculate the sum of the first window - for (int i = 0; i < k; i++) { - windowSum += arr[i]; - } - minSum = windowSum; - - // Slide the window across the array - for (int i = k; i < arr.length; i++) { - windowSum += arr[i] - arr[i - k]; - minSum = Math.min(minSum, windowSum); - } - return minSum; - } -} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy