Given two arrays **A[]** and **B[]** containing **N** elements, the task is to find, for every element in the array **B[]**, the element which is just strictly greater than that element which is present in the array **A[]**. If no value is present, then print ‘null’.

**Note:** The value from the array A[] can only be used once.

**Examples:**

Input:A[] = {0, 1, 2, 3, 4}, B[] = {0, 1, 1, 2, 3}

Output:1 2 3 4 null

Explanation:

On iterating every element in the array B[]:

The value which is strictly greater than 0 and present in the array A[] is 1.

Similarly, the value which is strictly greater than 1 and present in the array A[] is 2.

Similarly, the value which is strictly greater than 1 and present in the array A[] is 3 because 2 has already been used for the previous 1.

Similarly, the value which is strictly greater than 2 and present in the array A[] is 4.

Now, there is no value in the array which is greater than 3 because 4 has already been used for the previous 2. So, null is printed.

Input:A[] = {0, 1, 6, 4, 0, 2, 4, 2, 4, 7}, B[] = {0, 1, 6, 4, 0, 2, 4, 2, 4, 7}

Output:1 2 7 6 2 4 null 4 null null

**Approach:** The idea is to use the Tree set Data structure. But since a tree set doesn’t support duplicate values, a hashmap is used to store the frequency of the elements.

- Iterate through the array A[].
- Add the elements in the array A[] into the tree set.
- Update their frequencies in the hashmap.
- Now, for every element in the array B[], find the value which is strictly greater than the current value by using the
**higher()**function of the tree set. - Now, reduce the frequency of this number in the hash map by 1.
- Keep repeating the above two steps until the frequency of the numbers become 0. If it is 0, then all the occurrences of that number have been used up for the elements. So, remove that element from the tree set.

Below is the implementation of the above approach:

`// Java program to find the values ` `// strictly greater than the element ` `// and present in the array ` ` ` `import` `java.io.*; ` `import` `java.util.*; ` `public` `class` `GFG { ` ` ` ` ` `// Function to find the values ` ` ` `// strictly greater than the element ` ` ` `// and present in the array ` ` ` `public` `static` `void` `operations( ` ` ` `int` `n, ` `long` `A[], ` `long` `B[]) ` ` ` `{ ` ` ` ` ` `// Treeset to store the ` ` ` `// values of the array A ` ` ` `TreeSet<Long> tree ` ` ` `= ` `new` `TreeSet<Long>(); ` ` ` ` ` `// HashMap to store the frequencies ` ` ` `// of the values in array A ` ` ` `HashMap<Long, Integer> freqMap ` ` ` `= ` `new` `HashMap<Long, Integer>(); ` ` ` ` ` `// Iterating through the array ` ` ` `// and add values in the treeset ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) { ` ` ` `long` `x = A[j]; ` ` ` `tree.add(x); ` ` ` ` ` `// Updating the frequencies ` ` ` `if` `(freqMap.containsKey(x)) { ` ` ` ` ` `freqMap.put(x, freqMap.get(x) + ` `1` `); ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `freqMap.put(x, ` `1` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Finding the strictly greater value ` ` ` `// in the array A[] using "higher()" ` ` ` `// function and also reducing the ` ` ` `// frequency of that value because it ` ` ` `// has to be used only once ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) { ` ` ` `long` `x = B[j]; ` ` ` ` ` `// If the higher value exists ` ` ` `if` `(tree.higher(x) != ` `null` `) { ` ` ` `System.out.print(tree.higher(x) + ` `" "` `); ` ` ` ` ` `// If the frequency value is 1 ` ` ` `// then remove it from treeset ` ` ` `// because it has been used ` ` ` `// and its frequency becomes 0 ` ` ` `if` `(freqMap.get(tree.higher(x)) == ` `1` `) { ` ` ` `tree.remove(tree.higher(x)); ` ` ` `} ` ` ` ` ` `// Else, reducing the frequency ` ` ` `// by 1 ` ` ` `else` `{ ` ` ` `freqMap.put( ` ` ` `tree.higher(x), ` ` ` `freqMap.get(tree.higher(x)) ` ` ` `- ` `1` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If the value is not present ` ` ` `// then print null ` ` ` `else` `{ ` ` ` `System.out.print(` `"null "` `); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `int` `n = ` `12` `; ` ` ` `long` `A[] = ` `new` `long` `[] { ` `9` `, ` `5` `, ` `100` `, ` `4` `, ` ` ` `89` `, ` `2` `, ` `0` `, ` `2` `, ` ` ` `89` `, ` `77` `, ` `77` `, ` `77` `}; ` ` ` `long` `B[] = ` `new` `long` `[] { ` `0` `, ` `18` `, ` `60` `, ` `34` `, ` ` ` `50` `, ` `29` `, ` `4` `, ` `20` `, ` ` ` `48` `, ` `77` `, ` `2` `, ` `8` `}; ` ` ` ` ` `operations(n, A, B); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2 77 77 77 89 89 5 100 null null 4 9

**Time Complexity:** *O(N * log(N))* because the insertion of one element takes log(N) in a tree set.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- First strictly greater element in a sorted array in Java
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find closest greater value for every element in array
- Find the next greater element in a Circular Array
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Closest greater element for every array element from another array
- First strictly smaller element in a sorted array in Java
- Check whether an array can be made strictly decreasing by modifying at most one element
- Maximum length of Strictly Increasing Sub-array after removing at most one element
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Check whether an array can be made strictly increasing by modifying atmost one element
- Find last element after deleting every second element in array of n integers
- Smallest element greater than X not present in the array
- Count of greater elements for each element in the Array
- Elements greater than the previous and next element in an Array
- Largest element smaller than current element on left for every element in Array
- Find an element in array such that sum of left array is equal to sum of right array
- Adding elements of an array until every element becomes greater than or equal to k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.