diff --git a/Java/KadanesAlgorithm.java b/Java/KadanesAlgorithm.java new file mode 100644 index 0000000..4b86615 --- /dev/null +++ b/Java/KadanesAlgorithm.java @@ -0,0 +1,39 @@ +public class KadanesAlgorithm { + + /** + * Kadane's Algorithm to find the maximum sum of a contiguous subarray. + * Problem (saloni-jaiswal-dev#358) + * + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * Test Cases: + * Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4] Expected: 6 + * Input: [1] Expected: 1 + * Input: [5, 4, -1, 7, 8] Expected: 23 + * Input: [-1, -2, -3, -4] Expected: -1 + */ + public static int maxSubArray(int[] nums) { + if (nums == null || nums.length == 0) { + throw new IllegalArgumentException("Input array must not be null or empty."); + } + int maxSoFar = nums[0]; + int currentMax = nums[0]; + for (int i = 1; i < nums.length; i++) { + currentMax = Math.max(nums[i], currentMax + nums[i]); + maxSoFar = Math.max(maxSoFar, currentMax); + } + return maxSoFar; + } + + public static void main(String[] args) { + int[] nums1 = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; + System.out.println("Test 1: " + maxSubArray(nums1)); // 6 + int[] nums2 = {1}; + System.out.println("Test 2: " + maxSubArray(nums2)); // 1 + int[] nums3 = {5, 4, -1, 7, 8}; + System.out.println("Test 3: " + maxSubArray(nums3)); // 23 + int[] nums4 = {-1, -2, -3, -4}; + System.out.println("Test 4: " + maxSubArray(nums4)); // -1 + } +} diff --git a/Java/LongestCommonPrefix.java b/Java/LongestCommonPrefix.java new file mode 100644 index 0000000..90d2080 --- /dev/null +++ b/Java/LongestCommonPrefix.java @@ -0,0 +1,55 @@ +/** + * Longest Common Prefix + * + * Problem: Write a function to find the longest common prefix string + * amongst an array of strings. If there is no common prefix, return "". + * + * Approach: Horizontal scanning - compare prefix with each string + * Time Complexity: O(S) where S = sum of all characters in all strings + * Space Complexity: O(1) + */ +public class LongestCommonPrefix { + + public static String longestCommonPrefix(String[] strs) { + if (strs == null || strs.length == 0) { + return ""; + } + + // Start with the first string as the initial prefix + String prefix = strs[0]; + + for (int i = 1; i < strs.length; i++) { + // Reduce the prefix until it matches the start of strs[i] + while (!strs[i].startsWith(prefix)) { + prefix = prefix.substring(0, prefix.length() - 1); + if (prefix.isEmpty()) { + return ""; + } + } + } + + return prefix; + } + + public static void main(String[] args) { + // Test Case 1: Common prefix "fl" + String[] strs1 = {"flower", "flow", "flight"}; + System.out.println("Test 1: " + longestCommonPrefix(strs1)); // Expected: "fl" + + // Test Case 2: No common prefix + String[] strs2 = {"dog", "racecar", "car"}; + System.out.println("Test 2: " + longestCommonPrefix(strs2)); // Expected: "" + + // Test Case 3: All strings are the same + String[] strs3 = {"abc", "abc", "abc"}; + System.out.println("Test 3: " + longestCommonPrefix(strs3)); // Expected: "abc" + + // Test Case 4: Single string + String[] strs4 = {"alone"}; + System.out.println("Test 4: " + longestCommonPrefix(strs4)); // Expected: "alone" + + // Test Case 5: Empty string in array + String[] strs5 = {"", "b"}; + System.out.println("Test 5: " + longestCommonPrefix(strs5)); // Expected: "" + } +}