📌  相关文章
📜  为第二个数组中的每个元素从第一个数组中查找严格更大的元素

📅  最后修改于: 2021-09-06 06:31:56             🧑  作者: Mango

给定两个包含N 个元素的数组A[]B[] ,任务是为数组B[]中的每个元素找到严格大于数组A[] 中存在的元素的元素。如果不存在值,则打印“null”。

注意:数组 A[] 中的值只能使用一次。

例子:

做法:思路是使用树集数据结构。但是由于树集不支持重复值,因此使用哈希图来存储元素的频率。

  • 遍历数组 A[]。
  • 将数组 A[] 中的元素添加到树集中。
  • 更新它们在哈希图中的频率。
  • 现在,对于数组 B[] 中的每个元素,使用树集的high()函数找到严格大于当前值的值。
  • 现在,将这个数字在哈希图中的频率减少 1。
  • 不断重复以上两步,直到数字出现的频率为0。如果为0,则表示该数字出现的所有元素都已用完。因此,从树集中删除该元素。

下面是上述方法的实现:

C++
// C++ program to find the values
// strictly greater than the element
// and present in the array
#include
using namespace std;
 
// Function to find the values
// strictly greater than the element
// and present in the array
void operations(int n, long long A[],
                       long long B[])
{
     
    // Treeset to store the
    // values of the array A
    settree;
     
    // HashMap to store the frequencies
    // of the values in array A
    mapfreqMap;
 
    // Iterating through the array
    // and add values in the treeset
    for(int j = 0; j < n; j++)
    {
        long long x = A[j];
        tree.insert(x);
        freqMap[x]++;
    }
 
    // 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 long  x = B[j];
 
        // If the higher value exists
        if (tree.upper_bound(x) != tree.end())
        {
            cout << *tree.upper_bound(x) << " ";
             
            // If the frequency value is 1
            // then remove it from treeset
            // because it has been used
            // and its frequency becomes 0
            if (freqMap[*tree.upper_bound(x)] == 1)
            {
                tree.erase(*tree.upper_bound(x));
            }
             
            // Else, reducing the frequency
            // by 1
            else
            {
                freqMap[*tree.upper_bound(x)]--;
            }
        }
 
        // If the value is not present
        // then print null
        else
        {
            cout << "null ";
        }
    }
}
 
// Driver code
int main()
{
    int n = 12;
    long long A[] = { 9, 5, 100, 4, 89, 2,
                      0, 2, 89, 77, 77, 77 };
    long long B[] = { 0, 18, 60, 34, 50, 29,
                      4, 20, 48, 77, 2, 8 };
     
    operations(n, A, B);
}
 
// This code is contributed by Stream_Cipher


Java
// 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 tree
            = new TreeSet();
 
        // HashMap to store the frequencies
        // of the values in array A
        HashMap freqMap
            = new HashMap();
 
        // 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);
    }
}


Python3
# Python program to find the values
# strictly greater than the element
# and present in the array
from typing import List
from bisect import bisect_right
 
# Function to find the values
# strictly greater than the element
# and present in the array
def operations(n: int, A: List[int], B: List[int]) -> None:
 
    # Treeset to store the
    # values of the array A
    tree = set()
 
    # HashMap to store the frequencies
    # of the values in array A
    freqMap = dict()
 
    # Iterating through the array
    # and add values in the treeset
    for j in range(n):
        x = A[j]
        tree.add(x)
        if x not in freqMap:
            freqMap[x] = 0
        freqMap[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 j in range(n):
        x = B[j]
 
        # If the higher value exists
        sset = sorted(list(tree))
        index = bisect_right(sset, x)
        if index < len(tree):
            print(sset[index], end=" ")
 
            # If the frequency value is 1
            # then remove it from treeset
            # because it has been used
            # and its frequency becomes 0
            if (freqMap[sset[index]] == 1):
                tree.remove(sset[index])
 
            # Else, reducing the frequency
            # by 1
            else:
                freqMap[sset[index]] -= 1
 
        # If the value is not present
        # then print null
        else:
            print("null", end=" ")
 
# Driver code
if __name__ == "__main__":
 
    n = 12
    A = [9, 5, 100, 4, 89, 2, 0, 2, 89, 77, 77, 77]
    B = [0, 18, 60, 34, 50, 29, 4, 20, 48, 77, 2, 8]
 
    operations(n, A, B)
 
# This code is contributed by sanjeev2552


输出:
2 77 77 77 89 89 5 100 null null 4 9

时间复杂度: O(N * log(N))因为在树集中插入一个元素需要 log(N)。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live