Last active
February 4, 2022 16:16
-
-
Save ozkanpakdil/5643cf1130dee457e5bff62572abe38c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int[] mergeTwoSortedIntArray(int[] foo, int[] bar) { | |
int fooLength = foo.length; | |
int barLength = bar.length; | |
int[] merged = new int[fooLength + barLength]; | |
int fooPosition, barPosition, mergedPosition; | |
fooPosition = barPosition = mergedPosition = 0; | |
while (fooPosition < fooLength && barPosition < barLength) { | |
if (foo[fooPosition] < bar[barPosition]) { | |
merged[mergedPosition++] = foo[fooPosition++]; | |
} else { | |
merged[mergedPosition++] = bar[barPosition++]; | |
} | |
} | |
while (fooPosition < fooLength) { | |
merged[mergedPosition++] = foo[fooPosition++]; | |
} | |
while (barPosition < barLength) { | |
merged[mergedPosition++] = bar[barPosition++]; | |
} | |
return merged; | |
} | |
//time complexity O(fooLength + barLength) | |
//space complexity O(fooLength + barLength) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment