Today I practiced an important DSA concept: Finding First & Last Occurrence of an Element in an Array What I learned: 1) How to find all occurrences using Linear Search 2) How to find first & last index 3) When to use Linear Search (unsorted array) 4) When to use Binary Search (sorted array) 5) Time Complexity → O(n) for unsorted array #DSA #Java #CodingPractice #PlacementPreparation #ProblemSolving #FutureSoftwareEngineer
More Relevant Posts
-
🚀 Day 6 – DSA Practice Solved Remove Duplicates from Sorted Array today. 📌 Problem: Given a sorted array, remove duplicates in-place such that each element appears only once and return the new length. 💡 Insight: Use the two-pointer approach — one pointer tracks unique elements, while the other scans the array to update values in-place. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Strengthening my understanding of in-place array manipulation and pointer techniques. #Java #DSA #LeetCode #TwoPointers #CodingInterview
To view or add a comment, sign in
-
-
🚀 DSA Tip: Two Pointer Approach for Target Sum If your array is sorted, you don’t need nested loops to find a pair with a given target sum. 👉 Use the Two Pointer Technique — simple and O(n)! 💡 How it works: • Place one pointer at the start (left) • Place another at the end (right) • Compare their sum with the target ✅ If sum is small → move left++ ✅ If sum is large → move right-- ✅ If equal → 🎯 pair found ⏱️ Complexity: Time → O(n) Space → O(1) 🔥 When to use: ✔️ Target sum in sorted array ✔️ Pair problems ✔️ Remove duplicates ✔️ Reverse problems Mastering two pointers can save you from unnecessary O(n²) solutions! #DSA #CodingInterview #Java #TwoPointers #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
Day 42 of DSA 🚀 | Alternating Binary String Today’s problem: Minimum Changes To Make Alternating Binary String 🔹 Task: Convert a binary string into an alternating string (0101... or 1010...) using the minimum number of flips. 💡 Key Insight: Only two valid patterns exist: 010101... 101010... Count mismatches with one pattern and compute the other using: min(count, n - count) 📌 Example s = "0100" → Output = 1 ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) Small problem, but a great reminder that recognizing patterns can simplify the solution drastically. #DSA #Java #LeetCode #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 23 of #DSAChallenge Focused on: Detecting a Cycle in a Linked List Understanding Floyd’s Cycle Detection Algorithm (Tortoise & Hare) Using two pointers moving at different speeds can detect a loop without extra space. This problem taught me how important observation and pattern recognition are in DSA. It’s not always about brute force — it’s about thinking efficiently. #DSA #Java #LinkedList #ProblemSolving #InterviewPreparation #Consistency
To view or add a comment, sign in
-
🚀 Day 43 of DSA |Check if Binary String Has at Most One Segment of Ones. Given a binary string without leading zeros, we need to check whether it contains at most one contiguous segment of '1's. Example Input: "1001" → Output: false Input: "110" → Output: true 💡 Key Insight If a '0' appears after the first segment of '1's and we encounter another '1', it means a new segment started. 🧠 Approach • Traverse the string • Once '0' appears after '1', mark it • If another '1' appears afterwards → return false ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem highlights how pattern observation can simplify string traversal problems. #DSA #Java #LeetCode #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
DSA daily streak | day-17 ✅Find Minimum in Rotated Sorted Array II Today, I worked on finding the minimum element in a rotated sorted array that may contain duplicates. This problem is a tricky extension of the classic binary search pattern. ✨ The idea is to: • Use modified binary search • Compare mid with high to detect the unsorted half • Carefully handle duplicates when they blur the sorted structure • Gradually shrink the search space to reach the minimum This approach avoids full linear scanning in most cases and keeps the solution efficient. #DSA #Java #ProblemSolving #LeetCode #BinarySearch #NPCI #LearningJourney
To view or add a comment, sign in
-
Day 32 of DSA 🚀 | Check If a String Contains All Binary Codes of Size K Today’s problem looked straightforward but tested how well I understand constraints and coverage. 🔹 Task: Check whether a binary string contains every possible binary code of length k as a substring. 🔹 Key realization: This is not about generating binary codes. It’s about verifying coverage using a sliding window. 💡 What I learned: Total binary codes of size k = 2^k Sliding window helps examine substrings efficiently HashSet tracks unique patterns automatically Correct boundary (s.length() - k) avoids runtime errors ⏱ Time Complexity: O(n × k) 📦 Space Complexity: O(2^k) 📌 Example: "00110110", k = 2 → all patterns {00, 01, 10, 11} exist → ✅ true This problem reinforced how small details in problem statements can completely change the approach. #DSA #SlidingWindow #Strings #Java #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🔁 DSA Practice: Reverse String (LeetCode Problem) Today I practiced the Reverse String problem, a fundamental question that helps build a strong understanding of arrays and two-pointer techniques. In this problem, we are given a character array and the goal is to reverse the string in-place, meaning we must modify the original array without using extra memory. 💡 Key Concept: Two-Pointer Technique One pointer starts from the beginning of the array. Another pointer starts from the end of the array. We swap the characters and move the pointers toward the center until they meet. 📌 Why this problem is important Improves understanding of array manipulation Introduces the two-pointer approach Helps practice in-place algorithms with O(1) space complexity ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistent practice with such problems strengthens problem-solving skills and builds a solid foundation for more advanced DSA challenges. #DSA #LeetCode #Java #ProblemSolving #CodingPractice #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 44 of DSA 🚀 Solved Minimum Number of Flips to Make the Binary String Alternating. 💡 Key Insight: An alternating binary string can only follow two patterns: 010101... 101010... To handle rotations efficiently: Double the string (s + s) Use a sliding window of size n Count mismatches with both patterns Track the minimum flips required. ⏱ Time: O(n) 💾 Space: O(n) Good practice for sliding window + pattern matching. #DSA #Java #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
Day 26 - Single Element in a Sorted Array Technique Used: Parity-Based Binary Search Key Idea: Since every element appears twice except one, used index parity (even/odd positions) to determine which half violates the pairing rule. If pairing is correct on the left side, move right. Otherwise, move left. Time Complexity: O(log n) #Day26 #LeetCode #Java #BinarySearch #DSA #Algorithms
To view or add a comment, sign in
-
Explore related topics
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