Skip to content

add median of two sorted array #9386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 4, 2023
Merged
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 2, 2023
commit fdc5914d39b6b88f40fe7cfe4f9ad0e8e8f5939e
5 changes: 3 additions & 2 deletions data_structures/arrays/median_two_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float
total = len(merged)

if total % 2 == 1:
# If the total number of elements is odd, return the
# If the total number of elements is odd, return the
# middle element as the median.
return float(merged[total // 2])
else:
# If the total number of elements is even, calculate
# If the total number of elements is even, calculate
# the average of the two middle elements as the median.
middle1 = merged[total // 2 - 1]
middle2 = merged[total // 2]
return (float(middle1) + float(middle2)) / 2.0


if __name__ == "__main__":
import doctest

Expand Down