📌  相关文章
📜  不会对给定数组的任何元素进行除法的最小正整数

📅  最后修改于: 2021-04-17 18:13:22             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是确定最小的正整数K ,使得没有一个数组元素不能被K整除。如果没有这样的整数

例子:

方法:请按照以下步骤解决问题:

  • 找到给定数组的最大元素,例如maxE
  • 使用变量i遍历[1,maxE + 1]的范围并检查给定数组中是否有可被i整除的整数。如果发现为真,则检查该范围内的下一个整数。
  • 否则,打印当前数字i

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the smallest number
// which doesn't divides any integer in
// the given array arr[]
void smallestNumber(int arr[], int len)
{
    int maxi = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < len; i++) {
 
        // Maximum array elemnt
        maxi = std::max(maxi, arr[i]);
    }
 
    // Initialize variable
    int ans = -1;
 
    // Traverse from 2 to max
    for (int i = 2; i < maxi + 2; i++) {
 
        // Stores if any such
        // integer is found or not
        bool flag = true;
 
        for (int j = 0; j < len; j++) {
 
            // If any array element
            // is divisible by j
            if (arr[j] % i == 0) {
 
                flag = false;
                break;
            }
        }
 
        if (flag) {
 
            // Smallest integer
            ans = i;
            break;
        }
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 6, 9, 2 };
    int N = sizeof(arr)
/ sizeof(arr[0]);
 
    // Function Call
    smallestNumber(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
// Function to find the smallest number
// which doesn't divides any integer in
// the given array arr[]
static void smallestNumber(int arr[], int len)
{
    int maxi = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < len; i++)
    {
 
        // Maximum array elemnt
        maxi = Math.max(maxi, arr[i]);
    }
 
    // Initialize variable
    int ans = -1;
 
    // Traverse from 2 to max
    for (int i = 2; i < maxi + 2; i++)
    {
 
        // Stores if any such
        // integer is found or not
        boolean flag = true;
        for (int j = 0; j < len; j++)
        {
 
            // If any array element
            // is divisible by j
            if (arr[j] % i == 0)
            {
                flag = false;
                break;
            }
        }
 
        if (flag)
        {
 
            // Smallest integer
            ans = i;
            break;
        }
    }
 
    // Print the answer
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 2, 6, 9, 2 };
    int N = arr.length;
 
    // Function Call
    smallestNumber(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to find the smallest number
# which doesn't divides any integer in
# the given array arr[]
def smallestNumber(arr, lenn):
    maxi = 0
 
    # Traverse the array arr[]
    for i in range(lenn):
 
        # Maximum array elemnt
        maxi = max(maxi, arr[i])
 
    # Initialize variable
    ans = -1
 
    # Traverse from 2 to max
    for i in range(2, maxi + 2):
 
        # Stores if any such
        # integer is found or not
        flag = True
        for j in range(lenn):
 
            # If any array element
            # is divisible by j
            if (arr[j] % i == 0):
                flag = False
                break
        if (flag):
 
            # Smallest integer
            ans = i
            break
 
    # Prthe answer
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    arr = [3, 2, 6, 9, 2]
    N = len(arr)
 
    #Function Call
    smallestNumber(arr, N)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find the smallest number
  // which doesn't divides any integer in
  // the given array arr[]
  static void smallestNumber(int []arr, int len)
  {
    int maxi = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < len; i++)
    {
 
      // Maximum array elemnt
      maxi = Math.Max(maxi, arr[i]);
    }
 
    // Initialize variable
    int ans = -1;
 
    // Traverse from 2 to max
    for (int i = 2; i < maxi + 2; i++)
    {
 
      // Stores if any such
      // integer is found or not
      bool flag = true;
      for (int j = 0; j < len; j++)
      {
 
        // If any array element
        // is divisible by j
        if (arr[j] % i == 0)
        {
          flag = false;
          break;
        }
      }
      if (flag)
      {
 
        // Smallest integer
        ans = i;
        break;
      }
    }
 
    // Print the answer
    Console.WriteLine(ans);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int []arr = { 3, 2, 6, 9, 2 };
    int N = arr.Length;
 
    // Function Call
    smallestNumber(arr, N);
  }
}
 
// This code is contributed by AnkThon


输出:
4

时间复杂度: O(N * max)其中max是给定数组中的最大元素
辅助空间: O(N)