Day 19 LeetCode #88 — Merge Sorted Array Most people try to merge from the front. That’s where the bug begins. The real trick is thinking backwards. Since nums1 already has empty space at the end, we can start filling from the back using 3 pointers: i → last valid element in nums1 j → last element in nums2 k → last index of final merged array Whichever value is bigger goes at position k. This avoids shifting elements and keeps the solution: ✅ In-place ✅ O(m + n) time ✅ O(1) space A small problem, but a powerful lesson: Sometimes the cleanest solution comes from reversing the direction of thought. #LeetCode #DSA #Java #TwoPointers #CodingInterview #ProblemSolving #SoftwareEngineering #100DaysOfCode
Merge Sorted Array with In-Place Solution
More Relevant Posts
-
🚀 Day 12 of #100DaysOfCode 🧠 LeetCode Daily: Rotate Image (Medium) This one looked like a simple matrix problem… but the real challenge was doing it in-place without using extra space 👀 💡 Problem Insight We’re given an n x n matrix (image), and we need to rotate it 90° clockwise. ⚠️ Constraint: We must modify the same matrix — no extra matrix allowed. 🧩 Intuition Instead of thinking about rotation directly, break it into steps: 👉 Step 1: Transpose the matrix (Swap rows with columns) 👉 Step 2: Reverse each row That’s it — the matrix gets rotated! #100DaysOfCode #LeetCode #Java #DSA #Matrix #ProblemSolving #CodingJourney #Placements
To view or add a comment, sign in
-
-
🔥 Day 147/360 – Stop Thinking in Functions… Start Thinking in Bits ⚡ 📌 Topic: Bit Manipulation 🧩 Problem: Power Function (xⁿ) 📝 Problem Statement: Calculate x raised to the power n efficiently 🔍 Example: Input: x = 2, n = 10 Output: 1024 💡 Approach: Binary Exponentiation (Bit-Based Thinking) ✔ Step 1 – Convert power into binary form ✔ Step 2 – Square the base at each step ✔ Step 3 – Multiply only when current bit is 1 ⏱ Complexity: Time → O(log n) Space → O(1) 📚 What I Learned: The biggest optimization comes from changing how you think, not just how you code #Day147 #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #TechJourney #360DaysOfCode
To view or add a comment, sign in
-
-
��� Day 9 — #100DaysOfLeetCode Today’s problem: Reveal Cards In Increasing Order (LeetCode 950) This problem was a good reminder that simulation problems become much easier when we focus on positions instead of values. 💡 Approach: -> Sorted the deck -> Used a queue to simulate indexes ->Applied the reveal + move-to-bottom process The most interesting part was: queue.offer(queue.poll()) which perfectly simulates moving the top card to the bottom. ⏱️ Complexity: Time: O(n log n) Space: O(n) #LeetCode #100DaysOfLeetCode #DSA #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 4 of #LeetCode60DayChallenge Today I solved Remove Element (Problem 27) – a classic in-place array manipulation problem. The task: Remove all occurrences of a given value from an array and return the count of remaining elements, without using extra space. The approach: Two-pointer technique (k pointer for placement, i pointer for traversal) Shift only non-target values to the front Achieved O(n) time and O(1) space The result: ✅ 0 ms runtime (beats 100%) ✅ Memory: 43.31 MB (beats 78.16%) Little by little, these small wins build big momentum. Consistency over intensity 💪 👉 What's your go-to strategy for in-place array problems? #LeetCode #100DaysOfCode #Java #CodingChallenge #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
Day: 129/365 📌 LeetCode POTD: Find Minimum in Rotated Sorted Array Level: Medium Key takeaways/Learnings from this problem: 1. This problem is a classic example of how binary search can work even when the array isn’t fully sorted. 2. The main trick is identifying which half is properly sorted and using that to shrink the search space. 3. It also teaches the importance of handling edge cases like an already sorted array without overcomplicating the logic. 4. Biggest takeaway: whenever you see a rotated sorted structure, think “modified binary search” before anything else. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
To view or add a comment, sign in
-
🚀 Day 32 of #LeetCode Challenge Problem: 50. Pow(x, n) Difficulty: Medium 🔍 Approach: Fast Exponentiation (Binary Exponentiation) Instead of multiplying x repeatedly, used an optimized approach: ➡️ If power is even → square the base ➡️ If power is odd → multiply result with base ➡️ Reduce power by half each step Handled negative powers by converting: Also used long to avoid overflow for large values of n 🚀 ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) 💡 Key Learning: Binary Exponentiation drastically improves performance from O(n) → O(log n) 🔥 #Day32 #LeetCode #DSA #Java #BinaryExponentiation #CodingJourney #Consistency
To view or add a comment, sign in
-
-
100-Day LeetCode Challenge – Day 67 Solved 392. Is Subsequence on LeetCode. 📌 Approach: • Used two pointers to traverse both strings • Compared characters and moved pointer when matched • Checked if all characters of s are found in order in t 🧠 Key Idea: A two-pointer technique efficiently checks subsequence without extra space. 💡 Example: s = "abc", t = "ahbgdc" → true ⚡ Complexity: • Time: O(n) • Space: O(1) 🔥 Simple logic, but powerful pattern for string problems. #LeetCode #DSA #100DaysChallenge #Java #ProblemSolving #Consistency #Day67 #LeetCode100
To view or add a comment, sign in
-
-
Day 97 of #100DaysOfCode Today’s problem: Power of Four (LeetCode 342) 🧠 Problem Determine whether a given integer n is a power of 4. 💡 Approach A number is a power of 4 if: It is greater than 0 It has only one set bit (power of 2) That bit is in the correct position ⚙️ Logic (n & (n - 1)) == 0 → ensures only one bit is set (n & 0x55555555) != 0 → ensures it’s a power of 4 🔁 Example 16 → true 5 → false 1 → true ⏱ Complexity Time: O(1) Space: O(1) 📌 Takeaway Bit manipulation helps solve problems in constant time, making solutions both clean and efficient. #Day97 #100DaysOfCode #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Solved today’s LeetCode Daily Challenge — 2553. Separate the Digits in an Array This was a simple and interesting problem based on arrays and digit manipulation. The task was to take every number from the array and separate its digits while keeping the same order. Example: [13, 25, 83] → [1, 3, 2, 5, 8, 3] What I learned from this problem: Better understanding of digit extraction using % and / Handling array/list operations efficiently Keeping elements in the correct order while processing numbers Approach: Traverse each number Extract digits one by one Store them in the correct order Build the final answer array Time Complexity: O(total digits) Small problems like this help strengthen basic logic and coding fundamentals step by step. #LeetCode #DSA #Java #ProblemSolving #CodingJourney #DailyChallenge
To view or add a comment, sign in
-
-
🚀 Day 57/100 – LeetCode DSA Challenge 📌 Problem: 1365. How Many Numbers Are Smaller Than the Current Number 💡 Problem Statement : Given an array nums, for each element nums[i], count how many numbers in the array are smaller than it (excluding itself). 🧠 Approach to Solve: 🔹 Brute Force Approach (Easy & Intuitive): Use two loops For each element, compare it with all others Count how many are smaller Store result in output array 🔹 Better Approach (Sorting): Create a copy of array and sort it First occurrence index of each number = its count of smaller elements Use HashMap to store value → count 📚 What I Learned Today: ✔️ How to apply nested loops for comparison problems ✔️ Importance of index-wise result storage ✔️ Difference between brute force and optimized approaches ✔️ Strengthened problem-solving thinking in arrays ✨ Every day one problem closer to success! #Day57 #100DaysOfCode #LeetCode #DSA #Java #CodingJourney #Consistency #ProblemSolving
To view or add a comment, sign in
-