0% found this document useful (0 votes)
20 views10 pages

Try It On GFG Prac Ce

Uploaded by

4064Pasupathy T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Try It On GFG Prac Ce

Uploaded by

4064Pasupathy T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Given two sorted arrays a[] and b[] of size n and m respec vely, the task is to merge both

the
arrays and rearrange the elements such that the smallest n elements are in a[] and the
remaining m elements are in b[]. All elements in a[] and b[] should be in sorted order.

Examples:

Input: a[] = [2, 4, 7, 10], b[] = [2, 3]


Output: a[] = [2, 2, 3, 4], b[] = [7, 10]
Explana on: Combined sorted array = [2, 2, 3, 4, 7, 10], array a[] contains smallest 4 elements: 2, 2,
3 and 4, and array b[] contains remaining 2 elements: 7, 10.

Input: a[] = [1, 5, 9, 10, 15, 20], b[] = [2, 3, 8, 13]


Output: a[] = [1, 2, 3, 5, 8, 9], b[] = [10, 13, 15, 20]
Explana on: Combined sorted array = [1, 2, 3, 5, 8, 9, 10, 13, 15, 20], array a[] contains smallest 6
elements: 1, 2, 3, 5, 8 and 9, and array b[] contains remaining 4 elements: 10, 13, 15, 20.

Input: a[] = [0, 1], b[] = [2, 3]


Output: a[] = [0, 1], b[] = [2, 3]
Explana on: Combined sorted array = [0, 1, 2, 3], array a[] contains smallest 2 elements: 0 and 1,
and array b[] contains remaining 2 elements: 2 and 3.

Try it on GfG Prac ce

Table of Content

 Using Insert of Inser on Sort

 Using n-th smallest element

 By Swap and Sort

 Using Gap method

Using Insert of Inser on Sort

The idea is to traverse b[] from the end in reverse order and compare each element with the last
(largest) element of a[]. For any index i, if b[i] is smaller than the last element of a[],
replace b[i] with the last element of a[] and use insert step of inser on sort to find the correct
place of b[i] in a[].
How do we keep a[] sorted? Every me we add any element from b[] to a[], we find the correct
index using insert step of inser on sort.
How do we keep b[] sorted? This is ensured by the fact that we traverse b[] from end and insert
only when current element of b[] is smaller.

Illustra on
C++CJavaPythonC#JavaScript

// JavaScript Code to Merge two sorted arrays a[] and b[] without

// extra space using insert of inser on sort


3

func on mergeArrays(a, b) {

// Traverse b[] star ng from the last element

for (let i = b.length - 1; i >= 0; i--) {

// If b[i] is smaller than the largest element of a[]

10

if (a[a.length - 1] > b[i]) {

11

12

// Place b[i] in the correct posi on in a[],

13

// and move last element of a[] to b[]

14

let last = a[a.length - 1];

15

let j = a.length - 2;

16

while (j >= 0 && a[j] > b[i]) {

17

a[j + 1] = a[j];

18
j--;

19

20

a[j + 1] = b[i];

21

b[i] = last;

22

23

24

25

26

const a = [1, 5, 9, 10, 15, 20];

27

const b = [2, 3, 8, 13];

28

mergeArrays(a, b);

29

30

console.log(a.join(" "));

31

console.log(b.join(" "));

Output

123589

10 13 15 20
Time Complexity: O(m * n), where n and m are sizes of a[] and b[] respec vely.
Auxiliary Space: O(1)

Using n-th smallest element

We can use the fact that nth smallest elemen n the sorted combined array acts as a pivot dividing
the elements among a[] and b[]. Ini ally, this nth smallest element can lie in either arrays so
instead of finding it, we can find the first index idx in a[], such that the elements a er this index
were larger than the pivot.

Elements of a[] placed a er index idx should be replaced with smaller elements from b[]. Now all
elements were in the correct arrays and we can apply sor ng to both arrays to maintain the order.

1/6

C++CJavaPythonC#JavaScript

// JavaScript program to merge two sorted arrays a[] and b[]

// without extra space using n-th smallest number

// Find m-th smallest element

// Do a binary search in a[] to find the right index idx

// in a[] such that all combined elements in a[idx..m-1]

// and b[m-idx...n-1] are greater than or equal to all

// the remaining elements (a[0..idx-1] and b[m-idx-1..n-1])

10
// in both the arrays.

11

func on kthSmallest(a, b, k) {

12

const n = a.length, m = b.length;

13

let lo = 0, hi = n, idx = 0;

14

15

16

while (lo <= hi) {

17

const mid1 = Math.floor((lo + hi) / 2);

18

const mid2 = k - mid1;

19

20

// We don't have mid2 elements in b[], so pick more

21

// elements from a[]

22

if (mid2 > m) {

23

lo = mid1 + 1;

24

con nue;

25

}
26

27

// Find elements to the le and right of par on in a[]

28

const l1 = (mid1 === 0 ? Number.NEGATIVE_INFINITY : a[mid1 - 1]);

29

const r1 = (mid1 === n ? Number.POSITIVE_INFINITY : a[mid1]);

30

31

// Find elements to the le and right of par on in b[]

32

const l2 = (mid2 === 0 ? Number.NEGATIVE_INFINITY : b[mid2 - 1]);

33

const r2 = (mid2 === m ? Number.POSITIVE_INFINITY : b[mid2]);

34

35

// If it is a valid par on

36

if (l1 <= r2 && l2 <= r1) {

37

idx = mid1;

38

break;

39

40

41
// Check if we need to take lesser elements from a[]

42

if (l1 > r2) {

43

hi = mid1 - 1;

44

45

46

// Check if we need to take more elements from a[]

47

else {

48

lo = mid1 + 1;

49

50

51

return idx;

52

53

54

func on mergeArrays(a, b) {

55

const n = a.length;

56

const m = b.length;
57

const idx = kthSmallest(a, b, n);

58

59

// Move all smaller elements in a[]

60

for (let i = idx; i < n; i++) {

61

[a[i], b[i - idx]] = [b[i - idx], a[i]];

62

63

64

// Sort both a[] and b[]

65

a.sort((x, y) => x - y);

66

b.sort((x, y) => x - y);

67

68

69

const a = [1, 5, 9, 10, 15, 20];

70

const b = [2, 3, 8, 13];

71

mergeArrays(a, b);

72
73

console.log(a.join(" "));

74

console.log(b.join(" "));

Output

123589

10 13 15 20

You might also like