📌  相关文章
📜  一个数组的元素不能被另一个数组的任何元素整除

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

一个数组的元素不能被另一个数组的任何元素整除

给定两个数组 A[] 和 B[],编写一个有效的代码来确定 B[] 的每个元素是否可以被 A[] 的至少一个元素整除。显示 B[] 的那些元素,它们不能被 A[] 中的任何元素整除。
例子 :

Input : A[] = {100, 200, 400, 100, 600}
        B[] = {45, 90, 48, 1000, 3000}
Output : 45, 90, 48 
The output elements are those that are 
not divisible by any element of A[].

方法一(朴素实现)

  • 遍历 B[] 的每一个元素。
  • 检查它是否可以被 A[] 的至少 1 个元素整除。如果不能被任何整除,则打印它。

C++
// C++ code for naive implementation
#include
using namespace std;
 
// Function for checking the condition
// with 2 loops
void printNonDivisible(int A[], int B[],
                          int n, int m)
{
    for (int i = 0; i < m; i++)
    {
        int j = 0;
        for (j = 0; j < n; j++)
            if( B[i] % A[j] == 0 )
                break;
 
        // If none of the elements in A[]
        // divided B[i]
        if (j == n)
            cout << B[i] << endl;
    }
}
 
// Driver code
int main()
{
    int A[] = {100, 200, 400, 100};
    int n = sizeof(A)/sizeof(A[0]);
    int B[] = {190, 200, 87, 600, 800};
    int m = sizeof(B)/sizeof(B[0]);
    printNonDivisible(A, B, n, m);
    return 0;
}


Java
// Java code for naive implementation
import java.io.*;
 
public class GFG {
     
// Function for checking the condition
// with 2 loops
static void printNonDivisible(int []A, int []B,
                              int n, int m)
{
     
    for (int i = 0; i < m; i++)
    {
        int j = 0;
        for (j = 0; j < n; j++)
            if( B[i] % A[j] == 0 )
                break;
 
        // If none of the elements
        // in A[] divided B[i]
        if (j == n)
            System.out.println(B[i]);
    }
}
 
    // Driver code
    static public void main (String[] args)
    {
        int []A = {100, 200, 400, 100};
        int n = A.length;
         
        int []B = {190, 200, 87, 600, 800};
        int m = B.length;
         
        printNonDivisible(A, B, n, m);
    }
}
 
// This code is contributed by vt_m .


Python3
# Python3 code for naive implementation
import math as mt
 
# Function for checking the condition
# with 2 loops
def printNonDivisible(A, B, n, m):
 
    for i in range(m):
        j = 0
        for j in range(n):
            if(B[i] % A[j] == 0):
                break
 
        # If none of the elements in A[]
        # divided B[i]
        if (j == n - 1):
            print(B[i])
 
# Driver code
A = [100, 200, 400, 100]
n = len(A)
B = [190, 200, 87, 600, 800]
m = len(B)
printNonDivisible(A, B, n, m)
 
# This code is contributed by#
# mohit kumar 29


C#
// C# code for naive implementation
using System;
 
public class GFG {
     
// Function for checking the
// condition with 2 loops
static void printNonDivisible(int []A, int []B,
                              int n, int m)
{
     
    for (int i = 0; i < m; i++)
    {
        int j = 0;
        for (j = 0; j < n; j++)
            if( B[i] % A[j] == 0 )
                break;
 
        // If none of the elements
        // in A[] divided B[i]
        if (j == n)
            Console.WriteLine(B[i]);
    }
}
 
    // Driver code
    static public void Main ()
    {
        int []A = {100, 200, 400, 100};
        int n = A.Length;
        int []B = {190, 200, 87, 600, 800};
        int m = B.Length;
        printNonDivisible(A, B, n, m);
    }
}
 
// This code is contributed by vt_m .


PHP


Javascript


C++
// CPP code for improved implementation
#include
using namespace std;
 
// Function for printing all elements of B[]
// that are not divisible by any element of A[]
void printNonDivisible(int A[], int B[], int n,
                                         int m)
{
    // Find maximum element in B[]
    int maxB = 0;
    for (int i = 0; i < m; i++)
        if (B[i] > maxB)
            maxB = B[i];
 
    // Initialize all multiples as marked
    int mark[maxB];
    memset(mark, 0, sizeof(mark));
 
    // Marking the multiples of all the
    // elements of the array.
    for (int i = 0; i < n; i++)
        for (int x = A[i]; x <= maxB; x += A[i])
            mark[x]++;
 
    // Print not marked elements
    for (int i = 0; i < m; i++)
        if (! mark[B[i]])
            cout << B[i] << endl;
}
 
// Driver function
int main()
{
    int A[] = {100, 200, 400, 100};
    int n = sizeof(A)/sizeof(A[0]);
    int B[] = {190, 200, 87, 600, 800};
    int m = sizeof(B)/sizeof(B[0]);
    printNonDivisible(A, B, n, m);
    return 0;
}


Java
// Java code for improved implementation
import java.io.*;
 
