Reputation: 29
Trying to attempt to solve 88. Merge Sorted Array with C# using a for and a while loop. Leetcode instructs to save the correct output in [nums1] array instead of a return output. These are the assumptions and inputs for the merge sorted array.
public void Merge(int[] nums1, int m, int[] nums2, int n) {
if (m == 0)
{
Console.WriteLine("Num 1 Before: {0}", nums1[0]);
nums1 = (int[])nums2.Clone();
Console.WriteLine("Num 1 After: {0}", nums1[0]);
}
else if ( n != 0 )
{
int arrTwoIt = 0;
for ( int i = m; i <m+n; i++)
{
nums1[i] = nums2[arrTwoIt++];
}
int arrOneIt = 0;
arrTwoIt = m;
while (arrOneIt < m)
{
if (nums1[arrOneIt] > nums1[arrTwoIt])
{
int leftItemTemp = nums1[arrOneIt];
nums1[arrOneIt] = nums1[arrTwoIt];
nums1[arrTwoIt++] = leftItemTemp;
}
arrOneIt++;
}
}
}
I am not sure where to go from there. Im not sure what I am missing. Any help is appreciated.
Upvotes: 0
Views: 571
Reputation: 3731
Try this simple 3 pointer approach:
namespace CodingInterview;
public static class LeetCodeQuestions
{
// Question: https://leetcode.com/problems/merge-sorted-array/?envType=study-plan-v2&envId=top-interview-150
// Time complexity: O(m+n) because we're looping backwards through m+n elements
// Space complexity: O(1) - Constant because we're not creating any new data structures that scale with input size
public static void MergeSortedArrays(int[] nums1, int m, int[] nums2, int n) {
// 3 pointer approach
var i = m - 1;
var j = n - 1;
var k = m + n - 1;
// Loop from k to 0
while (k > -1){
// Means we sorted all of nums1, but we still have to place nums2 elements
if(i < 0){
nums1[k] = nums2[j];
j--;
}
// Means we sorted all of nums2, but there are elements in nums1 but they're already sorted, so you can just get out
else if (j < 0){
break;
}
else if (nums1[i] > nums2[j]){
nums1[k] = nums1[i];
i--;
}
else{
nums1[k] = nums2[j];
j--;
}
k--;
}
}
}
Test it. I'm using xUnit.
namespace CodingInterview.Tests;
public class LeetCodeQuestionsTests
{
[Theory]
// Test Cases 1-3
[InlineData(new[] {1, 2, 3, 0, 0, 0}, 3, new[] {2, 5, 6}, 3, new[] {1, 2, 2, 3, 5, 6})]
[InlineData(new[] {1}, 1, new int[] {}, 0, new[] {1})]
[InlineData(new[] {0}, 0, new[] {1}, 1, new[] {1})]
public void Merge_Sorted_Arrays(int[] nums1, int m, int[] nums2, int n, int[] expected)
{
// Act
LeetCodeQuestions.MergeSortedArrays(nums1, m, nums2, n);
// Assert
Assert.Equal(expected, nums1);
}
}
Upvotes: 0
Reputation: 67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console1.Core.Algorithms
{
class Merge_Sorted_Array
{
static void Main(string[] args)
{
int[] nums1 = { 1, 2, 3, 0, 0, 0 }; //here you can put your values
int[] nums2 = { 2, 5, 6 }; //here you can put your values
int count = 0;
for (int i = 0; i < nums1.Length; i++)
{
count++;
}
for (int i = 0; i < nums2.Length; i++)
{
count++;
}
int[] nums3 = new int[count];
for (int i = 0; i < nums1.Length; i++)
{
nums3[i] = nums1[i];
}
int d = nums1.Length;
for (int i = 0; i < nums2.Length; i++)
{
nums3[d] = nums2[i];
d++;
}
List<int> arrayList = new List<int>();
for (int i = 0; i < nums3.Length; i++)
{
if (nums3.ElementAt(i).Equals(0))
{
continue;
}
else
{
arrayList.Add(nums3.ElementAt(i));
}
}
arrayList.Sort();
int[] ans = new int[arrayList.Count];
for (int i = 0; i < arrayList.Count; i++)
{
ans[i] = arrayList.ElementAt(i);
}
for (int i = 0; i < ans.Length; i++)
{
Console.WriteLine(ans.ElementAt(i));
}
}
}
}
Upvotes: -1
Reputation: 639
You can merge two arrays first & Simply call Array.Sort()
method. If you don't want to use any in-Built method, you can sort with your custom logic.
public void Merge(int[] nums1, int m, int[] nums2, int n) {
for(int i = 0; i < nums2.Length; i++){
nums1[i + m] = nums2[i];
}
Array.Sort(nums1);
}
Upvotes: 0