100 Days of Coding Challenge – Day 26 📌 Problem: Remove Duplicates from Sorted Array II 💻 Language: Java 🧠 Concept Used: Two Pointer Technique + Counting 🔍 Platform: LeetCode Today’s challenge was to remove duplicates from a sorted array in-place, but this time allowing at most two occurrences of each element. Approach: ✔ Traverse the array and track occurrences using a counter ✔ Allow elements to appear at most twice ✔ Use a pointer k to place valid elements in the correct position ✔ Skip elements when they exceed the allowed count Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gm2_CHbz 🔗 Code: https://lnkd.in/gKxuTbsG #100DaysOfCode #Day26 #Java #DSA #LeetCode #TwoPointers #Arrays #ProblemSolving #CodingJourney
Remove Duplicates from Sorted Array II in Java with Two Pointers
More Relevant Posts
-
100 Days of Coding Challenge – Day 30 📌 Problem: Find the Index of the First Occurrence in a String 💻 Language: Java 🧠 Concept Used: String Matching (Brute Force) 🔍 Platform: LeetCode Today’s challenge was to find the first occurrence of a substring (needle) in a given string (haystack). If not found, return -1. Example: Input: "sadbutsad", "sad" Output: 0 Approach: ✔ Traverse the string from index 0 to n - m ✔ Extract substring of length m at each position ✔ Compare it with the target string ✔ Return index immediately when match is found ✔ If no match → return -1 Time Complexity: O(n × m) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/g2ktYFFS 🔗 Code: https://lnkd.in/gynFixSQ #100DaysOfCode #Day30 #Java #DSA #LeetCode #Strings #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 93 - LeetCode Journey Solved LeetCode 9: Palindrome Number in Java ✅ At first glance, it feels like a string problem… but the real challenge is solving it without converting to string. Instead of reversing the whole number, I reversed only half of it and compared both parts. This avoids overflow and keeps it efficient. Smart approach > brute force 💡 Key takeaways: • Handling edge cases (negative numbers, trailing zeroes) • Reversing only half of the number • Avoiding extra space (no string conversion) • Writing optimized mathematical logic ✅ All test cases passed ⚡ O(log n) time and O(1) space Sometimes the best solutions are the simplest ones, just a different way of thinking 🔥 #LeetCode #DSA #Java #Math #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 33 🚀 📌 Problem: Longest Mountain in Array 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Peak Expansion / Two Pointers 1. Traverse the array and identify a peak element (greater than both neighbors) 2. From the peak, expand left while elements are increasing 3. Expand right while elements are decreasing 4. Calculate the length of the mountain (right - left + 1) 5. Track the maximum length across all valid peaks Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gS3Uzs3t 🔗 Code Link: https://lnkd.in/gG8gq2KU #100DaysOfCode #Day33 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #TwoPointers #Array
To view or add a comment, sign in
-
-
🚀 Mastering Java Through LeetCode 🧠 Day 32 Today I solved an Easy-level Tree problem that strengthened my understanding of Recursion + Depth Calculation — a fundamental concept for tree-based problems 📌 LeetCode Problem Solved: Q.104. Maximum Depth of Binary Tree 💭 Problem Summary: Given a binary tree, the task is to find the maximum depth — i.e., the number of nodes along the longest path from root to leaf. 🧠 Approach (Optimal): Instead of iterating level by level, I used a clean recursive approach: ✔️ If node is null → return 0 ✔️ Recursively calculate left subtree depth ✔️ Recursively calculate right subtree depth ✔️ Return 1 + max(left, right) ⚡ Key Learning: Recursion makes tree problems much simpler when you think in terms of “what should each function return for a node?” Complexity: Time: O(n) Space: O(h) Takeaway: Tree problems become easier once you master recursion and start recognizing patterns. Consistency is the key — showing up every day #Day32 #LeetCode #Java #DSA #CodingJourney #Recursion #BinaryTree #100DaysOfCode #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 22 of #50DaysLeetCode Challenge 🚀 Today I solved the “Remove Element” problem using Java. 🔹 Problem: Given an array and a value, remove all occurrences of that value in-place and return the number of remaining elements. 🔹 Approach: Used the two-pointer technique. One pointer iterates through the array, while the other keeps track of where to place elements that are not equal to the given value. 🔹 Key Learning: In-place array problems often rely on pointer manipulation instead of extra space. 🔹 Time Complexity: O(n) 🔹 Example: Input: nums = [3,2,2,3], val = 3 Output: 2 (array becomes [2,2,...]) Simple problem, but important for mastering array manipulation techniques. #Day22 #LeetCode #Java #DSA #Arrays #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 29 📌 Problem: Isomorphic Strings 💻 Language: Java 🧠 Concept Used: HashMap + Bidirectional Mapping 🔍 Platform: LeetCode Two strings are isomorphic if characters in one string can be replaced to get the other string with a one-to-one mapping, while preserving order. Example: s = "egg", t = "add" → ✅ true Approach: ✔ Traverse both strings simultaneously ✔ Use two HashMaps: • One for mapping s → t • One for mapping t → s ✔ Ensure consistency in both directions ✔ If any mismatch occurs → return false Time Complexity: O(n) Space Complexity: O(n) 🔗 Problem Link: https://lnkd.in/gHJ3Vn2a 🔗 Code: https://lnkd.in/gWrRUiN4 #100DaysOfCode #Day29 #Java #DSA #LeetCode #HashMap #Strings #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 32 📌 Problem: Subarray Product Less Than K 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Sliding Window (Two Pointers) 1. Initialize two pointers (left and right) and a variable to track product 2. Expand the window by moving right and multiply elements 3. If product becomes ≥ k, shrink the window from the left 4. For each valid window, count subarrays using right - left + 1 5. Accumulate the count to get the final answer 👉 Key Insight: If a window is valid, all its subarrays ending at right are also valid Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gFWbBHeF 🔗 Code Link: https://lnkd.in/gxZF2_JA #100DaysOfCode #Day32 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #SlidingWindow #TwoPointers
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 27 📌 Problem: Maximum Average Subarray I 💻 Language: Java 🧠 Concept Used: Sliding Window Technique 🔍 Platform: LeetCode Today’s challenge was to find a contiguous subarray of length k that has the maximum average value. Example: Input: [1,12,-5,-6,50,3], k = 4 Output: 12.75 Approach: ✔ Calculate the sum of the first window of size k ✔ Slide the window by removing the left element and adding the next element ✔ Keep track of the maximum sum ✔ Divide by k to get the maximum average Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/ghU4zrpt 🔗 Code: https://lnkd.in/g7qXeHx6 #100DaysOfCode #Day27 #Java #DSA #LeetCode #SlidingWindow #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 28 📌 Problem: Find First and Last Position of Element in Sorted Array 💻 Language: Java 🧠 Concept Used: Binary Search (Modified) 🔍 Platform: LeetCode Today’s challenge was to find the starting and ending positions of a target value in a sorted array. If the target is not found → return [-1, -1]. Approach: ✔ Use Binary Search since the array is sorted ✔ Perform two searches: • One to find the first occurrence • One to find the last occurrence ✔ Narrow the search space based on comparisons ✔ Combine both results into the final answer Time Complexity: O(log n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gK5EM63Z 🔗 Code: https://lnkd.in/gMwMsF7R #100DaysOfCode #Day28 #Java #DSA #LeetCode #BinarySearch #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 73 of #90DaysDSAChallenge Solved LeetCode 451: Sort Characters By Frequency Learned an important Java design concept today. Problem Overview: The task was to sort characters in a string based on descending frequency. What confused me initially: Why create a separate Freq class instead of just using HashMap and PriorityQueue directly? Key Learning: PriorityQueue stores one complete object at a time. For this problem, each item needs two pieces of data together: Character Frequency Example: Instead of storing: e and 2 separately We package them as: Freq('e', 2) That custom class acts like a container holding both values in one object, so PriorityQueue can compare and sort them correctly. Why this matters: This taught me that custom classes in Java are often not about complexity, they simply bundle related data into one manageable unit. Alternative approach: We can also use Map.Entry<Character, Integer> instead of creating a custom class, but building Freq makes the logic easier to understand while learning. Today’s takeaway: Not every class is for business logic — sometimes it exists just to package data cleanly. #Java #90DaysDSAChallenge #LeetCode #PriorityQueue #HashMap #CodingJourney #ProblemSolving
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
Great efforts Janani Murugesan I really appreciate for your dedication towards competitive coding - never ever invest your precious time on only competative solving - invest time on learning frameworks - you will not regret