class GFG
{
     
// Function for printing all elements of B[]
// that are not divisible by any element of A[]
static void printNonDivisible(int []A, int []B,
                                    int n,int m)
{
    // Find maximum element in B[]
    int maxB = 0;
    for (int i = 0; i < m; i++)
        if (B[i] > maxB)
            maxB = B[i];
 
    // Initialize all multiples as marked
 
    int [] mark = new int[maxB + 1];
    for(int i = 0; i < maxB; i++)
        mark[i]=0;
 
    // Marking the multiples of all the
    // elements of the array.
    for (int i = 0; i < n; i++)
        for (int x = A[i]; x <= maxB; x += A[i])
            mark[x]++;
 
    // Print not marked elements
    for (int i = 0; i < m; i++)
        if (mark[B[i]] == 0)
            System.out.println(B[i]);
}
 
// Driver code
static public void main(String[] args)
{
    int []A= {100, 200, 400, 100};
    int n = A.length;
    int []B= {190, 200, 87, 600, 800};
    int m = B.length;
    printNonDivisible(A, B, n, m);
}
}
 
// This code is contributed by Mohit Kumar.


Python3
# Python 3 code for improved implementation
 
# Function for printing all elements of B[]
# that are not divisible by any element of A[]
def printNonDivisible(A, B, n, m):
     
    # Find maximum element in B[]
    maxB = 0
    for i in range(0, m, 1):
        if (B[i] > maxB):
            maxB = B[i]
 
    # Initialize all multiples as marked
    mark = [0 for i in range(maxB)]
 
    # Marking the multiples of all
    # the elements of the array.
    for i in range(0, n, 1):
        for x in range(A[i], maxB, A[i]):
            mark[x] += 1
 
    # Print not marked elements
    for i in range(0, m - 1, 1):
        if (mark[B[i]] == 0):
            print(B[i])
 
# Driver Code
if __name__ == '__main__':
    A = [100, 200, 400, 100]
    n = len(A)
    B = [190, 200, 87, 600, 800]
    m = len(B)
    printNonDivisible(A, B, n, m)
 
# This code is contributed by
# Shashank_Sharma


C#
// C# code for improved implementation
using System;
 
class GFG
{
     
// Function for printing all elements of []B
// that are not divisible by any element of []A
static void printNonDivisible(int []A, int []B,
                                    int n, int m)
{
    // Find maximum element in []B
    int maxB = 0;
    for (int i = 0; i < m; i++)
        if (B[i] > maxB)
            maxB = B[i];
 
    // Initialize all multiples as marked
    int [] mark = new int[maxB + 1];
    for(int i = 0; i < maxB; i++)
        mark[i] = 0;
 
    // Marking the multiples of all the
    // elements of the array.
    for (int i = 0; i < n; i++)
        for (int x = A[i]; x <= maxB; x += A[i])
            mark[x]++;
 
    // Print not marked elements
    for (int i = 0; i < m; i++)
        if (mark[B[i]] == 0)
            Console.WriteLine(B[i]);
}
 
// Driver code
static public void Main(String[] args)
{
    int []A= {100, 200, 400, 100};
    int n = A.Length;
    int []B= {190, 200, 87, 600, 800};
    int m = B.Length;
    printNonDivisible(A, B, n, m);
}
}
 
// This code is contributed by Rajput-Ji


PHP
 $maxB)
            $maxB = $B[$i];
    }
     
    // Initialize all multiples as marked
    $mark = array();
    for ($i = 0; $i < $maxB; $i++)
    {
        $mark[] = "0";
    }
 
    // Marking the multiples of all
    // the elements of the array.
    for ($i = 0; $i < $n; $i++)
    {
        for ($x = $A[$i]; $x < $maxB;
                          $x += $A[$i])
        {
            $mark[$x] += 1;
        }
    }
     
    // Print not marked elements
    for ($i = 0; $i < $m - 1; $i++)
    {
        if ($mark[$B[$i]] == 0)
            echo "$B[$i]\n";
    }
}
 
// Driver Code
$A = array(100, 200, 400, 100);
$n = count($A);
$B = array(190, 200, 87, 600, 800);
$m = count($B);
printNonDivisible($A, $B, $n, $m);
 
// This code is contributed by
// Srathore
?>


Javascript


输出 :

190
87

时间复杂度:- O(n*m)
辅助空间:- O(1)
方法2(当元素很小时有效)

  • 维护一个数组mark[]来标记A[]中数字的倍数。
  • 标记 A[] 中所有元素的所有倍数,直到最大为 B[]。
  • 检查 B[] 中每个元素 n 的 mark[B[i]] 值是否不为 0,如果未标记则打印。

C++

// CPP code for improved implementation
#include
using namespace std;
 
