Created
January 5, 2020 04:23
-
-
Save shubham1710/1da48b4a1b4ae9838855fe274e187e2e to your computer and use it in GitHub Desktop.
Uy
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.lang.*; | |
| import java.io.*; | |
| class GFG | |
| { | |
| public static void main (String[] args) | |
| { | |
| //code | |
| Scanner sc=new Scanner(System.in); | |
| int t=sc.nextInt();int n,m,i; | |
| while(t-->0) | |
| { | |
| n=sc.nextInt(); | |
| m=sc.nextInt(); | |
| int a[]=new int[n]; | |
| int b[]=new int[m]; | |
| for(i=0;i<n;i++) | |
| a[i]=sc.nextInt(); | |
| for(i=0;i<m;i++) | |
| b[i]=sc.nextInt(); | |
| int s[]=new int[n+m]; | |
| for(i=0;i<n;i++) | |
| s[i]=a[i]; | |
| for(i=0;i<m;i++) | |
| s[n+i]=b[i]; | |
| buildHeap(s,s.length); | |
| for(i=0;i<s.length;i++) | |
| System.out.print(s[i]+" "); | |
| System.out.println(); | |
| } | |
| } | |
| static void heapify(int a[],int n,int i) | |
| { | |
| int largest=i; | |
| int l=2*i+1; | |
| int r=2*i+2; | |
| if(l<n && a[l]>a[largest]) | |
| largest=l; | |
| else | |
| largest=i; | |
| if(r< n && a[r]>a[largest]) | |
| largest=r; | |
| if(largest!=i) | |
| { | |
| int temp=a[i]; | |
| a[i]=a[largest]; | |
| a[largest]=temp; | |
| heapify(a,n,largest); | |
| } | |
| } | |
| static void buildHeap(int a[],int n) | |
| { | |
| for(int i=n/2-1;i>=0;i--) | |
| { | |
| heapify(a,n,i); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment