From 0ea655872436e809d563c8861dcb6fdd23301c8f Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Tue, 1 Oct 2024 13:38:55 +0000 Subject: [PATCH 01/27] Added LongestArthmeticSubsequence --- .../LongestArthmeticSubsequence.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/LongestArthmeticSubsequence.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArthmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArthmeticSubsequence.java new file mode 100644 index 000000000000..1f380a08e497 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArthmeticSubsequence.java @@ -0,0 +1,58 @@ +package com.thealgorithms.dynamicprogramming; + +import java.util.HashMap; + +final class LongestArithmeticSubsequence { + + private LongestArithmeticSubsequence() { + } + + /** + * Returns the length of the longest arithmetic subsequence in the given array. + * + * A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value + * (for 0 <= i < seq.length - 1). + * + * @param nums the input array of integers + * @return the length of the longest arithmetic subsequence + */ + public static int getLongestArithmeticSubsequenceLength(int[] nums) { + // If the array is empty or has only one element, return its length. + if (nums == null || nums.length == 0) { + return 0; + } + + if (nums.length == 1) { + return 1; + } + + int n = nums.length; + HashMap[] dp = new HashMap[n]; + int maxLength = 2; + + // Initialize dp array + for (int i = 0; i < n; i++) { + dp[i] = new HashMap<>(); + for (int j = 0; j < i; j++) { + int diff = nums[i] - nums[j]; + dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1); + maxLength = Math.max(maxLength, dp[i].get(diff)); + } + } + + return maxLength; + } + + public static void main(String[] args) { + int[] nums = {3, 6, 9, 12, 15}; + int length = getLongestArithmeticSubsequenceLength(nums); + + // Print the result + System.out.println("Array: "); + for (int num : nums) { + System.out.print(num + " "); + } + System.out.println(); + System.out.println("Length of the Longest Arithmetic Subsequence: " + length); + } +} From 45cedb7323cf1c9eb62debbd7fada736f471a111 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Tue, 1 Oct 2024 13:59:11 +0000 Subject: [PATCH 02/27] Renamed the file --- ...rthmeticSubsequence.java => LongestArithmeticSubsequence.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/com/thealgorithms/dynamicprogramming/{LongestArthmeticSubsequence.java => LongestArithmeticSubsequence.java} (100%) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArthmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java similarity index 100% rename from src/main/java/com/thealgorithms/dynamicprogramming/LongestArthmeticSubsequence.java rename to src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java From 970008f3ac1277c0542693b1dc79e0dfad13b18c Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 08:58:29 +0000 Subject: [PATCH 03/27] removed main method from the file --- .../LongestArithmeticSubsequence.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index 1f380a08e497..b61a4249ae33 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -42,17 +42,4 @@ public static int getLongestArithmeticSubsequenceLength(int[] nums) { return maxLength; } - - public static void main(String[] args) { - int[] nums = {3, 6, 9, 12, 15}; - int length = getLongestArithmeticSubsequenceLength(nums); - - // Print the result - System.out.println("Array: "); - for (int num : nums) { - System.out.print(num + " "); - } - System.out.println(); - System.out.println("Length of the Longest Arithmetic Subsequence: " + length); - } } From 0fff2733daf6554093598e74785edc3ffe714a41 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 09:03:04 +0000 Subject: [PATCH 04/27] Added Parameterized tests --- .../LongestArithmeticSubsequenceTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java new file mode 100644 index 000000000000..5b879e517306 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.dynamicprogramming; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class LongestArithmeticSubsequenceTest { + + @ParameterizedTest + @MethodSource("provideTestCases") + void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) { + assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums)); + } + + private static Stream provideTestCases() { + return Stream.of( + Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), + Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), + Arguments.of(new int[] {1, 2, 3, 4}, 4), + Arguments.of(new int[] {}, 0), // Edge case: empty array + Arguments.of(new int[] {10}, 1), // Edge case: single element + Arguments.of(new int[] {9, 4, 7, 2, 10}, 3) // Random test case + ); + } +} From f4655500349e9475b1a168dde011bd03114afc5e Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 09:08:39 +0000 Subject: [PATCH 05/27] formatted tests arguments --- .../LongestArithmeticSubsequenceTest.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 5b879e517306..7123cacd7116 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -16,13 +16,7 @@ void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) { } private static Stream provideTestCases() { - return Stream.of( - Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), - Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), - Arguments.of(new int[] {1, 2, 3, 4}, 4), - Arguments.of(new int[] {}, 0), // Edge case: empty array - Arguments.of(new int[] {10}, 1), // Edge case: single element - Arguments.of(new int[] {9, 4, 7, 2, 10}, 3) // Random test case - ); + return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), + Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3)); } } From 65c2f588b94701ec7e3cc3bf2f92810925b6e79a Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 09:11:01 +0000 Subject: [PATCH 06/27] clang format changes --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 7123cacd7116..b35ef24b1d7c 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -16,7 +16,6 @@ void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) { } private static Stream provideTestCases() { - return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), - Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3)); + return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3)); } } From bd24402d2c47668ebc30dbfec5661af37067b5fc Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:11:48 +0000 Subject: [PATCH 07/27] Added exception for null case --- .../LongestArithmeticSubsequence.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index b61a4249ae33..b03b11a46d64 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -17,24 +17,24 @@ private LongestArithmeticSubsequence() { * @return the length of the longest arithmetic subsequence */ public static int getLongestArithmeticSubsequenceLength(int[] nums) { - // If the array is empty or has only one element, return its length. - if (nums == null || nums.length == 0) { - return 0; + // Throws an exception if nums is null. + if (nums == null) { + throw new IllegalArgumentException("Input array cannot be null"); } - - if (nums.length == 1) { - return 1; + + // If the array is empty or has only one element, return its length. + if (nums.length == 1 || nums.length == 0) { + return nums.length; } - int n = nums.length; - HashMap[] dp = new HashMap[n]; + HashMap[] dp = new HashMap[nums.length]; int maxLength = 2; // Initialize dp array - for (int i = 0; i < n; i++) { + for (int i = 0; i < nums.length; i++) { dp[i] = new HashMap<>(); for (int j = 0; j < i; j++) { - int diff = nums[i] - nums[j]; + final int diff = nums[i] - nums[j]; dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1); maxLength = Math.max(maxLength, dp[i].get(diff)); } From eded33dfd4519eca537d14e3ecdad00450f551b2 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:13:01 +0000 Subject: [PATCH 08/27] Added test for null case --- .../LongestArithmeticSubsequenceTest.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index b35ef24b1d7c..4598abeaa2a6 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -1,21 +1,30 @@ package com.thealgorithms.dynamicprogramming; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.apache.commons.lang3.ArrayUtils; public class LongestArithmeticSubsequenceTest { @ParameterizedTest @MethodSource("provideTestCases") - void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) { + void testGetLongestArithmeticSubsequenceLengthReversedInput(int[] nums, int expected) { + ArrayUtils.reverse(nums); assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums)); } + @ParameterizedTest + void testNullInput() { + // Verify that an IllegalArgumentException is thrown when nums is null + assertThrows(IllegalArgumentException.class, () -> LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(null)); + } + private static Stream provideTestCases() { - return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3)); + return Stream.of(Arguments.of(new int[]{3, 6, 9, 12, 15}, 5), Arguments.of(new int[]{1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[]{1, 2, 3, 4}, 4), Arguments.of(new int[]{}, 0), Arguments.of(new int[]{10}, 1), Arguments.of(new int[]{9, 4, 7, 2, 10}, 3)); } } From f6cd84318828b6b9b1268ca659316210ccd2bfef Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:17:01 +0000 Subject: [PATCH 09/27] clang format changes done --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 4598abeaa2a6..007cc70c4aa2 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -4,10 +4,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.stream.Stream; +import org.apache.commons.lang3.ArrayUtils; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.apache.commons.lang3.ArrayUtils; + public class LongestArithmeticSubsequenceTest { @@ -25,6 +26,6 @@ void testNullInput() { } private static Stream provideTestCases() { - return Stream.of(Arguments.of(new int[]{3, 6, 9, 12, 15}, 5), Arguments.of(new int[]{1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[]{1, 2, 3, 4}, 4), Arguments.of(new int[]{}, 0), Arguments.of(new int[]{10}, 1), Arguments.of(new int[]{9, 4, 7, 2, 10}, 3)); + return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3)); } } From 0a51008f497f98d16b5f9101c0dd042c294e98d6 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:19:27 +0000 Subject: [PATCH 10/27] Check fixes --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 007cc70c4aa2..ae3e66ff9515 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -9,7 +9,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; - public class LongestArithmeticSubsequenceTest { @ParameterizedTest From e1a1fb49a3056a28a01e737ad1a1a81790f25f81 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:22:13 +0000 Subject: [PATCH 11/27] clang format fixes --- .../LongestArithmeticSubsequence.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index b03b11a46d64..ccbb3fea928c 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -21,10 +21,14 @@ public static int getLongestArithmeticSubsequenceLength(int[] nums) { if (nums == null) { throw new IllegalArgumentException("Input array cannot be null"); } - + // If the array is empty or has only one element, return its length. - if (nums.length == 1 || nums.length == 0) { - return nums.length; + if (nums.length == 0) { + return 0; + } + + if (nums.length == 1){ + return 1; } HashMap[] dp = new HashMap[nums.length]; From 2b3ea126ab3deaaa2e72a5610098ac579a64b708 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:23:23 +0000 Subject: [PATCH 12/27] fixes --- .../dynamicprogramming/LongestArithmeticSubsequence.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index ccbb3fea928c..68adc619183b 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -27,7 +27,7 @@ public static int getLongestArithmeticSubsequenceLength(int[] nums) { return 0; } - if (nums.length == 1){ + if (nums.length == 1) { return 1; } From 79cf9cdcb7df341f35ccbf462d2bf795995e3711 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:30:30 +0000 Subject: [PATCH 13/27] buildfailure fixes --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index ae3e66ff9515..6ec79e25e2dd 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -18,7 +18,7 @@ void testGetLongestArithmeticSubsequenceLengthReversedInput(int[] nums, int expe assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums)); } - @ParameterizedTest + @Test void testNullInput() { // Verify that an IllegalArgumentException is thrown when nums is null assertThrows(IllegalArgumentException.class, () -> LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(null)); From d103e846ee2f586c72c5c30389011a0036416aa3 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:36:02 +0000 Subject: [PATCH 14/27] imported test class from junit --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 6ec79e25e2dd..0cd7d809f83b 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.stream.Stream; +import org.junit.jupiter.api.Test; import org.apache.commons.lang3.ArrayUtils; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; From 47cccfef333cafe61ff89882cab5ccee9d65fa81 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:37:19 +0000 Subject: [PATCH 15/27] clang format changes --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 0cd7d809f83b..7dad252bb0be 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -4,8 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.stream.Stream; -import org.junit.jupiter.api.Test; import org.apache.commons.lang3.ArrayUtils; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; From 7cb9fd9576d9f8750c448195813d720e61ab78a8 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:25:05 +0530 Subject: [PATCH 16/27] Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../dynamicprogramming/LongestArithmeticSubsequence.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index 68adc619183b..545c7f9ab373 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -22,7 +22,6 @@ public static int getLongestArithmeticSubsequenceLength(int[] nums) { throw new IllegalArgumentException("Input array cannot be null"); } - // If the array is empty or has only one element, return its length. if (nums.length == 0) { return 0; } From 3be82b82470f923ca445596214242ba6ad93a804 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:25:15 +0530 Subject: [PATCH 17/27] Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../dynamicprogramming/LongestArithmeticSubsequence.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index 545c7f9ab373..e57368214ee4 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -17,7 +17,6 @@ private LongestArithmeticSubsequence() { * @return the length of the longest arithmetic subsequence */ public static int getLongestArithmeticSubsequenceLength(int[] nums) { - // Throws an exception if nums is null. if (nums == null) { throw new IllegalArgumentException("Input array cannot be null"); } From 8657b3a724978a200e137197950c8abfbfb9955a Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:59:43 +0000 Subject: [PATCH 18/27] Added the original tests --- .../LongestArithmeticSubsequenceTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 7dad252bb0be..8df175812d22 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -11,7 +11,12 @@ import org.junit.jupiter.params.provider.MethodSource; public class LongestArithmeticSubsequenceTest { - + @ParameterizedTest + @MethodSource("provideTestCases") + void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) { + assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums)); + } + @ParameterizedTest @MethodSource("provideTestCases") void testGetLongestArithmeticSubsequenceLengthReversedInput(int[] nums, int expected) { From 677bbad78fb7f12c92becd5abce4bb99a9355ebd Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:13:10 +0000 Subject: [PATCH 19/27] clang fixes --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 8df175812d22..09a6b9658f10 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -16,7 +16,6 @@ public class LongestArithmeticSubsequenceTest { void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) { assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums)); } - @ParameterizedTest @MethodSource("provideTestCases") void testGetLongestArithmeticSubsequenceLengthReversedInput(int[] nums, int expected) { From 85a850b7e0160af33262d6a5f5730c3aeb81a1de Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:57:11 +0530 Subject: [PATCH 20/27] Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../dynamicprogramming/LongestArithmeticSubsequence.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index e57368214ee4..7a1fbb7d0f7a 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -21,12 +21,8 @@ public static int getLongestArithmeticSubsequenceLength(int[] nums) { throw new IllegalArgumentException("Input array cannot be null"); } - if (nums.length == 0) { - return 0; - } - - if (nums.length == 1) { - return 1; + if (nums.length <= 1) { + return nums.length; } HashMap[] dp = new HashMap[nums.length]; From b93f734841dea4375f25b9570a4ee1ec7b41332d Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:57:36 +0530 Subject: [PATCH 21/27] Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../dynamicprogramming/LongestArithmeticSubsequence.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index 7a1fbb7d0f7a..7edd7411bc6b 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -28,7 +28,7 @@ public static int getLongestArithmeticSubsequenceLength(int[] nums) { HashMap[] dp = new HashMap[nums.length]; int maxLength = 2; - // Initialize dp array + // fill the dp array for (int i = 0; i < nums.length; i++) { dp[i] = new HashMap<>(); for (int j = 0; j < i; j++) { From 09517279d415c233f344a31e285163363fbc59ba Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:57:58 +0530 Subject: [PATCH 22/27] Update src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 09a6b9658f10..d95f3baa8653 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -24,8 +24,7 @@ void testGetLongestArithmeticSubsequenceLengthReversedInput(int[] nums, int expe } @Test - void testNullInput() { - // Verify that an IllegalArgumentException is thrown when nums is null + void testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() { assertThrows(IllegalArgumentException.class, () -> LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(null)); } From 225307e3c3436a8c0f310c0ac821cd9b67b77733 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:58:07 +0530 Subject: [PATCH 23/27] Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../dynamicprogramming/LongestArithmeticSubsequence.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index 7edd7411bc6b..b5ac62b4674b 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -3,7 +3,6 @@ import java.util.HashMap; final class LongestArithmeticSubsequence { - private LongestArithmeticSubsequence() { } From d5c2fe30dc6a1f50b4d162c16d133f73cd787296 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 05:31:21 +0000 Subject: [PATCH 24/27] Added constant sequence test --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index d95f3baa8653..036736868e4a 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -29,6 +29,6 @@ void testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() { } private static Stream provideTestCases() { - return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3)); + return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3), Arguments.of(new int[] {1, 2, 2, 2, 2, 5}, 4)); } } From 70ba32e3d3247d7a2657d0bfd03f72e801ee2b55 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 05:33:54 +0000 Subject: [PATCH 25/27] clang fixes --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 036736868e4a..007c68275db0 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -29,6 +29,7 @@ void testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() { } private static Stream provideTestCases() { - return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3), Arguments.of(new int[] {1, 2, 2, 2, 2, 5}, 4)); + return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3), + Arguments.of(new int[] {1, 2, 2, 2, 2, 5}, 4)); } } From 539bca89c51882df59294a799e246770e007c3a2 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 05:35:16 +0000 Subject: [PATCH 26/27] clang fixes --- .../dynamicprogramming/LongestArithmeticSubsequenceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java index 007c68275db0..6384fe2afebe 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java @@ -30,6 +30,6 @@ void testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() { private static Stream provideTestCases() { return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3), - Arguments.of(new int[] {1, 2, 2, 2, 2, 5}, 4)); + Arguments.of(new int[] {1, 2, 2, 2, 2, 5}, 4)); } } From 0910171dfcfecfb18b609b93a2122adb829011b4 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 05:43:17 +0000 Subject: [PATCH 27/27] Trigger GitHub Actions