View on GitHub

SCRATCH PAD

No polish, just pulse

Striver’s SDE Sheet Notes (Day 4: Arrays & Hashing)


1. Two Sum

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> numMap = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (numMap.containsKey(complement)) {
            return new int[] { numMap.get(complement), i };
        }
        numMap.put(nums[i], i);
    }
    return new int[0];
}


2. 4-Sum

public List<List<Integer>> fourSum(int[] nums, int target) {
    Arrays.sort(nums);
    List<List<Integer>> result = new ArrayList<>();

    for (int i = 0; i < nums.length - 3; i++) {
        if (i > 0 && nums[i] == nums[i - 1]) continue;
        for (int j = i + 1; j < nums.length - 2; j++) {
            if (j > i + 1 && nums[j] == nums[j - 1]) continue;
            int left = j + 1, right = nums.length - 1;
            while (left < right) {
                int sum = nums[i] + nums[j] + nums[left] + nums[right];
                if (sum == target) {
                    result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left++; right--;
                } else if (sum < target) {
                    left++;
                } else {
                    right--;
                }
            }
        }
    }
    return result;
}


3. Longest Consecutive Sequence

public int longestConsecutive(int[] nums) {
    Set<Integer> numSet = new HashSet<>();
    for (int num : nums) {
        numSet.add(num);
    }

    int maxLength = 0;
    for (int num : numSet) {
        if (!numSet.contains(num - 1)) {
            int currentNum = num;
            int length = 1;

            while (numSet.contains(currentNum + 1)) {
                currentNum++;
                length++;
            }
            maxLength = Math.max(maxLength, length);
        }
    }

    return maxLength;
}


4. Largest Subarray with K Sum

public int maxSubArrayLen(int[] nums, int k) {
    Map<Integer, Integer> prefixSumMap = new HashMap<>();
    int maxLength = 0, sum = 0;

    for (int i = 0; i < nums.length; i++) {
        sum += nums[i];

        if (sum == k) {
            maxLength = i + 1;
        }
        if (prefixSumMap.containsKey(sum - k)) {
            maxLength = Math.max(maxLength, i - prefixSumMap.get(sum - k));
        }
        prefixSumMap.putIfAbsent(sum, i);
    }

    return maxLength;
}


5. Count Subarrays with Given XOR

public int subarraysWithXor(int[] nums, int target) {
    Map<Integer, Integer> prefixXorMap = new HashMap<>();
    int xor = 0, count = 0;

    for (int num : nums) {
        xor ^= num;
        if (xor == target) count++;
        if (prefixXorMap.containsKey(xor ^ target)) {
            count += prefixXorMap.get(xor ^ target);
        }
        prefixXorMap.put(xor, prefixXorMap.getOrDefault(xor, 0) + 1);
    }

    return count;
}


6. Longest Substring Without Repeating Characters

public int lengthOfLongestSubstring(String s) {
    int n = s.length(), maxLength = 0;
    Map<Character, Integer> charIndexMap = new HashMap<>();
    int left = 0;

    for (int right = 0; right < n; right++) {
        if (charIndexMap.containsKey(s.charAt(right))) {
            left = Math.max(charIndexMap.get(s.charAt(right)) + 1, left);
        }
        charIndexMap.put(s.charAt(right), right);
        maxLength = Math.max(maxLength, right - left + 1);
    }

    return maxLength;
}