📘 DSA Journey — Day 24 Today’s focus: Prefix Sum for range queries. Problem solved: • Count Vowel Strings in Ranges (LeetCode 2559) Concepts used: • Prefix Sum • Efficient range query processing • String boundary checks Key takeaway: The goal is to count how many strings in a given range start and end with a vowel. A direct approach would check each query range individually, leading to higher time complexity. Instead, we preprocess using a prefix sum array: • Mark each word as 1 if it starts and ends with a vowel, else 0 • Build a prefix sum over this array For any query [l, r], the answer can be found in O(1) using: prefix[r] - prefix[l - 1] This reduces the overall complexity significantly when handling multiple queries. This problem highlights how prefix sum is extremely useful for repeated range-based queries. Continuing to strengthen fundamentals and consistency in DSA problem solving. #DSA #Java #LeetCode #CodingJourney
Prefix Sum for Efficient Range Queries in DSA
More Relevant Posts
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 74 of #100DaysOfLeetCode 💻✅ Solved #162. Find Peak Element problem in Java. Approach: • Used Binary Search technique to efficiently find the peak element • Set two pointers, left at start and right at end of the array • Calculated mid index using safe mid formula • Compared nums[mid] with nums[mid + 1] to determine direction • If mid element is smaller, moved search space to right half • Otherwise, moved search space to left half including mid • Continued until left and right pointers converged • Final position (left == right) represents the peak index Performance: ✓ Runtime: 0 ms (Beats 100.00% submissions) 🚀 ✓ Memory: 44.32 MB (Beats 25.49% submissions) Key Learning: ✓ Strengthened understanding of Binary Search on unsorted arrays ✓ Learned how to apply divide-and-conquer beyond traditional searching ✓ Improved intuition for peak finding using neighbor comparison ✓ Practiced optimizing search space instead of linear scanning Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 27 Today’s focus: Binary Search on modified arrays. Problem solved: • Search in Rotated Sorted Array (LeetCode 33) Concepts used: • Binary Search • Identifying sorted halves • Conditional search space reduction Key takeaway: This problem extends binary search to a rotated sorted array, where the array is not fully sorted but divided into two sorted parts. At each step, we: • Find the mid element • Check which half (left or right) is sorted • Decide whether the target lies in the sorted half • Eliminate the other half This allows us to still achieve O(log n) time complexity. Continuing to strengthen fundamentals and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 33 Today’s focus: Binary Search for next greater element. Problem solved: • Find Smallest Letter Greater Than Target (LeetCode 744) Concepts used: • Binary Search • Upper bound concept • Circular handling Key takeaway: The goal is to find the smallest character strictly greater than a given target in a sorted array. This is a classic upper bound problem: We use binary search to find the first element greater than the target. During search: • If letters[mid] > target, store it as a possible answer and move left • Else move right An important edge case: If no character is greater than the target, we return the first element (circular behavior). Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 DAY 94/150 — CONNECTING TREE LEVELS! 🌳🔗 Day 94 of my 150 Days DSA Challenge in Java and today I solved a very interesting problem that focuses on level connections in Binary Trees 💻🧠 📌 Problem Solved: Populating Next Right Pointers in Each Node 📌 LeetCode: #116 📌 Difficulty: Medium 🔹 Problem Insight The task is to connect each node’s next pointer to its right node on the same level. If there is no right node → set next = null. 👉 This problem is based on a perfect binary tree, which allows some optimizations. 🔹 Approaches Used ✅ Approach 1: BFS (Level Order Traversal) • Use a queue to traverse level by level • Connect nodes within the same level using a prev pointer ✔️ Easy to understand ❌ Uses extra space (queue) ✅ Approach 2: Optimized (Constant Space) • Use already established next pointers • Connect: left → right right → next.left ✔️ No extra space (O(1)) ✔️ More optimized and elegant ⏱ Complexity Time Complexity: O(n) Space Complexity: • BFS → O(n) • Optimized → O(1) 🧠 What I Learned • Same problem can have multiple approaches (basic → optimized) • Perfect binary tree structure helps reduce complexity • Using existing pointers smartly can eliminate extra space 💡 Key Takeaway This problem taught me: How to connect nodes level-wise Difference between BFS vs pointer-based optimization Writing space-efficient solutions 🌱 Learning Insight Now I’m focusing more on: 👉 Moving from brute-force → optimized solutions 🚀 ✅ Day 94 completed 🚀 56 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gDWj7K5Q 💡 Optimization is about using what you already have. #DSAChallenge #Java #LeetCode #BinaryTree #BFS #DFS #Pointers #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 3 🧩 Problem Solved: Combination Sum III (LeetCode #216) 💭 What I Learned: Used backtracking to find all possible combinations of k numbers (1–9) that sum up to a target n. At each step: ✔️ Decided whether to include the current number ✔️ Reduced both the remaining sum (n) and count (k) ✔️ Ensured each number is used only once by moving to the previous index Applied constraints effectively to prune recursion when: Remaining sum becomes negative Required count becomes invalid This improved my understanding of handling multiple constraints (sum + size) simultaneously in recursion. ⏱ Time Complexity: O(C(9,k) 🧠 Space Complexity: O(k) (recursion depth) ⚡ Key Takeaways: ✔️ Backtracking with multiple constraints requires careful pruning ✔️ Fixed input size can significantly reduce complexity ✔️ Choosing + skipping pattern helps explore all valid combinations 💻 Language Used: Java ☕ 📘 Concepts: Backtracking · Recursion · Combinations · Constraint Handling #CodeEveryday #DSA #LeetCode #Java #Backtracking #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟒 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the minimum element in a rotated sorted array using binary search. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Find Minimum in Rotated Sorted Array 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used binary search to reduce the search space • Compared the middle element with the rightmost element Logic: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) • Continued until left meets right 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Rotated arrays require a modified binary search • Comparing with boundary elements helps identify the sorted half • Binary search is not only for exact matches • Reducing the search space is the core idea 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(log n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Binary search is about asking the right question — not just finding the middle. 54 days consistent 🚀 On to Day 55. #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟵/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟰𝟰. 𝗙𝗶𝗻𝗱 𝗦𝗺𝗮𝗹𝗹𝗲𝘀𝘁 𝗟𝗲𝘁𝘁𝗲𝗿 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗧𝗮𝗿𝗴𝗲𝘁 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks easy — but the circular wrap-around is the trap most people miss. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Given a sorted circular array of letters, find the smallest letter strictly greater than the target. If none exists, wrap around to the first letter. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵: ✅ Standard binary search for the first letter > target ✅ If letters[mid] > target → go left (end = mid - 1) ✅ Else → go right (start = mid + 1) ✅ After the loop, start points to the answer ✅ Use start % letters.length to handle the circular wrap-around 𝗧𝗵𝗲 𝗰𝗹𝗲𝘃𝗲𝗿 𝗯𝗶𝘁: If target is greater than or equal to all letters, start lands at letters.length — modulo wraps it back to index 0 automatically. No special case needed. 🔄 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(log n) 📦 Space: O(1) One modulo operation handles the entire circular edge case cleanly. This is binary search with a twist — and the wrap-around trick is worth remembering! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gvK_p44b 11 more days. So close! 💪 #LeetCode #Day89of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
🚀 Day 41/100 – LeetCode DSA Practice 📌 Problem: 561. Array Partition Today’s challenge was about grouping elements into pairs such that the sum of the minimum values of each pair is maximized. 💡 My Approach: Initially thought about checking all possible pair combinations ❌ (too complex) Then realized this is a problem Sorted the array in ascending order Paired adjacent elements Added elements at even indices (0, 2, 4...) to get the maximum sum 🧠 Key Insight: After sorting, pairing neighbors ensures that the minimum in each pair is as large as possible, which maximizes the total sum. 💻 Code (Java): import java.util.Arrays; class Solution { public int arrayPairSum(int[] nums) { Arrays.sort(nums); int sum = 0; for(int i = 0; i < nums.length; i += 2) { sum += nums[i]; } return sum; } } 📚 What I Learned Today: Importance of recognizing patterns Sorting can simplify complex pairing problems Always look for pattern instead of brute force 🔥 Small insight, big impact! #Day41 #100DaysOfCode #LeetCode #DSA #GreedyAlgorithm #CodingJourney
To view or add a comment, sign in
-
-
Another step forward in my DSA journey 🚀 After going through the basics, I’ve now started Arrays — and today was all about understanding how they actually work under the hood. 💡 Key takeaway: Arrays are stored in the heap, but each array itself is allocated as a continuous block of memory — making indexing fast and efficient. Also explored: • Why arrays are needed • Internal working & memory representation • Dynamic memory allocation • null & default values in Java • for-each loop & toString() • Arrays of objects • Passing arrays in functions Small concept, big clarity. This is where real problem-solving starts. #DSA #Java #LearningInPublic #Consistency #CodingJourney
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development