Merge Sorted Array - Array - Easy - LeetCode
💻 coding

Merge Sorted Array - Array - Easy - LeetCode

1 min read 50 words
1 min read
ShareWhatsAppPost on X
  • 1The Merge function combines two sorted arrays into the first array in non-decreasing order.
  • 2It uses a reverse iteration approach to efficiently merge elements from both arrays.
  • 3The algorithm operates in O(m+n) time complexity, where m and n are the lengths of the input arrays.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The Merge function combines two sorted arrays into the first array in non-decreasing order."

Merge Sorted Array - Array - Easy - LeetCode

public class Solution {
 public void Merge(int[] nums1, int m, int[] nums2, int n) {
 int i=m-1;
 int j=n-1;
 for(int x=m+n-1;x>=0;x--){
 if(i >= 0 && j >= 0){
 if(nums1[i]>nums2[j]){
 nums1[x]=nums1[i];
 i--;
 }
 else{
 nums1[x]=nums2[j];
 j--;
 }
 }
 else{
 if(i>=0){
 nums1[x]=nums1[i];
 i--;
 }
 if(j>=0){
 nums1[x]=nums2[j];
 j--;
 }
 }
 
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 25 September 2020 · 1 min read · 50 words

Part of AskGif Blog · coding

You might also like