chore(Java): add merge without extra space for two array (#592)

pull/576/head^2
Suraj Kumar Modi 2021-10-19 18:40:00 +05:30 committed by GitHub
parent 96c9f520fb
commit d4b94e4b27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 0 deletions

View File

@ -10,6 +10,7 @@
- [K-th Element of Two Sorted Arrays](arrays/kth-element--orted-array.java)
- [Trapping Rain Water](arrays/trapping-rain-water.java)
- [Ugly Number](arrays/ugly-number.java)
- [Merge Without Extra Space](arrays/merge-without-extra-space.java)
## Graphs
- [Dijkstras](graphs/Dijkstras.java)

View File

@ -0,0 +1,95 @@
// Program to merge two sorted arrays without any extra space in Java
/*
Author : Suraj Kumar Modi
Github : https://github.com/skmodi649
*/
/** Algorithm :
* 1) Initialize i,j as n-1,0 where n is size of arr1
* 2) Iterate through every element of arr1 and arr2 using two pointers i and j respectively
* while(i>=0 && j<m)
* if arr1[i] is more than arr2[j]
* swap arr1[i] and arr2[j]
* else
* decrement i
* increment j
*
* 3) Sort both arr1 and arr2
*/
import java.util.*;
class MergingWithoutExtraSpace {
// Driver code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sizes of arr1 and arr2 respectively : ");
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr1 = new int[n];
int[] arr2 = new int[m];
System.out.println("Enter elements in the arr1 : ");
for (int i = 0; i < n; i++) {
arr1[i] = sc.nextInt();
}
System.out.println("Enter elements in the arr2 : ");
for (int i = 0; i < m; i++) {
arr2[i] = sc.nextInt();
}
System.out.println("Merged array : ");
MergingWithoutExtraSpace ob = new MergingWithoutExtraSpace();
ob.merge(arr1 , arr2 , n , m);
StringBuffer str = new StringBuffer();
for (int i = 0; i < n; i++) {
str.append(arr1[i]).append(" ");
}
for (int i = 0; i < m; i++) {
str.append(arr2[i]).append(" ");
}
System.out.println(str);
}
public void merge(int[] arr1, int[] arr2, int n, int m) {
int i = n-1 , j = 0;
while(i>=0 && j<m)
{
if(arr1[i] > arr2[j])
{
int temp = arr1[i];
arr1[i] = arr2[j];
arr2[j] = temp;
}
i--;
j++;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
}
}
/** TEST CASES :
*
* Test Case 1 :
* Input : n = 3 , m = 4
* arr1[] = {1,27,36} arr2[] = {12,14,16,22}
* Output :
* 1 12 14 16 22 27 36
*
* Test Case 2 :
* Input : n = 4 , m = 5
* arr1[] = {0,12,21,27} arr2[] = {4,6,8,10,12}
* Output :
* 0 4 6 8 10 12 12 21 27
*/
/**
* Time Complexity : O((n+m)log(n+m))
* Auxiliary Space Complexity : O(1)
*/