Java Program for Maximize elements using another array Last Updated : 24 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appearance of elements is kept same in output as in input.Examples: Input : arr1[] = {2, 4, 3} arr2[] = {5, 6, 1} Output : 5 6 4 As 5, 6 and 4 are maximum elements from two arrays giving second array higher priority. Order of elements is same in output as in input.Input : arr1[] = {7, 4, 8, 0, 1} arr2[] = {9, 7, 2, 3, 6} Output : 9 7 6 4 8 Approach : We create an auxiliary array of size 2*n and store the elements of 2nd array in auxiliary array, and then we will store elements of 1st array in it. After that we will sort auxiliary array in decreasing order. To keep the order of elements according to input arrays we will use hash table. We will store 1st n largest unique elements of auxiliary array in hash table. Now we traverse the second array and store that elements of second array in auxiliary array that are present in hash table. Similarly we will traverse first array and store the elements that are present in hash table. In this way we get n unique and largest elements from both the arrays in auxiliary array while keeping the order of appearance of elements same.Below is the implementation of above approach : Java // Java program to print the maximum elements // giving second array higher priority import java.util.*; class GFG { // Function to maximize array elements static void maximizeArray(int[] arr1,int[] arr2) { // auxiliary array arr3 to store // elements of arr1 & arr2 int arr3[] = new int[10]; for(int i = 0; i < arr3.length; i++) { //arr2 has high priority arr3[i] = 0; } // Arraylist to store n largest // unique elements ArrayList<Integer> al = new ArrayList<Integer>(); for(int i = 0; i < arr2.length; i++) { if(arr3[arr2[i]] == 0) { // to avoid repetition of digits of arr2 in arr3 arr3[arr2[i]] = 2; // simultaneously setting arraylist to // preserve order of arr2 and arr3 al.add(arr2[i]); } } for(int i = 0; i < arr1.length; i++) { if(arr3[arr1[i]] == 0) { // if digit is already present in arr2 // then priority is arr2 arr3[arr1[i]] = 1; // simultaneously setting arraylist to // preserve order of arr1 al.add(arr1[i]); } } // to get only highest n elements(arr2+arr1) // and remove others from arraylist int count = 0; for(int j = 9; j >= 0; j--) { if(count < arr1.length & (arr3[j] == 2 || arr3[j] == 1)) { // to not allow those elements // which are absent in both arrays count++; } else { al.remove(Integer.valueOf(j)); } } int i = 0; for(int x:al) { arr1[i++] = x; } } // Function to print array elements static void printArray(int[] arr) { for(int x:arr) { System.out.print(x + " "); } } // Driver Code public static void main(String args[]) { int arr1[] = {7, 4, 8, 0, 1}; int arr2[] = {9, 7, 2, 3, 6}; maximizeArray(arr1,arr2); printArray(arr1); } } // This code is contributed by KhwajaBilkhis Output: 9 7 6 4 8 Time complexity: O(n * log n).Auxiliary Space: O(n) as list has been created. Please refer complete article on Maximize elements using another array for more details! Comment More infoAdvertise with us Next Article Java Program for Maximize elements using another array kartik Follow Improve Article Tags : Java Sorting Hash Java Programs DSA Arrays cpp-unordered_set +3 More Practice Tags : ArraysHashJavaSorting Similar Reads Java Program to Minimize the Maximum Element of an Array Given two integers N and K. Create an array of N positive integers such that the sum of all elements of the array is divisible by K and the maximum element in the array is the minimum possible. You have to find the Maximum element of that array. Example: Input : N=5, K=11 Output : 3 Explanation : We 4 min read Java Program for Maximum equilibrium sum in an array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[]. Examples : Input : arr[] = {-1, 2, 3, 0, 3, 2, -1} Output : 4 Prefix sum of arr[0..3] = Suffix sum of arr[3..6] Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2} Output : 7 Prefix sum of arr[0..3] = Su 4 min read Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou 4 min read Java Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app 4 min read Java Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example :  Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 1 5 min read How to Find the Maximum Element in an Array? In Java, the array is a data structure that allows the users to store data of the same type in contiguous memory locations. To find the maximum element in an Array in Java, we can sort the array in ascending order using the Arrays.sort() method and then we can access the last element of the array wh 1 min read Java Program to Get the Maximum Element From a Vector Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we l 4 min read Finding Maximum Element of Java ArrayList For finding the maximum element in the ArrayList, complete traversal of the ArrayList is required. There is an inbuilt function in the ArrayList class to find the maximum element in the ArrayList, i.e. Time Complexity is O(N), where N is the size of ArrayList, Let's discuss both the methods. Example 2 min read Java Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each ele 2 min read Java Program to Find the Maximum Element in a Matrix Given a multi-dimensional 2D matrix of n rows and m column order N à M. The task is to find the maximum element in the given matrix. Illustration: Input : mat[][] = { {1,3,4,19}, {11,10,12,1}, {7,9,0,4,99} } Output : 99 Methods: Iterative method (naive approach)Using the principle of recursion (Bit 3 min read Like