VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-of-middle-elements-of-two-sorted-arrays/

⇱ Sum of middle elements of two sorted Arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of middle elements of two sorted Arrays

Last Updated : 23 Jul, 2025

Given two sorted arrays, arr1[] and arr2[], each of size N, the task is to merge the arrays and find the sum of the two middle elements of the merged array.

Example:

Input: N = 5, arr1[] = {1,2,4,6,10}, arr2[] = {4,5,6,9,12}
Output: 11
Explanation: The merged array is {1,2,4,4,5,6,6,9,10,12}. The sum of the middle elements (5 and 6) is 11.

Input: N = 5, arr1[] = {1,12,15,26,38}, arr2[]={2,13,17,30,45}
Output: 32
Explanation: The merged array is {1,2,12,13,15,17,26,30,38,45}. The sum of the middle elements (15 and 17) is 32.

[Naive Approach] Using Merging Arrays with space- O(n) time and O(n) auxiliary space

The simplest way to solve the problem is to merge the two sorted arrays into one single sorted array. Merging two sorted array can be done by initializing a new array merged[] of size 2*N, we traverse both arr1[] and arr2[] simultaneously using two pointers. We compare the current elements of both arrays and append the smaller element to merged[]. Once all elements are merged, the middle two elements are at positions N-1 and N, and their sum is the required result.


Output
11

[Optimized Approach] Using Merging Arrays without space - O(n) time and O(1) auxiliary space

In the optimized approach, we eliminate the need for an auxiliary array by keeping track of the middle elements during a single pass merge. Using two pointers for arr1[] and arr2[], and two variables m1 and m2 to store the middle elements, we traverse both arrays. At each step, we update m1 to the previous m2 and set m2 to the smaller current element of the two arrays. This continues until we reach the middle of the merged array, ensuring m1 and m2 hold the middle elements.


Output
11

Time Complexity: O(n)
Auxiliary Space: O(1), Since we have used count, m1 and m2 to keep track of the middle elements

[Expected Approach] Using Binary search - O(log(n)) time and O(1) auxiliary space

Prerequisite:Median of two sorted array

The most efficient approach use binary search to find the correct partition point in one of the arrays, this ensuring the two halves contain the middle elements. By setting initial search range pointers low and high, we use binary search to find the partition cut1 in arr1[] and cut2 in arr2[]. We then check if the largest elements of the left partitions and the smallest elements of the right partitions form a valid split. If not, we adjust the search range accordingly. Once the partitions are valid, the middle elements are determined by the maximum of the left partition elements and the minimum of the right partition elements.

Detailed Intuition:

  • Initialize Search Range: Set low = 0 and high = N to define the search range within arr1[].
  • Binary Search Partition: Use binary search to find the partition point cut1 in arr1[].
    • Calculate the corresponding partition point cut2 in arr2[].
  • Determine Partition Validity:
    • Define l1 as the largest element on the left side of arr1[] partition, and l2 as the largest element on the left side of arr2[] partition.
    • Define r1 as the smallest element on the right side of arr1[] partition, and r2 as the smallest element on the right side of arr2[] partition.
  • Adjust Search Range: Check if the partitions are valid:
    • If l1 <= r2 and l2 <= r1, the partitions are valid.
    • If l1 > r2, adjust the search range to high = cut1 - 1.
    • If l2 > r1, adjust the search range to low = cut1 + 1.
  • Find Middle Elements: Once the partitions are valid, the middle elements are max(l1, l2) and min(r1, r2). Sum these elements to get the result.

Output
11
Comment
Article Tags: