Merge two sorted arrays in O(1) extra space using Heap
Last Updated :
26 Jul, 2021
Given two sorted arrays, arr[], brr[] of size N, and M, the task is to merge the two given arrays such that they form a sorted sequence of integers combining elements of both the arrays.
Examples:
Input: arr[] = {10}, brr[] = {2, 3}
Output: 2 3 10
Explanation: The merged sorted array obtained by taking all the elements from the both the arrays is {2, 3, 10}.
Therefore, the required output is 2 3 10.
Input: arr[] = {1, 5, 9, 10, 15, 20}, brr[] = {2, 3, 8, 13}
Output: 1 2 3 5 8 9 10 13 15 20
Naive Approach: Refer to Merge two sorted arrays for the simplest approach to merge the two given arrays.
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Space Optimized Approach: Refer to Merge two sorted arrays with O(1) extra space to merge the two given arrays without using any extra memory.
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Efficient Space Optimized Approach: Refer to Efficiently merging two sorted arrays with O(1) extra space to merge the two given array without using any extra memory.
Time Complexity: O((N + M) * log(N + M))
Auxiliary Space: O(1)
Heap – based Approach: The problem can be solved using Heap. The idea is to traverse arr[] array and compare the value of arr[i] with brr[0] and check if arr[i] is greater than brr[0] or not. If found to be true then swap(arr[i], brr[0) and perform the heapify operation on brr[]. Follow the steps below to solve the problem:
- Traverse the array arr[] and compare the value of arr[i] with brr[0] and check if arr[i] is greater than brr[0] or not. If found to be true then swap(arr[i], brr[0) and perform the heapify operation on brr[].
- Finally, sort the array brr[] and print both arrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minHeapify( int brr[], int i, int M)
{
int left = 2 * i + 1;
int right = 2 * i + 2;
int smallest = i;
if (left < M && brr[left] < brr[smallest]) {
smallest = left;
}
if (right < M && brr[right] < brr[smallest]) {
smallest = right;
}
if (smallest != i) {
swap(brr[i], brr[smallest]);
minHeapify(brr, smallest, M);
}
}
void merge( int arr[], int brr[],
int N, int M)
{
for ( int i = 0; i < N; ++i) {
if (arr[i] > brr[0]) {
swap(arr[i], brr[0]);
minHeapify(brr, 0, M);
}
}
sort(brr, brr + M);
}
void printArray( int arr[], int N)
{
for ( int i = 0; i < N; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 2, 23, 35, 235, 2335 };
int brr[] = { 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = sizeof (brr) / sizeof (brr[0]);
merge(arr, brr, N, M);
printArray(arr, N);
printArray(brr, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minHeapify( int brr[],
int i, int M)
{
int left = 2 * i + 1 ;
int right = 2 * i + 2 ;
int smallest = i;
if (left < M && brr[left] <
brr[smallest])
{
smallest = left;
}
if (right < M && brr[right] <
brr[smallest])
{
smallest = right;
}
if (smallest != i)
{
int temp = brr[i];
brr[i] = brr[smallest];
brr[smallest] = temp;
minHeapify(brr, smallest, M);
}
}
static void merge( int arr[], int brr[],
int N, int M)
{
for ( int i = 0 ; i < N; ++i)
{
if (arr[i] > brr[ 0 ])
{
int temp = arr[i];
arr[i] = brr[ 0 ];
brr[ 0 ] = temp;
minHeapify(brr, 0 , M);
}
}
Arrays.sort(brr);
}
static void printArray( int arr[],
int N)
{
for ( int i = 0 ; i < N; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 2 , 23 , 35 , 235 , 2335 };
int brr[] = { 3 , 5 };
int N = arr.length;
int M = brr.length;
merge(arr, brr, N, M);
printArray(arr, N);
printArray(brr, M);
}
}
|
Python3
def minHeapify(brr, i, M):
left = 2 * i + 1
right = 2 * i + 2
smallest = i
if (left < M and brr[left] < brr[smallest]):
smallest = left
if (right < M and brr[right] < brr[smallest]):
smallest = right
if (smallest ! = i):
brr[i], brr[smallest] = brr[smallest], brr[i]
minHeapify(brr, smallest, M)
def merge(arr, brr, N, M):
for i in range (N):
if (arr[i] > brr[ 0 ]):
arr[i], brr[ 0 ] = brr[ 0 ], arr[i]
minHeapify(brr, 0 , M)
brr.sort()
def printArray(arr, N):
for i in range (N):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 2 , 23 , 35 , 235 , 2335 ]
brr = [ 3 , 5 ]
N = len (arr)
M = len (brr)
merge(arr, brr, N, M)
printArray(arr, N)
printArray(brr, M)
|
C#
using System;
class GFG{
static void minHeapify( int []brr,
int i, int M)
{
int left = 2 * i + 1;
int right = 2 * i + 2;
int smallest = i;
if (left < M && brr[left] <
brr[smallest])
{
smallest = left;
}
if (right < M && brr[right] <
brr[smallest])
{
smallest = right;
}
if (smallest != i)
{
int temp = brr[i];
brr[i] = brr[smallest];
brr[smallest] = temp;
minHeapify(brr, smallest, M);
}
}
static void merge( int []arr, int []brr,
int N, int M)
{
for ( int i = 0; i < N; ++i)
{
if (arr[i] > brr[0])
{
int temp = arr[i];
arr[i] = brr[0];
brr[0] = temp;
minHeapify(brr, 0, M);
}
}
Array.Sort(brr);
}
static void printArray( int []arr,
int N)
{
for ( int i = 0; i < N; i++)
Console.Write(arr[i] + " " );
}
public static void Main(String[] args)
{
int []arr = { 2, 23, 35, 235, 2335 };
int []brr = {3, 5};
int N = arr.Length;
int M = brr.Length;
merge(arr, brr, N, M);
printArray(arr, N);
printArray(brr, M);
}
}
|
Javascript
<script>
function minHeapify(brr, i, M)
{
let left = 2 * i + 1;
let right = 2 * i + 2;
let smallest = i;
if (left < M && brr[left] < brr[smallest]) {
smallest = left;
}
if (right < M && brr[right] < brr[smallest]) {
smallest = right;
}
if (smallest != i) {
let temp = brr[i];
brr[i] = brr[smallest];
brr[smallest] = temp;
minHeapify(brr, smallest, M);
}
}
function merge(arr, brr,
N, M)
{
for (let i = 0; i < N; ++i) {
if (arr[i] > brr[0]) {
let temp = arr[i];
arr[i] = brr[0];
brr[0] = temp;
minHeapify(brr, 0, M);
}
}
brr.sort();
}
function printArray(arr, N)
{
for (let i = 0; i < N; i++)
document.write(arr[i] + " " );
}
let arr = [ 2, 23, 35, 235, 2335 ];
let brr = [ 3, 5 ];
let N = arr.length;
let M = brr.length;
merge(arr, brr, N, M);
printArray(arr, N);
printArray(brr, M);
</script>
|
Output:
2 3 5 23 35 235 2335
Time Complexity: O((N + M) * log (M))
Auxiliary Space: O(1)
Efficient Approach: Refer to merge two sorted arrays to efficiently merge the two given arrays.
Time Complexity: O(N + M)
Auxiliary Space: O(N + M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...