Merging two sorted arrays in Java for LeetCode challenge

💻 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

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories