Session 18: HashMap & Two Pointers
Original Session
Date: 15 Dec 2024
Topic: Two Pointers
Concept: Hash Map
Concept: Two Pointers
Concept:
consider start, i and j index in array.
sum from start to i = Sum
sum of start to j = Sum
which means sum from i to j = 0;
HomeWork: https://leetcode.com/problems/minimum-area-rectangle/description/
Programs
- Number of occurrence
- GFG here
int countFreq(int[] arr, int target) { // code here Map<Integer, Integer> map = new HashMap(); for (int value : arr) { map.put(value, map.getOrDefault(value, 0) + 1); } return map.getOrDefault(target, 0); }
- Print frequency of each occurrence
- Not on any portal
void freq(int[] arr) { // code here Map<Integer, Integer> map = new HashMap(); for (int value : arr) { map.put(value, map.getOrDefault(value, 0) + 1); } for (Map.Entry<Integer, Integer> entry: map.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } }
- Array Subset
- GFG here
Map<Integer, Integer> map = new HashMap(); for (int value: a) { map.put(value, map.getOrDefault(value, 0) + 1); } for (int value: b) { if (!map.containsKey(value)) { return false; } int occurence = map.get(value); if (occurence == 0) { return false; } map.put(value, occurence - 1); } return true;
- Valid Anagram
- leetocode here
public boolean isAnagram(String s, String t) { if (s.length() <= 0 || t.length() <= 0) { return false; } if (s.length() != t.length()) { return false; } Map<Character, Integer> hashMap = new HashMap(); for (Character ch: s.toCharArray()) { if (!hashMap.containsKey(ch)) { hashMap.put(ch, 1); continue; } hashMap.put(ch, hashMap.get(ch) + 1); } for (Character ch: t.toCharArray()) { if (!hashMap.containsKey(ch)) { return false; } int newCount = hashMap.get(ch) - 1; if (newCount <= 0) { hashMap.remove(ch); continue; } hashMap.put(ch, newCount); } return true; }
- Subarray with 0 sum
- GFG here
boolean findsum(int arr[]) { // Your code here int sum = 0; int ans = 0; Map<Integer, Integer> map = new HashMap(); for (int i=0; i< arr.length; i++) { sum += arr[i]; if (sum == 0) { ans = i + 1; continue; } if (!map.containsKey(sum)) { map.put(sum, i); continue; } ans = Math.max(ans, i-map.get(sum)); } return ans > 0; }
- Largest subarray with 0 sum
- GFG here
int maxLen(int arr[]) { int sum = 0; int ans = 0; Map<Integer, Integer> map = new HashMap(); for (int i=0; i< arr.length; i++) { sum += arr[i]; if (sum == 0) { ans = i + 1; continue; } if (!map.containsKey(sum)) { map.put(sum, i); continue; } ans = Math.max(ans, i-map.get(sum)); } return ans; }
- Subarrays with sum K
- GFG here
public int findSubArraySum(int k, int nums[]) { int sum = 0, ans = 0; HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, 1); for (int i = 0; i < nums.length; i++) { sum += nums[i]; if (map.containsKey(sum - k)) { ans += map.get(sum - k); } map.put(sum, map.getOrDefault(sum, 0) + 1); } return ans; }
- Subarray Sum Equals K
- leetocode here
public int subarraySum(int[] nums, int k) { Map<Integer, Integer> map = new HashMap(); int sum = 0, ans = 0; map.put(sum , 1); for (int i=0; i< nums.length; i++) { sum += nums[i]; if (map.containsKey(sum-k)) { ans += map.get(sum-k); } map.put(sum, map.getOrDefault(sum, 0) + 1); } return ans; }
- 525. Contiguous Array
- leetocode here
public int findMaxLength(int[] arr) { int sum = 0; int ans = 0; Map<Integer, Integer> map = new HashMap(); for (int i=0; i< arr.length; i++) { sum += arr[i] ==0 ? -1 : arr[i]; if (sum == 0) { ans = i + 1; continue; } if (!map.containsKey(sum)) { map.put(sum, i); continue; } ans = Math.max(ans, i-map.get(sum)); } return ans; }