📌  相关文章
📜  满足给定条件的数组可能的最大子集

📅  最后修改于: 2021-04-27 18:32:06             🧑  作者: Mango

给定一个数组arr []和一个整数K。任务是找到最大子集的大小,以使子集(X,Y)中的每一对都具有Y!=(X * K)的形式,其中X

例子:

方法:

  • 对所有数组元素进行排序。
  • 创建一个空的整数集S ,该整数集将保存子集的元素。
  • 遍历排序后的数组,并对该数组中的每个整数x进行遍历:
    • 如果x%k = 0x / kS中尚不存在,则将x插入S中
    • 否则丢弃x并检查下一个元素。
  • 最后打印集S的大小。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the size of the required sub-set
int sizeSubSet(int a[], int k, int n)
{
    // Sort the array
    sort(a, a + n);
  
    // Set to store the contents of the required sub-set
    unordered_set s;
  
    // Insert the elements satisfying the conditions
    for (int i = 0; i < n; i++) {
        if (a[i] % k != 0 || s.count(a[i] / k) == 0)
            s.insert(a[i]);
    }
  
    // Return the size of the set
    return s.size();
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 2;
  
    cout << sizeSubSet(a, k, n);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
      
// Function to return the size of the required sub-set
static int sizeSubSet(int a[], int k, int n)
{
    // Sort the array
    Arrays.sort(a);
  
    // HashMap to store the contents
    // of the required sub-set
    HashMap< Integer, Integer> s = new HashMap< Integer, Integer>();
      
    // Insert the elements satisfying the conditions
    for (int i = 0; i < n; i++)
    {
        if (a[i] % k != 0 || s.get(a[i] / k) == null)
            s.put(a[i], s.get(a[i]) == null ? 1 : s.get(a[i]) + 1);
    }
  
    // Return the size of the set
    return s.size();
}
  
// Driver code
public static void main(String args[])
{
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int n = a.length;
    int k = 2;
    System.out.println( sizeSubSet(a, k, n));
}
}
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
  
import math as mt 
# Function to return the size of the required sub-set
def sizeSubSet(a, k, n):
  
    # Sort the array
    a.sort()
   
    # Set to store the contents of the required sub-set
    s=set()
   
    # Insert the elements satisfying the conditions
    for i in range(n):
        if (a[i] % k != 0 or a[i] // k not in s):
            s.add(a[i])
      
   
    # Return the size of the set
    return len(s)
  
   
# Driver code
a=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
n = len(a)
k = 2
  
print(sizeSubSet(a, k, n))
  
# This is contributed by Mohit kumar 29


C#
// C# mplementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
      
// Function to return the size of 
// the required sub-set
static int sizeSubSet(int []a, int k, int n)
{
    // Sort the array
    Array.Sort(a);
  
    // HashMap to store the contents
    // of the required sub-set
    Dictionary s = new Dictionary();
      
    // Insert the elements satisfying the conditions
    for (int i = 0; i < n; i++)
    {
        if (a[i] % k != 0 || !s.ContainsKey(a[i] / k))
        {
            if(s.ContainsKey(a[i]))
            {
                var val = s[a[i]];
                s.Remove(a[i]);
                s.Add(a[i], val + 1); 
            }
            else
            {
                s.Add(a[i], 1);
            }
        }
    }
  
    // Return the size of the set
    return s.Count;
}
  
// Driver code
public static void Main(String []args)
{
    int []a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int n = a.Length;
    int k = 2;
    Console.WriteLine(sizeSubSet(a, k, n));
}
}
  
// This code is contributed by PrinciRaj1992


PHP


输出:
6
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”