forked from IOSD/Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_2arrays.cpp
More file actions
54 lines (46 loc) · 1.16 KB
/
Copy pathmerge_2arrays.cpp
File metadata and controls
54 lines (46 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
using namespace std;
///program to merge two arrays
///time complexity= O(n)
///space complexity = O(1) extra space
/* Merges array N[] of size n into array mPlusN[]
of size m+n*/
int merge(int mPlusN[], int N[], int m, int n)
{
int i = n; /* Current index of i/p part of mPlusN[]*/
int j = 0; /* Current index of N[]*/
int k = 0; /* Current index of of output mPlusN[]*/
while (k < (m+n))
{
/* Take an element from mPlusN[] if
a) value of the picked element is smaller and we have
not reached end of it
b) We have reached end of N[] */
if ((i < (m+n) && mPlusN[i] <= N[j]) || (j == n))
{
mPlusN[k] = mPlusN[i];
k++;
i++;
}
else // Otherwise take element from N[]
{
mPlusN[k] = N[j];
k++;
j++;
}
}
}
int main()
{
/* Initialize arrays */
int mPlusN[] = {2, 8,NA,NA,NA, 13,NA, 15, 20};
int N[] = {5, 7, 9, 25};
int n = sizeof(N)/sizeof(N[0]);
int m = sizeof(mPlusN)/sizeof(mPlusN[0]) - n;
/*Merge N[] into mPlusN[] */
merge(mPlusN, N, m, n);
/* Print the resultant mPlusN */
for(int i=0;i<(m+n);i++)
cout<<mPlusN[i]<<endl;
return 0;
}