📜  查找最大可能的k乘数集

📅  最后修改于: 2021-04-24 20:12:04             🧑  作者: Mango

给定一个包含不同的正整数和整数k的数组。任务是从给定元素的数组中找到最大可能的k乘数集。
如果集合中没有两个元素(即x,y)退出,从而y = x * k,则该集合称为k多重集合。
可能有多个答案。您可以打印其中任何一个。
例子:

方法:一种有效的方法是对给定的元素数组进行排序并遍历整个数组,如果集合中不包含等于x / k的元素(其中x可以被k整除),则将元素x推入集合中。
下面是上述方法的实现:

C++
// C++ program to find the largest
// possible k-multiple set
#include 
using namespace std;
  
// Function to find the largest
// possible k-multiple set
void K_multiple(int a[], int n, int k)
{
    // Sort the given array
    sort(a, a + n);
  
    // To store k-multiple set
    set s;
  
    // Traverse through the whole array
    for (int i = 0; i < n; i++) {
        // Check if x/k is already present or not
        if ((a[i] % k == 0 && s.find(a[i] / k) == s.end())
             || a[i] % k != 0)
            s.insert(a[i]);
    }
  
    // Print the k-multiple set
    for (auto i = s.begin(); i != s.end(); i++){
        cout << *i << " ";}
}
  
// Driver code
int main()
{
    int a[] = { 2, 3, 4, 5, 6, 10 } ;
    int k = 2;
  
    int n = sizeof(a) / sizeof(a[0]);
  
    // Function call
    K_multiple(a, n, k);
  
    return 0;
}


Java
// Java program to find the largest
// possible k-multiple set
import java.util.*;
 
class GFG
{
 
// Function to find the largest
// possible k-multiple set
static void K_multiple(int a[], int n, int k)
{
    // Sort the given array
    Arrays.sort(a);
 
    // To store k-multiple set
    HashSet s = new HashSet<>();
 
    // Traverse through the whole array
    for (int i = 0; i < n; i++)
    {
        // Check if x/k is already present or not
        if ((a[i] % k == 0 && !s.contains(a[i] / k))
            || a[i] % k != 0)
            s.add(a[i]);
    }
 
    // Print the k-multiple set
    for (Integer i:s)
        System.out.print(i+" ");
}
 
// Driver code
public static void main(String args[])
{
    int a[] = { 2, 3, 4, 5, 6, 10 } ;
    int k = 2;
 
    int n = a.length;
 
    // Function call
    K_multiple(a, n, k);
}
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 program to find the largest
# possible k-multiple set
 
# Function to find the largest
# possible k-multiple set
def K_multiple(a, n, k) :
 
    # Sort the given array
    a.sort();
 
    # To store k-multiple set
    s = set();
 
    # Traverse through the whole array
    for i in range(n) :
         
        # Check if x/k is already present or not
        if ((a[i] % k == 0 and
             a[i] // k not in s ) or a[i] % k != 0) :
            s.add(a[i]);
             
    # Print the k-multiple set
    for i in s :
        print(i, end = " ")
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 2, 3, 4, 5, 6, 10 ];
    k = 2;
 
    n = len(a);
 
    # Function call
    K_multiple(a, n, k);
 
# This code is contributed by AnkitRai01


C#
// C# program to find the largest
// possible k-multiple set
using System;
using System.Collections.Generic;
public class GFG
{
 
// Function to find the largest
// possible k-multiple set
static void K_multiple(int []a, int n, int k)
{
    // Sort the given array
    Array.Sort(a);
 
    // To store k-multiple set
    HashSet s = new HashSet();
 
    // Traverse through the whole array
    for (int i = 0; i < n; i++)
    {
        // Check if x/k is already present or not
        if ((a[i] % k == 0 && !s.Contains(a[i] / k))
            || a[i] % k != 0)
            s.Add(a[i]);
    }
 
    // Print the k-multiple set
    foreach (int i in s)
        Console.Write(i+" ");
}
 
// Driver code
public static void Main(String []args)
{
    int []a = { 2, 3, 4, 5, 6, 10 } ;
    int k = 2;
 
    int n = a.Length;
 
    // Function call
    K_multiple(a, n, k);
}
}
 
// This code has been contributed by 29AjayKumar


输出
2 3 5 

时间复杂度: O(N * log(N))