Skip to content

Commit

Permalink
solved: merged two sorted arrays -@iamserda
Browse files Browse the repository at this point in the history
  • Loading branch information
iamserda committed Feb 14, 2025
1 parent a7e0cde commit 2432935
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions neetcodeio/merge_sorted_arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def merge_sorted_arrays(arr1, arr2):
arr3 = []
ptr1 = 0
ptr2 = 0
while ptr1 < len(arr1) or ptr2 < len(arr2):
if ptr1 >= len(arr1) or arr1[ptr1] >= arr2[ptr2]:
arr3.append(arr2[ptr2])
ptr2 += 1
continue
if ptr2 >= len(arr2) or arr1[ptr1] <= arr2[ptr2]:
arr3.append(arr1[ptr1])
ptr1 += 1
continue
break

return arr3


arr1 = [1, 3, 5, 7]
arr2 = [0, 2, 4, 5, 7, 8]
arr3 = merge_sorted_arrays(arr1, arr2)
print(arr3)
assert arr3 == sorted([*arr1, *arr2])

0 comments on commit 2432935

Please sign in to comment.