0% found this document useful (0 votes)
24 views1 page

DeltaX Round2 Java Codes

Uploaded by

roopasree004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views1 page

DeltaX Round2 Java Codes

Uploaded by

roopasree004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

DeltaX Round 2 – Java Coding Templates

// DeltaX Round 2 – Online Coding Problems (Java Templates) import java.util.*; //


1. Two Sum class TwoSum { public int[] twoSum(int[] nums, int target) { Map<Integer,
Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int diff =
target - nums[i]; if (map.containsKey(diff)) { return new int[]{map.get(diff), i}; }
map.put(nums[i], i); } return new int[]{}; } } // 2. Valid Parentheses class
ValidParentheses { public boolean isValid(String s) { Stack<Character> stack = new
Stack<>(); Map<Character, Character> pairs = Map.of(')', '(', ']', '[', '}', '{');
for (char c : s.toCharArray()) { if (pairs.containsValue(c)) stack.push(c); else if
(stack.isEmpty() || stack.pop() != pairs.get(c)) return false; } return
stack.isEmpty(); } } // 3. Merge Two Sorted Lists class ListNode { int val; ListNode
next; ListNode(int x) { val = x; } } class MergeTwoLists { public ListNode
mergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0), tail =
dummy; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { tail.next = l1; l1
= l1.next; } else { tail.next = l2; l2 = l2.next; } tail = tail.next; } tail.next =
(l1 != null) ? l1 : l2; return dummy.next; } } // 4. Remove Duplicates from Sorted
Array class RemoveDuplicates { public int removeDuplicates(int[] nums) { if
(nums.length == 0) return 0; int w = 1; for (int i = 1; i < nums.length; i++) { if
(nums[i] != nums[w-1]) nums[w++] = nums[i]; } return w; } } // 5. Maximum Subarray
(Kadane) class MaxSubArray { public int maxSubArray(int[] nums) { int cur = nums[0],
best = nums[0]; for (int i = 1; i < nums.length; i++) { cur = Math.max(nums[i], cur
+ nums[i]); best = Math.max(best, cur); } return best; } } // 6. Longest Substring
Without Repeating Characters class LongestSubstring { public int
lengthOfLongestSubstring(String s) { Map<Character, Integer> last = new HashMap<>();
int start = 0, best = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i);
if (last.containsKey(c) && last.get(c) >= start) { start = last.get(c) + 1; }
last.put(c, i); best = Math.max(best, i - start + 1); } return best; } } // 7. Kth
Largest Element in Array (Quickselect) class KthLargest { public int
findKthLargest(int[] nums, int k) { return quickSelect(nums, 0, nums.length-1,
nums.length-k); } private int quickSelect(int[] a, int l, int r, int k) { if (l ==
r) return a[l]; int pivot = a[(l+r)/2], i = l, j = r; while (i <= j) { while (a[i] <
pivot) i++; while (a[j] > pivot) j--; if (i <= j) { int tmp=a[i]; a[i]=a[j];
a[j]=tmp; i++; j--; } } if (k <= j) return quickSelect(a, l, j, k); if (k >= i)
return quickSelect(a, i, r, k); return a[k]; } } // 8. Top K Frequent Elements class
TopKFrequent { public int[] topKFrequent(int[] nums, int k) { Map<Integer,Integer>
freq = new HashMap<>(); for (int n : nums) freq.put(n, freq.getOrDefault(n,0)+1);
List<Integer>[] buckets = new ArrayList[nums.length+1]; for (int n: freq.keySet()) {
int f = freq.get(n); if (buckets[f]==null) buckets[f]=new ArrayList<>();
buckets[f].add(n); } int[] ans = new int[k]; int idx=0; for (int
i=buckets.length-1;i>=0 && idx<k;i--){ if (buckets[i]!=null){ for (int
n:buckets[i]){ ans[idx++]=n; if (idx==k) return ans; } } } return ans; } } // 9.
Group Anagrams class GroupAnagrams { public List<List<String>>
groupAnagrams(String[] strs) { Map<String,List<String>> map = new HashMap<>(); for
(String s : strs){ char[] arr = s.toCharArray(); Arrays.sort(arr); String key = new
String(arr); map.computeIfAbsent(key,k->new ArrayList<>()).add(s); } return new
ArrayList<>(map.values()); } } // 10. Find Peak Element class PeakElement { public
int findPeakElement(int[] nums) { int l=0,r=nums.length-1; while(l<r){ int
m=(l+r)/2; if(nums[m]<nums[m+1]) l=m+1; else r=m; } return l; } } // ... Continue
similarly for problems 11–20

You might also like