📌  相关文章
📜  计算所有不同数字都出现在 K 中的数组元素

📅  最后修改于: 2021-10-27 08:50:02             🧑  作者: Mango

给定一个由N 个正整数和一个正整数K组成的数组arr[] ,任务是找到其不同数字是K数字的子集的数组元素的计数。

例子:

朴素的方法:解决问题的最简单的方法是遍历数组arr[]并对于每个数组元素,检查其所有不同的数字是否都出现在K 中。如果发现为真,则增加计数。最后,打印获得的计数。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check a digit occurs
// in the digit of K or not
static bool isValidDigit(int digit, int K)
{
     
    // Iterate over all possible
    // digits of K
    while (K != 0)
    {
         
        // If current digit
        // equal to digit
        if (K % 10 == digit)
        {
            return true;
        }
         
        // Update K
        K = K / 10;
    }
    return false;
}
 
// Function to find the count of array
// elements whose distinct digits are
// a subset of digits of K
int noOfValidNumbers(int K, int arr[], int n)
{
     
    // Stores count of array elements
    // whose distinct digits are subset
    // of digits of K
    int count = 0;
 
    // Traverse the array, []arr
    for(int i = 0; i < n; i++)
    {
         
        // Stores the current element
        int no = arr[i];
 
        // Check if all the digits arr[i]
        // is a subset of the digits of
        // K or not
        bool flag = true;
 
        // Iterate over all possible
        // digits of arr[i]
        while (no != 0)
        {
             
            // Stores current digit
            int digit = no % 10;
             
            // If current digit does not
            // appear in K
            if (!isValidDigit(digit, K))
            {
                 
                // Update flag
                flag = false;
                break;
            }
             
            // Update no
            no = no / 10;
        }
 
        // If all the digits arr[i]
        // appear in K
        if (flag == true)
        {
             
            // Update count
            count++;
        }
    }
 
    // Finally print count
    return count;
}
 
// Driver Code
int main()
{
    int K = 12;
    int arr[] = { 1, 12, 1222, 13, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
         
    cout << noOfValidNumbers(K, arr, n);
     
    return 0;
}
 
// This code is contributed by susmitakundugoaldanga


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to check a digit occurs
    // in the digit of K or not
    static boolean isValidDigit(int digit, int K)
    {
        // Iterate over all possible
        // digits of K
        while (K != 0) {
 
            // If current digit
            // equal to digit
            if (K % 10 == digit) {
                return true;
            }
 
            // Update K
            K = K / 10;
        }
 
        return false;
    }
 
    // Function to find the count of array elements
    // whose distinct digits are a subset of digits of K
    static int noOfValidNumbers(int K, int arr[])
    {
 
        // Stores count of array elements
        // whose distinct digits are subset
        // of digits of K
        int count = 0;
 
        // Traverse the array, arr[]
        for (int i = 0; i < arr.length; i++) {
 
            // Stores the current element
            int no = arr[i];
 
            // Check if all the digits arr[i]
            // is a subset of the digits of
            // K or not
            boolean flag = true;
 
            // Iterate over all possible
            // digits of arr[i]
            while (no != 0) {
 
                // Stores current digit
                int digit = no % 10;
 
                // If current digit does not
                // appear in K
                if (!isValidDigit(digit, K)) {
 
                    // Update flag
                    flag = false;
                    break;
                }
 
                // Update no
                no = no / 10;
            }
 
            // If all the digits arr[i] appear in K
            if (flag == true) {
 
                // Update count
                count++;
            }
        }
 
        // Finally print count
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int K = 12;
        int arr[] = { 1, 12, 1222, 13, 2 };
        System.out.println(noOfValidNumbers(K, arr));
    }
}


Python3
# Python3 program for the above approach
 
# Function to check a digit occurs
# in the digit of K or not
def isValidDigit(digit, K):
 
    # Iterate over all possible
    # digits of K
    while (K != 0):
 
        # If current digit
        # equal to digit
        if (K % 10 == digit):
            return True
 
        # Update K
        K = K // 10
    return False
 
# Function to find the count of array
# elements whose distinct digits are
# a subset of digits of K
def noOfValidNumbers(K, arr, n):
 
    # Stores count of array elements
    # whose distinct digits are subset
    # of digits of K
    count = 0
 
    # Traverse the array, []arr
    for i in range(n):
 
        # Stores the current element
        no = arr[i]
 
        # Check if all the digits arr[i]
        # is a subset of the digits of
        # K or not
        flag = True
 
        # Iterate over all possible
        # digits of arr[i]
        while (no != 0):
 
            # Stores current digit
            digit = no % 10
 
            # If current digit does not
            # appear in K
            if (not isValidDigit(digit, K)):
 
                # Update flag
                flag = False
                break
 
            # Update no
            no = no // 10
 
        # If all the digits arr[i]
        # appear in K
        if (flag == True):
 
            # Update count
            count += 1
 
    # Finally print count
    return count
 
