💻 LeetCode 50 Days Challenge — Day 7: Merge Sorted Array Day 7 of my #LeetCode50DaysChallenge ✅ Today’s problem was about merging two sorted arrays efficiently — Merge Sorted Array ✨ 🧩 Problem: You are given two sorted integer arrays nums1 and nums2, along with integers m and n, representing the number of valid elements in each array. The task is to merge nums2 into nums1 so that the result is a single array sorted in non-decreasing order, all done in-place without returning a new array. 💡 Approach: I started by appending all elements from nums2 to nums1 from the index m onward. Then, I simply used Java’s built-in Arrays.sort() to sort the combined array. Although this isn’t the most optimal in-place merge, it’s clean, concise, and leverages Java’s efficient sorting algorithm — perfect for understanding the fundamentals of merging. ⏱️ Time Complexity: O((m + n) log(m + n)) 📊 Example: Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Every problem solved adds up — like merging arrays, small consistent steps combine into something powerful. Keep going! 💪 #LeetCode #CodingChallenge #Day7 #ProblemSolving #Java #SoftwareDevelopment #Consistency #50DaysOfCode
Merging two sorted arrays in Java for LeetCode challenge
More Relevant Posts
-
💻 LeetCode Challenge – Day 6: Merge Two Sorted Lists Today’s challenge was all about linked lists — merging two sorted lists into one sorted list 🔗 🔹 Problem: Given two sorted linked lists, merge them into a single sorted linked list and return it. 🔹 Key Learnings: ✅ Deepened understanding of linked list traversal and pointers ✅ Practiced building a dummy node approach for cleaner logic ✅ Learned how to handle edge cases efficiently ✅ Reinforced the importance of iterative vs recursive thinking This problem really helped me think more clearly about data structure manipulation and clean code design. 💪 #LeetCode #100DaysOfCode #Java #CodingChallenge #ProblemSolving #LinkedList #MergeTwoSortedLists #CodingJourney
To view or add a comment, sign in
-
-
💡 LeetCode 1929 – Concatenation of Array 💡 Today, I solved LeetCode Problem #1929: Concatenation of Array, a simple yet satisfying problem that tests your understanding of array manipulation and indexing in Java. ⚙️📊 🧩 Problem Overview: You’re given an integer array nums. Your task is to create a new array ans such that ans = nums + nums (i.e., concatenate the array with itself). 👉 Example: Input → nums = [1,2,1] Output → [1,2,1,1,2,1] 💡 Approach: 1️⃣ Find the length n of the given array. 2️⃣ Create a new array of size 2 * n. 3️⃣ Loop through nums once — place each element both at index i and index n + i. 4️⃣ Return the resulting array. ⚙️ Complexity Analysis: ✅ Time Complexity: O(n) — Single traversal of the array. ✅ Space Complexity: O(n) — For the concatenated array. ✨ Key Takeaways: Practiced index manipulation and array construction. Reinforced the importance of efficient iteration. A great warm-up problem that strengthens logical thinking and array fundamentals. 🌱 Reflection: Even the simplest problems build the foundation for solving more complex ones. Every bit of consistent practice helps in mastering problem-solving and clean coding habits. 🚀 #LeetCode #1929 #Java #ArrayManipulation #DSA #CodingJourney #CleanCode #ProblemSolving #AlgorithmicThinking #ConsistencyIsKey
To view or add a comment, sign in
-
-
🚀 Day 63 of My LeetCode journey 🚀 Problem : Custom Sort String Today’s problem was interesting because instead of sorting using normal alphabetical order, we sort the given string based on a custom priority order. 🧠 Problem Understanding Given: order → defines priority of characters. str → the input string which needs to be rearranged. Goal: ✅ Arrange characters of str based on the sequence defined in order. ✅ Characters not present in order should appear at the end (any order). 💡 Approach (Simple & Efficient) Count frequency of each character in str. First append characters following the order. Then append remaining characters. Time Complexity: O(n) Space Complexity: O(1) (fixed array size for 26 letters) ✨ Learnings Sometimes the problem isn't about sorting, but about following a custom priority. Frequency counting is extremely powerful for character-based problems. StringBuilder → best way to build strings efficiently in Java. #leetcode #coding #dsa #java #100DaysOfCode #day63 #learningEveryday #problemSolving
To view or add a comment, sign in
-
-
🌳 Day 65 of #LeetCode Journey 🔹 Problem: 106. Construct Binary Tree from Inorder and Postorder Traversal 🔹 Difficulty: Medium 🔹 Language: Java Today’s problem is about rebuilding a binary tree when you’re given its inorder and postorder traversal arrays. 🧩 Key Idea: The last element in postorder is always the root. In inorder traversal, elements to the left of the root are in the left subtree, and elements to the right are in the right subtree. We recursively build the tree using this property. 💡 Approach: Start from the end of the postorder array to get the root. Use a hashmap to store inorder indices for O(1) lookup. Recursively construct the right subtree first, then the left subtree (since we’re moving backward in postorder). 🧠 Concepts reinforced: Recursion HashMap for index lookup Understanding inorder & postorder relationships 🔥 Another step forward in mastering tree construction problems! #LeetCode #100DaysOfCode #Java #CodingChallenge #DataStructures #BinaryTree #Recursion #ProblemSolving #ProgrammingJourney
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
-
-
🌟 Problem 21 – Merge Two Sorted Lists I solved this problem today on LeetCode 💪. It helped me understand how to work with linked lists and how to merge them efficiently. 🧩 Description: Given two sorted linked lists, merge them into one sorted list and return its head. 👉 Example: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] ✅ Approach: * Compare nodes from both lists one by one. * Add the smaller value to the new list. * Continue until all elements are merged. 📈 Time Complexity: O(n + m) 📦 Space Complexity: O(1) #LeetCode #TopInterview150 #Java #DSA #Coding #ProblemSolving #LinkedList
To view or add a comment, sign in
-
-
💻 LeetCode 50 Days Challenge — Day 3: Remove Duplicates from Sorted Array Day 3 of my #LeetCode50DaysChallenge ✅ Today’s problem was about array manipulation — Remove Duplicates from Sorted Array ✨ 🧩 Problem: Given an integer array nums sorted in non-decreasing order, remove duplicates in-place such that each unique element appears only once. The relative order of the elements should remain the same. 💡 Approach: This problem is a classic two-pointer approach! One pointer i keeps track of the last unique element’s position. The other pointer j iterates through the array. Whenever a new element is found (nums[i] != nums[j]), we move it forward by incrementing i and assigning nums[i] = nums[j]. In the end, i + 1 gives the count of unique elements. A simple yet elegant technique to modify arrays in-place! ⏱️ Time Complexity: O(n) 📊 Example: Input: [0,0,1,1,1,2,2,3,3,4] Output: [0,1,2,3,4] Consistency is the secret ingredient to progress! 🌱 Each problem solved adds another brick to the wall of mastery 💪 #LeetCode #CodingChallenge #Day3 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
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
-
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