📌 Day 3/100 - Remove Duplicates from Sorted Array (LeetCode 26) 🔹 Problem: Given a sorted array, remove the duplicates in-place so that each element appears only once and return the new length. You must modify the array without using extra space for another array. 🔹 Approach: I used a simple counting-based approach: Iterate through the array using a single loop. If the current element is the same as the next, skip it. Otherwise, place it at the current count index and increment count. Finally, return count as the number of unique elements. 🔹 Key Learning: Practiced in-place array modification efficiently without extra space. Improved understanding of loop-based filtering logic. Realized that sometimes the simplest linear approach works best! Consistency compounds — each problem adds a new layer of confidence! 🚀#100DaysOfCode #LeetCode #Java #ProblemSolving #Array #DSA #TwoPointers
Removing Duplicates from Sorted Array in Java
More Relevant Posts
-
📌 Day 2/100 – Remove Element (LeetCode 27) 🔹 Problem: Given an integer array nums and a value val, remove all instances of that value in-place and return the new length of the array. The order of elements can be changed. 🔹 Approach: Used the two-pointer technique to efficiently modify the array in-place. One pointer iterates through the array, while the other tracks the position to overwrite non-val elements. Returned the position of the second pointer as the new length. 🔹 Key Learning: Strengthened understanding of in-place array manipulation. Improved logic building for pointer movement and conditional overwriting. Learned how to minimize extra space usage while maintaining readability and clarity. Another small yet powerful step toward mastering array-based problems! 💻 🔥 #100DaysOfCode #LeetCode #Java #ProblemSolving #TwoPointers #DSA #CodingJourney
To view or add a comment, sign in
-
-
💡 LeetCode #15 — 3Sum Today I solved LeetCode Problem 15: 3Sum 🔍 Problem Summary: Given an integer array nums, return all unique triplets (a, b, c) such that: a + b + c = 0 The solution must not contain duplicate triplets. Key Idea: Use sorting + two pointers to reduce the time from O(n³) to O(n²). Steps: Sort the array. Fix one number (nums[f]) in each iteration. Use two pointers (i and j) to find pairs whose sum equals -nums[f]. Skip duplicates for both the fixed index and the pointer values. This efficiently finds all unique triplets. #LeetCode #Java #DSA #TwoPointers #ProblemSolving #CodingChallenge #Algorithms
To view or add a comment, sign in
-
-
📌 Day 32/100 – Count and Say (LeetCode 38) 🔹 Problem: Given an integer n, return the nth term of the Count and Say sequence, where each term is generated by describing the digits of the previous term. 🔹 Approach: Used recursion to generate the previous sequence (n-1) and built the current one by counting consecutive identical digits. Utilized StringBuilder for efficient string formation during traversal. 🔹 Key Learning: Improved recursion and pattern recognition skills. Practiced string manipulation and efficient concatenation. Understood how to construct sequences based on descriptive logic. 🔹 Complexity: Time: O(m) per level (m = length of previous term) Space: O(n) due to recursion depth #100DaysOfCode #LeetCode #Java #Recursion #Strings #ProblemSolving #DSA #CodingPatterns #Motivation #descipline
To view or add a comment, sign in
-
-
28/30days✅ #2427 Number of Common Factors The logic behind the Number of Common Factors problem is simple yet efficient. The goal is to find how many numbers can divide both given integers a and b without leaving a remainder. To achieve this, I first identify the smaller of the two numbers using Math.min(a, b) since no common factor can be greater than the smaller value. Then, I use a loop that runs from 1 up to that number and check for each value whether it divides both a and b completely (a % i == 0 && b % i == 0). If it does, it’s counted as a common factor. Finally, the program returns the total count of such numbers. For example, if a = 12 and b = 6, the common factors are 1, 2, 3, and 6, giving the output 4. This approach is easy to implement, logically clear, and works efficiently for all valid inputs. #LeetCode #Java #Coding #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 7️⃣7️⃣ of #100DaysOfCode Solved LeetCode Problem #1437 — Check If All 1’s Are at Least K Places Away 🔍✨ 🔧 Concept: Given a binary array, ensure that every 1 is separated by at least K zeros. The solution simply tracks the previous index of 1 and checks the distance. 🧠 Key Idea: Scan the array once When a 1 is found, verify: 👉 currentIndex - previousIndex > k If any pair violates this, return false ⚡ Efficient: Time Complexity: O(n) Space Complexity: O(1) Another clean and efficient logical problem solved. #LeetCode #Java #Arrays #ProblemSolving #DSA #CodingJourney #100DaysOfCode #DeveloperLife #Motivation
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 67 Reverse Words in a String Problem: – Reverse Words in a String Task: – Given a string, reverse the order of the words and remove extra spaces. Example: Input: s = " the sky is blue " → Output: "blue is sky the" My Approach: Used trim() to remove leading and trailing spaces. Split the string using split("\\s+") to handle multiple spaces. Reversed the array and joined the words with a single space. Time Complexity: O(N) | Space Complexity: O(N) Even simple string problems can teach the importance of clean code and efficient use of built-in methods. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #GeeksForGeeks #CodeNewbie #StringManipulation
To view or add a comment, sign in
-
-
🚀 Day 31: LeetCode 167 — Two Sum II (Two Pointer Approach) 🔹 Concept Covered: Two Pointer Technique on a Sorted Array 🔹 Problem Goal: Find two numbers such that they add up to a specific target. 🔹 Approach: Use two pointers (low at start, high at end). Calculate the sum of both pointers. If sum == target → return indices. If sum > target → move high-- (reduce sum). If sum < target → move low++ (increase sum). 💡 Time Complexity: O(n) 💡 Space Complexity: O(1) 🎯 Learning: The two-pointer approach is extremely efficient for sorted arrays — no need for nested loops or hash maps. It’s all about moving intelligently in one pass! #LeetCode #Day31 #TwoPointer #Java #CodingJourney #YeswanthCodes
To view or add a comment, sign in
-
-
📌 Day 11/100 – Longest Common Prefix (LeetCode 14) 🔹 Problem: Given an array of strings, find the longest common prefix shared among them. If no common prefix exists, return an empty string. 🔹 Approach: I first sorted the array of strings in alphabetical order. The idea is that only the first and last strings in the sorted order will differ the most — so the common prefix between them is also the prefix for the entire array. I then compared characters one by one between these two strings to extract the prefix efficiently. 🔹 Key Learning: Sorting simplifies comparison by aligning similar prefixes together. Smart observation can reduce unnecessary iterations. Sometimes, the most optimal logic lies in choosing the right perspective. Step by step, improving problem-solving clarity and confidence 💪 #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #LearnByDoing #CodingChallenge
To view or add a comment, sign in
-
-
🗓 Day 6 / 100 – #100DaysOfLeetCode 📘 Problem: 3228. Maximum Number of Operations to Move Ones to the End Difficulty: Medium 💡 Problem Summary: Given a binary string s, you can repeatedly choose an index i where s[i] == '1' and s[i+1] == '0', and move that '1' to the right until it reaches the end of the string or hits another '1'. The goal is to find the maximum number of such operations possible. 🧠 My Approach: Instead of simulating the moves (which would be inefficient), I used a counting strategy: Keep a running count of the number of '1's seen so far (cnt). Whenever a '0' appears after one or more '1's, we can perform cnt operations involving those ones. Sum these up for the final result. 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
✅Day 80 of #100DaysOfLeetCode 1.📌Problem: Set Mismatch You are given an array that represents a set of integers from 1 1 to n n, but due to an error, one number is duplicated and another is missing. The task is to find the duplicated number and the missing number and return them as an array. 2.🟢 Difficulty: Easy 3.📍Topic: Hashing,Array 4.🎯 Goal: Find the number that occurs twice and the number that is missing, then return them in an array. 5.🧠 key idea: Approach 1: Use a HashMap to count occurrences of each number in the input array. Identify the duplicated number by finding the one which appears twice. Compute the total sum expected (n(n+1)/2 n(n+1)/2) and compare with the actual sum (excluding the duplicate). The missing number equals the difference between the expected total and the corrected actual sum. Return the duplicate and missing numbers as the result array. #LeetCode #100DaysOfCode #CodingChallenge #Java #Programming #InterviewPrep #Algorithms #DataStructures #Hashing #ProblemSolving #Array #SoftwareEngineering #TechCareers #LearnToCode #CodeNewbie #DailyCode #ChallengeYourself
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