Day 9 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Remove Duplicates from a Sorted Array The array was already sorted. We had to keep only unique elements. And do it in-place. Let’s understand with real life example ◾️Imagine you are arranging certificates in a file. ◾️Some certificates are duplicated. ◾️You only keep one copy of each. ◾️You arrange them neatly at the front. ◾️Whatever remains at the back does not matter. That’s the idea. 💻Optimal Approach (Using Set) 🔹️Create a set. 🔹️Insert all elements of the array into the set. 🔹️Set automatically removes duplicates. 🔹️Size of set gives number of unique elements (k). 🔹️Traverse the set. 🔹️Fill first k indices of the array with unique elements. Clean and simple. 📊 Complexity Analysis ▫️Time Complexity: O(N) We traverse the array once. ▫️Space Complexity: O(1) Constant extra space used. 📚 What I learned today: ▫️Sorted arrays make duplicate problems easier. ▫️Set is very useful for handling uniqueness. ▫️Understanding constraints helps in choosing approach. #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
Remove Duplicates from Sorted Array in Place
More Relevant Posts
-
Day 28 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Convert Numbers to Words We were given a number. Task was to convert it into words. Example: 123 → One Hundred Twenty Three This problem was more about mapping. And understanding place values. 💻 Approach 🔹️Create mappings for digits 0–9. 🔹️Create mappings for 10–19 (special cases). 🔹️Create mappings for tens like 20, 30, 40... 🔹️Also store place values like hundred and thousand. Then process the number step by step. 🔹️Traverse the number from left to right. 🔹️If more than two digits remain, handle place values. 🔹️If two digits remain and first digit is 1, use teen mapping. 🔹️Otherwise print tens and units separately. Continue until all digits are processed. 📊 Complexity Analysis Time Complexity: O(n) Each digit is processed once. Space Complexity: O(1) Only fixed mapping arrays are used. 📚 What I learned today: ▫️This problem is essentially a lookup + formatting problem using constant mapping tables. ▫️Handling special ranges (10–19) separately avoids incorrect word formation. ▫️Breaking the number into place-value segments (hundreds, tens, units) simplifies logic. ▫️Designing clean mapping structures is important for problems involving symbolic representation. Day 28 completed. Learning different problem patterns every day 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 32 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Most Frequent Character in a String We were given a string s. All characters are lowercase. Task was to find the character with maximum frequency. If multiple characters have same frequency, return the lexicographically smaller one. Example: "testsample" → e 💻 Approach 🔹️Create a frequency array of size 26. 🔹️Traverse the string and count each character. 🔹️Find the character with maximum frequency. 🔹️If frequencies are same, pick smaller character. Simple counting logic. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) Fixed array of size 26. 📚 What I learned today: ▫️Frequency arrays are very useful for string problems. ▫️Handling tie conditions (lexicographic order) is important. ▫️Constant space solutions are possible with fixed character sets. ▫️Careful comparison logic matters in final result. Day 32 completed. Improving string handling step by step 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 23 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Check if a Number is an Abundant Number We were given an integer n. We had to check if it is an abundant number. An abundant number means: The sum of its proper divisors is greater than the number itself. Example: 12 → divisors: 1, 2, 3, 4, 6 Sum = 16 Since 16 > 12, it is an abundant number. 💻 Brute Force Approach 🔹️Traverse from 1 to n. 🔹️Check if i divides n. 🔹️If yes, add it to sum. 🔹️After traversal, compare sum with n. 🔹️If sum > n → abundant number. Time Complexity: O(n) Space Complexity: O(1) Works. But unnecessary checks happen. 💻 Optimized Approach 🔹️Traverse from 1 to √n. 🔹️If i divides n, add i to sum. 🔹️Also add n / i as paired divisor. 🔹️Avoid adding duplicates when i = n/i. 🔹️Finally compare sum with n. Time Complexity: O(√n) Space Complexity: O(1) Much fewer iterations. 📚 What I learned today: ▫️Divisors always appear in pairs. ▫️Checking till √n is enough for divisor problems. ▫️Avoid counting duplicate divisors for perfect squares. ▫️Small observations can reduce complexity a lot. Day 23 completed. #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 29 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Reverse a String We were given a string. Task was simple. Reverse the characters. Example: "hello" → "olleh" Looks easy. But there are multiple ways to do it. 💻 Brute Force Approach 🔹️Create a new empty string. 🔹️Traverse the original string from the end. 🔹️Append each character to the new string. 🔹️Return the new reversed string. Time Complexity: O(n) Space Complexity: O(n) Because a new string is created. 💻 Optimal Approach (Two Pointers) 🔹️Convert the string into a character array. 🔹️Place one pointer at the start. 🔹️Place another pointer at the end. 🔹️Swap both characters. 🔹️Move pointers toward the center. Continue until they meet. Time Complexity: O(n) Space Complexity: O(1) No extra string needed. 📚 What I learned today: ▫️Two-pointer technique works well for reversal problems. ▫️In-place operations help reduce space complexity. ▫️String problems often become easier when treated as arrays. ▫️Pointer movement patterns appear in many algorithms. Day 29 completed. Small problem, but useful pattern 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 4/100 — LeetCode Challenge Today I practiced problems based on the Two Pointer technique, a powerful approach for solving array and string problems efficiently. Problems solved: • Valid Palindrome • Two Sum II (Input Array Is Sorted) 🧠 Concept: Two pointers starting from different ends of the array/string and moving toward each other. 💡 Key Learning: Two pointer techniques often help eliminate nested loops and reduce time complexity to O(n). 📂 Solutions Repository https://lnkd.in/gkFh2mPZ Understanding these patterns is helping me recognize more efficient ways to approach problems. #100DaysOfLeetCode #DSA #LeetCode #CodingChallenge #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Day 6/100 — LeetCode Challenge Today's problem: 3Sum 🧠 Concept: Sorting + Two Pointers 💡 Key Idea: After sorting the array, fix one element and use two pointers to find the remaining pair that sums to the target while avoiding duplicate triplets. ⚡ Time Complexity: O(n²) 📂 Solutions Repository https://lnkd.in/gkFh2mPZ This problem was a great exercise in combining multiple techniques to reduce brute-force complexity. #100DaysOfLeetCode #DSA #LeetCode #CodingChallenge #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Day 5/100 — LeetCode Challenge Today's problem: Container With Most Water 🧠 Concept: Two Pointers 💡 Key Idea: Start with two pointers at the ends of the array and calculate the container area using the smaller height. Move the pointer with the smaller height to explore potentially larger areas. ⚡ Time Complexity: O(n) 📂 Solutions Repository https://lnkd.in/gkFh2mPZ Interesting to see how a simple two-pointer strategy can turn a brute-force O(n²) solution into an efficient O(n) approach. #100DaysOfLeetCode #DSA #LeetCode #CodingChallenge #SoftwareEngineering
To view or add a comment, sign in
-
What if I told you a simple grid can improve your recursion thinking? 🧠 Day 17of #60DaysOfCode 🚀 Today I worked on the Maze Path problem using recursion 🔁 You start from the top-left corner of a grid and can only move: ➡ Right ⬇ Down Goal is to reach the bottom-right corner 🎯 At first it felt confusing… but then the pattern clicked 💡 From any position (i, j), I only have 2 choices: ➡ Go Right → (i, j+1) ⬇ Go Down → (i+1, j) So the total number of ways from that cell becomes: ways(i, j) = ways(i, j+1) + ways(i+1, j) Why does this work? Because every valid path must begin with either a right move or a down move. So I just add both possibilities The most important part was understanding the base cases 📌 ✅ If I reach the destination → return 1 (one valid path found) ❌ If I go outside the grid → return 0 (invalid path) Just breaking the problem into smaller pieces and understanding recursion #DSA #Recursion #60DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
Day 27 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Convert Octal to Binary We were given an octal number. Task was to convert it into binary. Instead of converting directly, we used a two-step process. First → Octal to Decimal. Then → Decimal to Binary. 💻 Approach Step 1: Octal to Decimal 🔹️Take each digit of the octal number. 🔹️Multiply it with (8^i). 🔹️Increase power (i) as we move left. 🔹️Add all values to get decimal number. Step 2: Decimal to Binary 🔹️Divide the decimal number by 2 repeatedly. 🔹️Store the remainders. 🔹️Continue until the number becomes 0. 🔹️Read the remainders in reverse order. That gives the binary number. 📊 Complexity Analysis Time Complexity: O(log n) Space Complexity: O(1) Only a few variables are used. 📚 What I learned today: ▫️Number system conversions can be chained. ▫️Octal to decimal uses powers of 8. ▫️Binary conversion uses repeated division by 2. ▫️Breaking the problem into steps makes it easier. Day 27 completed. Practicing number system conversions 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🧩 LeetCode Challenge – Day 72 ✅ Back to tree traversal today — focusing on constructing paths from root to leaf. 🔗 LeetCode 257 – Binary Tree Paths The task is to explore every root-to-leaf path and record it as a string. A simple DFS works well here, building the path as we move down the tree and storing it once a leaf is reached. 💡 Key Takeaways: • DFS is ideal for exploring all root-to-leaf paths • Passing path information through recursion keeps the logic clean • Tree problems often become easier when you visualize the traversal #Day72 #LeetCodeChallenge #365DaysOfCode #DSA #CodingJourney #ProblemSolving #BinaryTree #DFS
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