🔹 Day 36: Merge Sorted Array (LeetCode #88) 📌 Problem Statement: You are given two sorted arrays, nums1 and nums2, and two integers m and n representing the number of initialized elements in each. Merge nums2 into nums1 as one sorted array in non-decreasing order. ✅ My Approach: I appended all elements of nums2 into the extra space of nums1 and then sorted the combined array. While this is a simple approach, an optimized method could merge from the end to avoid extra sorting. 📊 Complexity: Time Complexity: O((m + n) log(m + n)) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 1 ms (Beats 27.92%) Memory: 42.32 MB 💡 Reflection: This problem reinforces the importance of understanding in-place array manipulation. A clean and straightforward implementation that still gets the job done! 💪 #LeetCode #Java #Arrays #Sorting #100DaysOfCode #Day36
Merging Sorted Arrays in O(1) Space Complexity
More Relevant Posts
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
🔹 Day 50: Maximum Average Subarray I (LeetCode #643) 📌 Problem Statement: Given an integer array nums and an integer k, find the contiguous subarray of length k that has the maximum average value and return this value. ✅ My Approach: I used the sliding window technique to efficiently calculate the sum of subarrays of length k. First, I computed the sum of the first k elements. Then, as the window slid forward, I subtracted the element going out and added the new element entering the window. I continuously updated the maximum sum encountered and finally returned the average by dividing it by k. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 99.90%) Memory: 56.27 MB (Beats 66.43%) 💡 Reflection: This problem highlighted how the sliding window technique can drastically improve performance over recalculating sums for each subarray, making the approach both elegant and efficient. 🚀 #LeetCode #Java #SlidingWindow #Optimization #100DaysOfCode #Day50
To view or add a comment, sign in
-
-
💡 LeetCode #88 — Merge Sorted Array Today I solved LeetCode Problem 88: Merge Sorted Array 🧩 Problem Summary: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. All merging should happen in-place, meaning you can’t use extra space for another array. Key Idea: Use three pointers starting from the end: i → last valid element in nums1 j → last element in nums2 k → last index in nums1 (total length) Compare elements from the back and fill nums1 from the end — this avoids overwriting elements we haven’t processed yet. #LeetCode #Java #DSA #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 68 String Manipulation Problem 1:- Largest Odd Number in String Task:- Given a numeric string, return the largest-valued odd number (as a substring) or an empty string if none exists. Example: Input: num = "35427" → Output: "35427" My Approach: Started scanning the string from right to left. The first odd digit encountered marks the end of the required substring. Returned the substring from start to that index. Time Complexity:- O(N) Space Complexity:- O(1) Sometimes, it’s not about complex algorithms just a small logical observation can lead to an efficient solution. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #CodeNewbie #StringManipulation #LogicBuilding #CleanCode
To view or add a comment, sign in
-
-
💻 Day 34 — LeetCode 912: Sort an Array (Merge Sort Implementation) Today, I learned and implemented Merge Sort, a classic Divide and Conquer algorithm that efficiently sorts arrays with a time complexity of O(n log n). I applied it to LeetCode 912 (Sort an Array) — where the task is to sort an integer array without using built-in sorting methods. This problem helped me understand: How recursion splits the array into halves How merging combines sorted halves Why Merge Sort guarantees stable and consistent performance 🧠 Key takeaway: Merge Sort ensures O(n log n) in all cases (best, average, and worst), making it one of the most reliable sorting algorithms. #LeetCode #Day34 #DSA #SortingAlgorithms #MergeSort #CodingJourney #Java #ProblemSolving
To view or add a comment, sign in
-
-
#Day_34 Today’s problem was a satisfying one to solve — “Single Element in a Sorted Array” 🔍 Given a sorted array where every element appears twice except for one unique element, the task is to identify that single element. At first, it seems like a simple linear scan could do the job — and yes, it can. But I focused on building a clean and logical O(n) approach before thinking about optimization 🧠 Approach We observe that: Every element appears in pairs, and because the array is sorted, duplicates are adjacent. The unique element is the only one that doesn’t match its left or right neighbor. We can handle three cases: First element: If it’s not equal to the next one → it’s the single element. Last element: If it’s not equal to the previous one → it’s the single element. Middle elements: If an element is different from both its previous and next → that’s our answer. This way, we can detect the single element in just one pass through the array ⏱ Complexity Time: O(n) — We traverse the array once. Space: O(1) — No extra memory used. 💬 Reflection This problem is a good reminder that not every efficient solution has to start complex. Sometimes, the cleanest brute-force version gives a perfect foundation for further optimization — in this case, even leading to an O(log n) binary search variant. Each problem in this challenge pushes me to think deeper about patterns, structure, and clarity in problem-solving. #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode #Java #LearnByDoing #DSA
To view or add a comment, sign in
-
-
📌 Day 21/100 – Sort an Array (LeetCode 912) 🔹 Problem: Given an integer array, sort it in ascending order without using built-in sorting functions and ensure the solution runs in O(n log n) time. 🔹 Approach (Merge Sort): Divide the array into two halves recursively Sort each half independently Merge the two sorted halves while maintaining order Uses Divide & Conquer and guarantees stable sorting 🔹 Key Learnings: Merge Sort ensures worst-case O(n log n) time unlike QuickSort Works well for linked lists & large datasets because of stability Uses extra space due to temporary list merging Great example of recursion + two-pointer merging technique #100DaysOfCode #Day21 #LeetCode #Java #DSA #MergeSort #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
✅Day 41 : Leetcode 153 - Find Minimum in Rotated Sorted Array #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a sorted array that has been rotated at an unknown pivot, find the minimum element in the array. The array contains unique elements, and the solution must run in O(log n) time. 💡 My Approach I used a binary search technique to efficiently find the minimum element. I maintained two pointers, low and high. At each step, I calculated the mid-point. If the left part (nums[low] to nums[mid]) was sorted, I updated my answer with the smaller of nums[low] and current ans, and moved low to mid + 1. Otherwise, I updated my answer with nums[mid] and moved high to mid - 1. This approach ensures we keep narrowing the search space toward the minimum element. ⏱️ Time Complexity O(log n) — Because the search space is halved in each iteration. #BinarySearch #LeetCode #RotatedSortedArray #DSA #CodingPractice #Java #ProblemSolving
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 81 Linked List Cycle Problem Given the head of a linked list, determine whether the list contains a cycle meaning a node’s next pointer refers back to a previous node. My Approach Used Floyd’s Cycle Detection Algorithm (Tortoise & Hare Method): Initialized two pointers slow and fast. Moved slow one step and fast two steps in each iteration. If they ever meet → cycle detected. If fast or fast.next becomes null → no cycle exists. Complexity Time: O(n) Space: O(1) Even in problems involving dynamic structures like linked lists, a simple pointer-based approach can lead to an elegant and optimal solution. #100DaysOfCode #LeetCode #Java #ProblemSolving #DataStructures #LinkedList #TortoiseAndHare #takeUforward #CodeNewbie #CodingJourney
To view or add a comment, sign in
-
-
Day 15 of #251DaysOfDSA Problem: Rotate Image ✨ What I learned: This problem helped me understand how to rotate a matrix in-place using just basic array manipulation. The trick was simple but elegant — first transpose the matrix, then reverse each row. A great example of how breaking down problems into smaller transformations can make them easy to solve. #251DaysOfDSA #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
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