# Driver Code
if __name__ == "__main__":
    K = 12
    arr = [1, 12, 1222, 13, 2]
    n = len(arr)
    print(noOfValidNumbers(K, arr, n))
 
    # This code is contributed by chitranayal.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check a digit occurs
// in the digit of K or not
static bool isValidDigit(int digit, int K)
{
     
    // Iterate over all possible
    // digits of K
    while (K != 0)
    {
         
        // If current digit
        // equal to digit
        if (K % 10 == digit)
        {
            return true;
        }
 
        // Update K
        K = K / 10;
    }
 
    return false;
}
 
// Function to find the count of array elements
// whose distinct digits are a subset of digits of K
static int noOfValidNumbers(int K, int []arr)
{
     
    // Stores count of array elements
    // whose distinct digits are subset
    // of digits of K
    int count = 0;
 
    // Traverse the array, []arr
    for(int i = 0; i < arr.Length; i++)
    {
         
        // Stores the current element
        int no = arr[i];
 
        // Check if all the digits arr[i]
        // is a subset of the digits of
        // K or not
        bool flag = true;
 
        // Iterate over all possible
        // digits of arr[i]
        while (no != 0)
        {
             
            // Stores current digit
            int digit = no % 10;
 
            // If current digit does not
            // appear in K
            if (!isValidDigit(digit, K))
            {
                 
                // Update flag
                flag = false;
                break;
            }
             
            // Update no
            no = no / 10;
        }
 
        // If all the digits arr[i] appear in K
        if (flag == true)
        {
             
            // Update count
            count++;
        }
    }
 
    // Finally print count
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    int K = 12;
    int []arr = { 1, 12, 1222, 13, 2 };
     
    Console.WriteLine(noOfValidNumbers(K, arr));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
static int noOfValidKbers(int K, vector arr)
{
 
    // Stores distinct digits of K
    map set;
 
    // Iterate over all the digits of K
    while (K != 0)
    {
 
        // Insert current digit into set
        set[K % 10] = 1;
 
        // Update K
        K = K / 10;
    }
 
    // Stores the count of array elements
    // whose distinct digits are a subset
    // of the digits of K
    int count = 0;
 
    // Traverse the array, arr[]
    for (int i = 0; i < arr.size(); i++)
    {
 
        // Stores current element
        int no = arr[i];
 
        // Check if all the digits of arr[i]
        // are present in K or not
        bool falg = true;
 
        // Iterate over all the digits of arr[i]
        while (no != 0)
        {
 
            // Stores current digit
            int digit = no % 10;
 
            // If digit not present in the set
            if (set.find(digit)==set.end())
            {
 
                // Update flag
                falg = false;
                break;
            }
 
            // Update no
            no = no / 10;
        }
 
        // If all the digits of
        // arr[i] present in set
        if (falg == true)
        {
 
            // Update count
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int K = 12;
    vector arr = { 1, 12, 1222, 13, 2 };
    cout<<(noOfValidKbers(K, arr));
}
 
// This code is contributed by mohit kumar 29


Java
// Java program to implement
// the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to the count of array elements whose
    // distinct digits are a subset of the digits of K
    static int noOfValidKbers(int K, int arr[])
    {
 
        // Stores distinct digits of K
        HashSet set = new HashSet<>();
 
        // Iterate over all the digits of K
        while (K != 0) {
 
            // Insert current digit into set
            set.add(K % 10);
 
            // Update K
            K = K / 10;
        }
 
        // Stores the count of array elements
        // whose distinct digits are a subset
        // of the digits of K
        int count = 0;
 
        // Traverse the array, arr[]
        for (int i = 0; i < arr.length; i++) {
 
            // Stores current element
            int no = arr[i];
 
            // Check if all the digits of arr[i]
            // are present in K or not
            boolean falg = true;
 
            // Iterate over all the digits of arr[i]
            while (no != 0) {
 
                // Stores current digit
                int digit = no % 10;
 
                // If digit not present in the set
                if (!set.contains(digit)) {
 
                    // Update flag
                    falg = false;
                    break;
                }
 
                // Update no
                no = no / 10;
            }
 
            // If all the digits of
            // arr[i] present in set
            if (falg == true) {
 
                // Update count
                count++;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int K = 12;
        int arr[] = { 1, 12, 1222, 13, 2 };
        System.out.println(noOfValidKbers(K, arr));
    }
}


Python3
# Python 3 program to implement
# the above approach
 
# Function to the count of array elements whose
# distinct digits are a subst of the digits of K
def noOfValidKbers(K, arr):
   
    # Stores distinct digits of K
    st = {}
 
    # Iterate over all the digits of K
    while(K != 0):
       
        # Insert current digit into st
        if(K % 10 in st):
            st[K % 10] = 1
        else:
            st[K % 10] = st.get(K % 10, 0) + 1
 
        # Update K
        K = K // 10
 
    # Stores the count of array elements
    # whose distinct digits are a subst
    # of the digits of K
    count = 0
 
    # Traverse the array, arr[]
    for i in range(len(arr)):
       
        # Stores current element
        no = arr[i]
 
        # Check if all the digits of arr[i]
        # are present in K or not
        falg = True
 
        # Iterate over all the digits of arr[i]
        while(no != 0):
           
            # Stores current digit
            digit = no % 10
 
            # If digit not present in the st
            if (digit not in st):
               
                # Update flag
                falg = False
                break
 
            # Update no
            no = no//10
             
        # If all the digits of
        # arr[i] present in st
        if (falg == True):
           
            # Update count
            count += 1
    return count
 
# Driver Code
if __name__ == '__main__':
    K = 12
    arr =  [1, 12, 1222, 13, 2]
    print(noOfValidKbers(K, arr))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
static int noOfValidKbers(int K, int []arr)
{
     
    // Stores distinct digits of K
    HashSet set = new HashSet();
 
    // Iterate over all the digits of K
    while (K != 0)
    {
         
        // Insert current digit into set
        set.Add(K % 10);
 
        // Update K
        K = K / 10;
    }
 
    // Stores the count of array elements
    // whose distinct digits are a subset
    // of the digits of K
    int count = 0;
 
    // Traverse the array, []arr
    for(int i = 0; i < arr.Length; i++)
    {
         
        // Stores current element
        int no = arr[i];
 
        // Check if all the digits of arr[i]
        // are present in K or not
        bool falg = true;
 
        // Iterate over all the digits of arr[i]
        while (no != 0)
        {
             
            // Stores current digit
            int digit = no % 10;
 
            // If digit not present in the set
            if (!set.Contains(digit))
            {
                 
                // Update flag
                falg = false;
                break;
            }
 
            // Update no
            no = no / 10;
        }
 
        // If all the digits of
        // arr[i] present in set
        if (falg == true)
        {
             
            // Update count
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    int K = 12;
    int []arr = { 1, 12, 1222, 13, 2 };
     
    Console.WriteLine(noOfValidKbers(K, arr));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

时间复杂度: O(N * log 10 (K) * log 10 (Max)),Max为最大数组元素
辅助空间: O(1)

高效的方法:上述方法可以使用 HashSet 进行优化。请按照以下步骤解决问题:

  1. 迭代K的数字并将所有数字插入一个 HashSet say set
  2. 遍历数组arr[]并对每个数组元素,迭代当前元素的所有数字并检查所有数字是否都存在于集合中。如果发现为真,则增加计数。
  3. 最后,打印获得的计数。

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
static int noOfValidKbers(int K, vector arr)
{
 
    // Stores distinct digits of K
    map set;
 
    // Iterate over all the digits of K
    while (K != 0)
    {
 
        // Insert current digit into set
        set[K % 10] = 1;
 
        // Update K
        K = K / 10;
    }
 
    // Stores the count of array elements
    // whose distinct digits are a subset
    // of the digits of K
    int count = 0;
 
    // Traverse the array, arr[]
    for (int i = 0; i < arr.size(); i++)
    {
 
        // Stores current element
        int no = arr[i];
 
        // Check if all the digits of arr[i]
        // are present in K or not
        bool falg = true;
 
        // Iterate over all the digits of arr[i]
        while (no != 0)
        {
 
            // Stores current digit
            int digit = no % 10;
 
            // If digit not present in the set
            if (set.find(digit)==set.end())
            {
 
                // Update flag
                falg = false;
                break;
            }
 
            // Update no
            no = no / 10;
        }
 
        // If all the digits of
        // arr[i] present in set
        if (falg == true)
        {
 
            // Update count
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int K = 12;
    vector arr = { 1, 12, 1222, 13, 2 };
    cout<<(noOfValidKbers(K, arr));
}
 
// This code is contributed by mohit kumar 29

Java

// Java program to implement
// the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to the count of array elements whose
    // distinct digits are a subset of the digits of K
    static int noOfValidKbers(int K, int arr[])
    {
 
        // Stores distinct digits of K
        HashSet set = new HashSet<>();
 
        // Iterate over all the digits of K
        while (K != 0) {
 
            // Insert current digit into set
            set.add(K % 10);
 
            // Update K
            K = K / 10;
        }
 
        // Stores the count of array elements
        // whose distinct digits are a subset
        // of the digits of K
        int count = 0;
 
        // Traverse the array, arr[]
        for (int i = 0; i < arr.length; i++) {
 
            // Stores current element
            int no = arr[i];
 
            // Check if all the digits of arr[i]
            // are present in K or not
            boolean falg = true;
 
            // Iterate over all the digits of arr[i]
            while (no != 0) {
 
                // Stores current digit
                int digit = no % 10;
 
                // If digit not present in the set
                if (!set.contains(digit)) {
 
                    // Update flag
                    falg = false;
                    break;
                }
 
                // Update no
                no = no / 10;
            }
 
            // If all the digits of
            // arr[i] present in set
            if (falg == true) {
 
                // Update count
                count++;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int K = 12;
        int arr[] = { 1, 12, 1222, 13, 2 };
        System.out.println(noOfValidKbers(K, arr));
    }
}

蟒蛇3

# Python 3 program to implement
# the above approach
 
# Function to the count of array elements whose
# distinct digits are a subst of the digits of K
def noOfValidKbers(K, arr):
   
    # Stores distinct digits of K
    st = {}
 
    # Iterate over all the digits of K
    while(K != 0):
       
        # Insert current digit into st
        if(K % 10 in st):
            st[K % 10] = 1
        else:
            st[K % 10] = st.get(K % 10, 0) + 1
 
        # Update K
        K = K // 10
 
    # Stores the count of array elements
    # whose distinct digits are a subst
    # of the digits of K
    count = 0
 
    # Traverse the array, arr[]
    for i in range(len(arr)):
       
        # Stores current element
        no = arr[i]
 
        # Check if all the digits of arr[i]
        # are present in K or not
        falg = True
 
        # Iterate over all the digits of arr[i]
        while(no != 0):
           
            # Stores current digit
            digit = no % 10
 
            # If digit not present in the st
            if (digit not in st):
               
                # Update flag
                falg = False
                break
 
            # Update no
            no = no//10
             
        # If all the digits of
        # arr[i] present in st
        if (falg == True):
           
            # Update count
            count += 1
    return count
 
# Driver Code
if __name__ == '__main__':
    K = 12
    arr =  [1, 12, 1222, 13, 2]
    print(noOfValidKbers(K, arr))
     
    # This code is contributed by SURENDRA_GANGWAR.

C#

// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to the count of array elements whose
// distinct digits are a subset of the digits of K
static int noOfValidKbers(int K, int []arr)
{
     
    // Stores distinct digits of K
    HashSet set = new HashSet();
 
    // Iterate over all the digits of K
    while (K != 0)
    {
         
        // Insert current digit into set
        set.Add(K % 10);
 
        // Update K
        K = K / 10;
    }
 
    // Stores the count of array elements
    // whose distinct digits are a subset
    // of the digits of K
    int count = 0;
 
    // Traverse the array, []arr
    for(int i = 0; i < arr.Length; i++)
    {
         
        // Stores current element
        int no = arr[i];
 
        // Check if all the digits of arr[i]
        // are present in K or not
        bool falg = true;
 
        // Iterate over all the digits of arr[i]
        while (no != 0)
        {
             
            // Stores current digit
            int digit = no % 10;
 
            // If digit not present in the set
            if (!set.Contains(digit))
            {
                 
                // Update flag
                falg = false;
                break;
            }
 
            // Update no
            no = no / 10;
        }
 
        // If all the digits of
        // arr[i] present in set
        if (falg == true)
        {
             
            // Update count
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    int K = 12;
    int []arr = { 1, 12, 1222, 13, 2 };
     
    Console.WriteLine(noOfValidKbers(K, arr));
}
}
 
// This code is contributed by 29AjayKumar

Javascript


输出:
4

时间复杂度: O(N * log 10 (Max)),其中 Max 是最大的数组元素
辅助空间: O(10)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程