📌  相关文章
📜  打印 Q 查询的值范围 [A, B] 内给定数组中的所有重复元素

📅  最后修改于: 2022-05-13 01:57:35.914000             🧑  作者: Mango

打印 Q 查询的值范围 [A, B] 内给定数组中的所有重复元素

给定一个大小为N的数组arr[]和形式为[A, B]Q个查询,任务是从数组中找到所有重复且它们的值介于AB (均包含)之间的唯一元素每个Q查询。

例子:

朴素的方法:解决这个问题的基本方法是使用嵌套循环。外层循环遍历所有元素,内层循环检查外层循环选取的元素是否出现在其他任何地方。然后,如果它出现在其他地方,请检查它是否在[A, B]的范围内。如果是,则打印它。

时间复杂度: O(Q * N 2 )
辅助空间: O(1)

高效方法:一种有效的方法是基于哈希映射的思想来存储所有元素的频率。请按照以下步骤操作:

  • 遍历hash map,检查某个元素出现的频率是否大于1。
  • 如果是,则检查元素是否存在于[A, B]的范围内。
  • 如果是,则打印它,否则跳过该元素。
  • 继续上述过程,直到遍历完哈希图的所有元素。

下面是上述方法的实现。

C++
// C++ code for the above approach.
#include 
using namespace std;
 
// Initializing hashmap to store
// Frequency of elements
unordered_map freq;
 
// Function to store frequency of elements in hash map
 
void storeFrequency(int arr[], int n)
{
    // Iterating the array
    for (int i = 0; i < n; i++) {
        freq[arr[i]]++;
    }
}
 
// Function to print elements
void printElements(int a, int b)
{
 
    // Traversing the hash map
    for (auto it = freq.begin(); it != freq.end(); it++) {
 
        // Checking 1st condition if frequency > 1, i.e,
        // Element is repetitive
 
        if ((it->second) > 1) {
 
            int value = it->first;
 
            // Checking 2nd condition if element
            // Is in range of a and b or not
            if (value >= a && value <= b) {
                // Printing the value
                cout << value << " ";
            }
        }
    }
}
 
// Function to find the elements
// satisfying given condition for each query
void findElements(int arr[], int N, int Q,
                  vector >& queries)
{
    storeFrequency(arr, N);
    for (int i = 0; i < Q; i++) {
        int A = queries[i].first;
        int B = queries[i].second;
        printElements(A, B);
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 1, 5, 1, 2, 3, 3, 4, 0, 0 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
    int Q = 2;
    vector > queries = { { 1, 3 }, { 0, 0 } };
 
    // Function call
    findElements(arr, N, Q, queries);
    return 0;
}


Python3
# Python 3 code for the above approach.
from collections import defaultdict
 
# Initializing hashmap to store
# Frequency of elements
freq = defaultdict(int)
 
# Function to store frequency of elements in hash map
def storeFrequency(arr, n):
 
    # Iterating the array
    for i in range(n):
        freq[arr[i]] += 1
 
# Function to print elements
def printElements(a, b):
 
    # Traversing the hash map
    for it in freq:
 
        # Checking 1st condition if frequency > 1, i.e,
        # Element is repetitive
        # print("it = ",it)
        if ((freq[it]) > 1):
            value = it
 
            # Checking 2nd condition if element
            # Is in range of a and b or not
            if (value >= a and value <= b):
               
                # Printing the value
                print(value, end=" ")
 
# Function to find the elements
# satisfying given condition for each query
def findElements(arr, N,  Q,
                 queries):
 
    storeFrequency(arr, N)
    for i in range(Q):
        A = queries[i][0]
        B = queries[i][1]
        printElements(A, B)
        print()
 
# Driver code
if __name__ == "__main__":
 
    arr = [1, 5, 1, 2, 3, 3, 4, 0, 0]
 
    # Size of array
    N = len(arr)
    Q = 2
    queries = [[1, 3], [0, 0]]
 
    # Function call
    findElements(arr, N, Q, queries)
 
    # This code is contributed by ukasp.


输出
3 1 
0 

时间复杂度: O(Q*N + N)
辅助空间: O(N)