// Function for printing all elements of B[]
// that are not divisible by any element of A[]
void printNonDivisible(int A[], int B[], int n,
                                         int m)
{
    // Find maximum element in B[]
    int maxB = 0;
    for (int i = 0; i < m; i++)
        if (B[i] > maxB)
            maxB = B[i];
 
    // Initialize all multiples as marked
    int mark[maxB];
    memset(mark, 0, sizeof(mark));
 
    // Marking the multiples of all the
    // elements of the array.
    for (int i = 0; i < n; i++)
        for (int x = A[i]; x <= maxB; x += A[i])
            mark[x]++;
 
    // Print not marked elements
    for (int i = 0; i < m; i++)
        if (! mark[B[i]])
            cout << B[i] << endl;
}
 
// Driver function
int main()
{
    int A[] = {100, 200, 400, 100};
    int n = sizeof(A)/sizeof(A[0]);
    int B[] = {190, 200, 87, 600, 800};
    int m = sizeof(B)/sizeof(B[0]);
    printNonDivisible(A, B, n, m);
    return 0;
}

Java

// Java code for improved implementation
import java.io.*;
 
class GFG
{
     
// Function for printing all elements of B[]
// that are not divisible by any element of A[]
static void printNonDivisible(int []A, int []B,
                                    int n,int m)
{
    // Find maximum element in B[]
    int maxB = 0;
    for (int i = 0; i < m; i++)
        if (B[i] > maxB)
            maxB = B[i];
 
    // Initialize all multiples as marked
 
    int [] mark = new int[maxB + 1];
    for(int i = 0; i < maxB; i++)
        mark[i]=0;
 
    // Marking the multiples of all the
    // elements of the array.
    for (int i = 0; i < n; i++)
        for (int x = A[i]; x <= maxB; x += A[i])
            mark[x]++;
 
    // Print not marked elements
    for (int i = 0; i < m; i++)
        if (mark[B[i]] == 0)
            System.out.println(B[i]);
}
 
// Driver code
static public void main(String[] args)
{
    int []A= {100, 200, 400, 100};
    int n = A.length;
    int []B= {190, 200, 87, 600, 800};
    int m = B.length;
    printNonDivisible(A, B, n, m);
}
}
 
// This code is contributed by Mohit Kumar.

Python3

# Python 3 code for improved implementation
 
# Function for printing all elements of B[]
# that are not divisible by any element of A[]
def printNonDivisible(A, B, n, m):
     
    # Find maximum element in B[]
    maxB = 0
    for i in range(0, m, 1):
        if (B[i] > maxB):
            maxB = B[i]
 
    # Initialize all multiples as marked
    mark = [0 for i in range(maxB)]
 
    # Marking the multiples of all
    # the elements of the array.
    for i in range(0, n, 1):
        for x in range(A[i], maxB, A[i]):
            mark[x] += 1
 
    # Print not marked elements
    for i in range(0, m - 1, 1):
        if (mark[B[i]] == 0):
            print(B[i])
 
# Driver Code
if __name__ == '__main__':
    A = [100, 200, 400, 100]
    n = len(A)
    B = [190, 200, 87, 600, 800]
    m = len(B)
    printNonDivisible(A, B, n, m)
 
# This code is contributed by
# Shashank_Sharma

C#

// C# code for improved implementation
using System;
 
class GFG
{
     
// Function for printing all elements of []B
// that are not divisible by any element of []A
static void printNonDivisible(int []A, int []B,
                                    int n, int m)
{
    // Find maximum element in []B
    int maxB = 0;
    for (int i = 0; i < m; i++)
        if (B[i] > maxB)
            maxB = B[i];
 
    // Initialize all multiples as marked
    int [] mark = new int[maxB + 1];
    for(int i = 0; i < maxB; i++)
        mark[i] = 0;
 
    // Marking the multiples of all the
    // elements of the array.
    for (int i = 0; i < n; i++)
        for (int x = A[i]; x <= maxB; x += A[i])
            mark[x]++;
 
    // Print not marked elements
    for (int i = 0; i < m; i++)
        if (mark[B[i]] == 0)
            Console.WriteLine(B[i]);
}
 
// Driver code
static public void Main(String[] args)
{
    int []A= {100, 200, 400, 100};
    int n = A.Length;
    int []B= {190, 200, 87, 600, 800};
    int m = B.Length;
    printNonDivisible(A, B, n, m);
}
}
 
// This code is contributed by Rajput-Ji

PHP

 $maxB)
            $maxB = $B[$i];
    }
     
    // Initialize all multiples as marked
    $mark = array();
    for ($i = 0; $i < $maxB; $i++)
    {
        $mark[] = "0";
    }
 
    // Marking the multiples of all
    // the elements of the array.
    for ($i = 0; $i < $n; $i++)
    {
        for ($x = $A[$i]; $x < $maxB;
                          $x += $A[$i])
        {
            $mark[$x] += 1;
        }
    }
     
    // Print not marked elements
    for ($i = 0; $i < $m - 1; $i++)
    {
        if ($mark[$B[$i]] == 0)
            echo "$B[$i]\n";
    }
}
 
// Driver Code
$A = array(100, 200, 400, 100);
$n = count($A);
$B = array(190, 200, 87, 600, 800);
$m = count($B);
printNonDivisible($A, $B, $n, $m);
 
// This code is contributed by
// Srathore
?>

Javascript


输出 :

190
87