Merge K sorted array with a helper function