📌  相关文章
📜  最大化可被K整除的和对的数量

📅  最后修改于: 2021-06-26 09:17:33             🧑  作者: Mango

给定一个由N个整数和一个整数K组成的数组。任务是打印可能被K整除的最大对数(a [i] + a [j])。

注意:一个特定的索引号不能以超过一对的形式考虑。

例子:

天真的方法:天真的方法是使用两个循环进行迭代,并计算总和可被K整除的对的总数。这种方法的时间复杂度为O(N ^ 2)。

高效的方法:一种有效的方法将是使用哈希技术来解决该问题。可以按照以下步骤解决上述问题。

  • 最初,为每个数组元素将hash [a [i]%k]增加一。
  • 在地图中进行迭代并获取所有可能的哈希值。
  • 如果哈希值为0,则对的数量将为hash [0] / 2。
  • 之后,对于每个哈希值x,我们可以使用(hash [x],hash [kx])的最小值并使用它们创建对。
  • 相应地从哈希值中减去使用的对的数量。

下面是上述方法的实现。

C++
// C++ program to implement the above
// approach
#include 
using namespace std;
  
// Function to maximize the number of pairs
int findMaximumPairs(int a[], int n, int k)
{
  
    // Hash-table
    unordered_map hash;
    for (int i = 0; i < n; i++)
        hash[a[i] % k]++;
  
    int count = 0;
  
    // Iterate for all numbers less than hash values
    for (auto it : hash) {
  
        // If the number is 0
        if (it.first == 0) {
  
            // We take half since same number
            count += it.second / 2;
            if (it.first % 2 == 0)
                hash[it.first] = 0;
            else
                hash[it.first] = 1;
        }
        else {
  
            int first = it.first;
            int second = k - it.first;
  
            // Check for minimal occurrence
            if (hash[first] < hash[second]) {
                // Take the minimal
                count += hash[first];
  
                // Subtract the pairs used
                hash[second] -= hash[first];
                hash[first] = 0;
            }
            else if (hash[first] > hash[second]) {
                // Take the minimal
                count += hash[second];
  
                // Subtract the pairs used
                hash[first] -= hash[second];
                hash[second] = 0;
            }
            else {
                // Check if numbers are same
                if (first == second) {
  
                    // If same then number of pairs will be half
                    count += it.second / 2;
  
                    // Check for remaining
                    if (it.first % 2 == 0)
                        hash[it.first] = 0;
                    else
                        hash[it.first] = 1;
                }
                else {
  
                    // Store the number of pairs
                    count += hash[first];
                    hash[first] = 0;
                    hash[second] = 0;
                }
            }
        }
    }
  
    return count;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 2, 3, 2, 4, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 2;
    cout << findMaximumPairs(a, n, k);
  
    return 0;
}


Java
// Java program to implement the above
// approach
import java.util.*;
  
class GFG
{
  
// Function to maximize the number of pairs
static int findMaximumPairs(int a[], int n, int k)
{
  
    // Hash-table
    HashMap hash = new HashMap();
    for (int i = 0; i < n; i++)
        if(hash.containsKey(a[i] % k)){
            hash.put(a[i] % k, hash.get(a[i] % k)+1);
        }
        else{
            hash.put(a[i] % k, 1);
        }
  
    int count = 0;
  
    // Iterate for all numbers less than hash values
    for (Map.Entry it : hash.entrySet()){
  
        // If the number is 0
        if (it.getKey() == 0) {
  
            // We take half since same number
            count += it.getValue() / 2;
            if (it.getKey() % 2 == 0)
                hash.put(it.getKey(), 0);
            else
                hash.put(it.getKey(), 1);
        }
        else {
  
            int first = it.getKey();
            int second = k - it.getKey();
  
            // Check for minimal occurrence
            if (hash.get(first) < hash.get(second)) 
            {
                  
                // Take the minimal
                count += hash.get(first);
  
                // Subtract the pairs used
                hash.put(second, hash.get(second)-hash.get(first));
                hash.put(first, 0);
            }
            else if (hash.get(first) > hash.get(second))
            {
                  
                // Take the minimal
                count += hash.get(second);
  
                // Subtract the pairs used
                hash.put(first, hash.get(first)-hash.get(second));
                hash.put(second, 0);
            }
            else 
            {
                // Check if numbers are same
                if (first == second) {
  
                    // If same then number of pairs will be half
                    count += it.getValue() / 2;
  
                    // Check for remaining
                    if (it.getKey() % 2 == 0)
                        hash.put(it.getKey(), 0);
                    else
                        hash.put(it.getKey(), 1);
                }
                else {
  
                    // Store the number of pairs
                    count += hash.get(first);
                    hash.put(first, 0);
                    hash.put(second, 0);
                }
            }
        }
    }
  
    return count;
}
  
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 2, 3, 2, 4, 10 };
    int n = a.length;
    int k = 2;
    System.out.print(findMaximumPairs(a, n, k));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement 
# the above approach 
  
# Function to maximize the
# number of pairs 
def findMaximumPairs(a, n, k) : 
  
    # Hash-table 
    hash = {}; 
    for i in range(n) :
        if a[i] % k not in hash :
            hash[a[i] % k] = 0
          
        hash[a[i] % k] += 1
  
    count = 0; 
  
    # Iterate for all numbers less 
    # than hash values 
    for keys,values in hash.items() :
  
        # If the number is 0 
        if (keys == 0) :
  
            # We take half since same number 
            count += values // 2; 
            if (keys % 2 == 0) :
                hash[keys] = 0; 
            else :
                hash[keys] = 1; 
                  
        else :
  
            first = keys; 
            second = k -keys; 
  
            # Check for minimal occurrence 
            if (hash[first] < hash[second]) :
                  
                # Take the minimal 
                count += hash[first]; 
  
                # Subtract the pairs used 
                hash[second] -= hash[first]; 
                hash[first] = 0; 
              
            elif (hash[first] > hash[second]) : 
                  
                # Take the minimal 
                count += hash[second]; 
  
                # Subtract the pairs used 
                hash[first] -= hash[second]; 
                hash[second] = 0; 
              
            else :
                  
                # Check if numbers are same 
                if (first == second) :
  
                    # If same then number of pairs 
                    # will be half 
                    count += values // 2; 
  
                    # Check for remaining 
                    if (keys % 2 == 0) :
                        hash[keys] = 0; 
                    else :
                        hash[keys] = 1; 
                  
                else :
  
                    # Store the number of pairs 
                    count += hash[first]; 
                    hash[first] = 0; 
                    hash[second] = 0; 
                      
    return count; 
  
# Driver code 
if __name__ == "__main__" :
  
    a = [ 1, 2, 2, 3, 2, 4, 10 ]; 
    n = len(a)
    k = 2; 
    print(findMaximumPairs(a, n, k)); 
  
# This code is contributed by Ryuga


输出:
3

时间复杂度: O(max(N,K))

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。