Posts

Showing posts with the label Java

✅ Topics for Day 6 DSA with Java

Focus : Two Pointer Technique 🔁 1. Learn & Implement Two Pointer Technique Understand how the two-pointer technique works on sorted arrays. Implement the following problems: // Java Example: Two Sum (Sorted Array) public int [] twoSum( int [] numbers, int target) { int left = 0 , right = numbers.length - 1 ; while (left < right) { int sum = numbers[left] + numbers[right]; if (sum == target) { return new int [] { left + 1 , right + 1 }; // 1-indexed } else if (sum < target) { left++; } else { right--; } } return new int [] {}; // no solution } 🧠 2. Solve 2 LeetCode Problems Using Two Pointers LeetCode 167. Two Sum II – Input Array Is Sorted LeetCode 283. Move Zeroes Optimize with minimal swaps using the two-pointer pattern. ✨ Bonus Challenges (Optional but Recommended) LeetCode 26. Remove Duplicates from Sorted Array LeetCode 844....

✅ Day 5 Solution - DSA with Java

  ✅ 1. Implement Merge Sort and Quick Sort 🔹 Merge Sort public class MergeSort { public static void mergeSort ( int [] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2 ; mergeSort(arr, left, mid); mergeSort(arr, mid + 1 , right); merge(arr, left, mid, right); } } public static void merge ( int [] arr, int left, int mid, int right) { int n1 = mid - left + 1 , n2 = right - mid; int [] L = new int [n1]; int [] R = new int [n2]; for ( int i = 0 ; i < n1; i++) L[i] = arr[left + i]; for ( int j = 0 ; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0 , j = 0 , k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) arr[k++] = L[i++]; else arr[k++] = R[j++]; } while (i < n1) arr[k++] = L[i++]; while (j < n2) ...

🚀 Day 5: Merge Sort & Quick Sort in Java 🎯 Goals for Today

Understand the Divide and Conquer strategy Learn and implement: Merge Sort Quick Sort Analyze Time & Space Complexities Solve intermediate-level sorting problems 📘 1. Merge Sort A stable, divide-and-conquer sorting algorithm. Time Complexity : O(n log n) in all cases Space Complexity : O(n) (extra array) ✅ Logic: Divide the array into two halves Sort each half recursively Merge the two sorted halves 🔹 Java Implementation public class MergeSort { public static void mergeSort ( int [] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2 ; // Sort the left and right halves mergeSort(arr, left, mid); mergeSort(arr, mid + 1 , right); // Merge them merge(arr, left, mid, right); } } public static void merge ( int [] arr, int left, int mid, int right) { int n1 = mid - left + 1 ; ...

✅ Day 4 Solution - DSA with Java

  ✅ 1. Implement All Three Sorting Algorithms 🔹 Bubble Sort public class BubbleSort { public static void bubbleSort ( int [] arr) { int n = arr.length; for ( int i = 0 ; i < n - 1 ; i++) { boolean swapped = false ; for ( int j = 0 ; j < n - i - 1 ; j++) { if (arr[j] > arr[j + 1 ]) { // Swap int temp = arr[j]; arr[j] = arr[j + 1 ]; arr[j + 1 ] = temp; swapped = true ; } } if (!swapped) break ; // Optimization } } } 🔹 Selection Sort public class SelectionSort { public static void selectionSort ( int [] arr) { int n = arr.length; for ( int i = 0 ; i < n - 1 ; i++) { int minIdx = i; for ( int j = i + 1 ; j < n; j++) { if (arr[j] < arr[minIdx]) { ...

✅ Day 4: Sorting Algorithms (Basic to Intermediate)

🎯 Goals for Today Understand how sorting works and why it’s needed Learn and implement: Bubble Sort Selection Sort Insertion Sort Analyze time & space complexities Practice beginner sorting problems 📘 1. Why Sorting Matters Sorting helps: Optimize searching (e.g., binary search) Solve problems like duplicates, frequency, etc. Prepare data for algorithms like two pointers , greedy , divide & conquer , etc. 🔁 2. Sorting Algorithms 🔹 Bubble Sort Repeatedly swap adjacent elements if they are in the wrong order. public class BubbleSort { public static void bubbleSort ( int [] arr) { int n = arr.length; for ( int i = 0 ; i < n - 1 ; i++) { boolean swapped = false ; for ( int j = 0 ; j < n - i - 1 ; j++) { if (arr[j] > arr[j + 1 ]) { // swap int temp = arr[j]; arr[j] = arr[j + 1 ]; ...

✅ Solution Implement Both Linear and Binary Search Day 3 DSA with Java

🔹 Linear Search in Java public class LinearSearch { public static int linearSearch ( int [] arr, int key) { for ( int i = 0 ; i < arr.length; i++) { if (arr[i] == key) return i; } return - 1 ; // Not found } public static void main (String[] args) { int [] arr = { 5 , 3 , 8 , 2 , 9 }; int key = 8 ; int index = linearSearch(arr, key); System.out.println( "Element found at index: " + index); } } 🔹 Binary Search in Java (Iterative) public class BinarySearch { public static int binarySearch ( int [] arr, int key) { int start = 0 , end = arr.length - 1 ; while (start <= end) { int mid = start + (end - start) / 2 ; if (arr[mid] == key) return mid; else if (arr[mid] < key) start = mid + 1 ; else end = mid - 1 ; } return - 1 ; } public static voi...

🚀 Day 3: Searching Algorithms in Java

🎯 Goals: Understand Linear Search and Binary Search Learn when and how to use each Solve real interview problems using searching techniques 🔍 1. Linear Search 📘 Definition: Linearly checks each element one by one. Used when the array is unsorted . ✅ Code Example: public static int linearSearch ( int [] arr, int key) { for ( int i = 0 ; i < arr.length; i++) { if (arr[i] == key) return i; } return - 1 ; // Not found } 🧠 2. Binary Search 📘 Definition: Used on sorted arrays . It divides the search space in half each time — O(log N) time complexity. ✅ Code (Iterative Approach): public static int binarySearch ( int [] arr, int key) { int start = 0 , end = arr.length - 1 ; while (start <= end) { int mid = start + (end - start) / 2 ; if (arr[mid] == key) return mid; else if (arr[mid] < key) start = mid + 1 ; else end = mid - 1 ; } return - 1 ; } ✅ Code (Recu...