Created
February 28, 2021 10:52
-
-
Save kaiwalyakoparkar/39100a52794dcba7b82f003f367b8313 to your computer and use it in GitHub Desktop.
For medium article on 28 /02/ 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Solution{ | |
| public static void main(String args[]){ | |
| Scanner sc = new Scanner(System.in); | |
| int n = sc.nextInt(); | |
| int m = sc.nextInt(); | |
| int a[] = new int[n]; | |
| for(int i = 0; i < a.length; i++){ | |
| a[i] = sc.nextInt(); | |
| } | |
| int b[] = new int[m]; | |
| for(int i = 0; i < b.length; i++){ | |
| b[i] = sc.nextInt(); | |
| } | |
| //This is swapping approach | |
| int k = n-1, j = 0; | |
| while(k >=0 && j <= m){ | |
| if(a[k] > b[j]){ | |
| int temp = a[k]; | |
| a[k] = b[j]; | |
| b[j] = temp; | |
| } | |
| k--; | |
| j++; | |
| } | |
| Arrays.sort(a); | |
| Arrays.sort(b); | |
| System.out.println("Sorted array is: "); | |
| for(int i = 0; i < a.length; i++){ | |
| System.out.print(a[i]+" "); | |
| } | |
| for(int i = 0; i < b.length; i++){ | |
| System.out.print(b[i]+" "); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment