📌  相关文章
📜  需要减去的最小素数以使所有数组元素相等

📅  最后修改于: 2021-09-06 11:32:42             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是找到从数组元素中减去以使所有数组元素相等所需的最小素数数。

例子:

方法:可以使用以下观察来解决给定的问题:

  • 每个大于2 的偶数都是两个素数之和。
  • 每个大于1 的奇数,最多可以表示为3 个素数之和。以下是相同的可能情况:
    • 情况 1:如果N是素数。
    • 情况 2:如果(N – 2)是素数。因此,需要 2 个数字,即2N – 2
    • 情况 3:如果(N – 3)是偶数,则使用哥德巴赫猜想。 (N – 3)可以表示为两个素数之和。
  • 因此,想法是将每个数组元素减少到数组的最小值(比如M ) arr[] ,如果数组中存在具有值(M + 1)的元素,则将每个元素减少到值(M – 2)

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

  • 初始化一个数组,比如prime[] ,大小为10 5 ,以存储在每个i索引处,无论i是否为素数,都使用埃拉托色尼筛法。
  • 找到数组中存在的最小元素,比如M
  • 如果数组arr[] 中存在任何值为(M + 1) 的元素,则将M更新为(M – 2)
  • 初始化一个变量,比如count ,以存储使所有数组元素相等所需的操作数。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 找出arr[i]M之间的区别,比如D
    • 根据D的以下值更新count的值:
      • 如果D的值是素数,则将count增加1
      • 如果D的值是偶数,则将count增加2
      • 如果D的值是奇数,并且(D – 2)是素数,则将count增加2 。否则,将count增加3
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#define limit 100000
using namespace std;
 
// Stores the sieve of prime numbers
bool prime[limit + 1];
 
// Function that performs the Sieve of
// Eratosthenes
void sieve()
{
    // Initialize all numbers as prime
    memset(prime, true, sizeof(prime));
 
    // Iterate over the range [2, 1000]
    for (int p = 2; p * p <= limit; p++) {
 
        // If the current element
        // is a prime number
        if (prime[p] == true) {
 
            // Mark all its multiples as false
            for (int i = p * p; i <= limit; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to find the minimum number of
// subtraction of primes numbers required
// to make all array elements the same
int findOperations(int arr[], int n)
{
    // Peform sieve of eratoshthenes
    sieve();
 
    int minm = INT_MAX;
 
    // Find the minimum value
    for (int i = 0; i < n; i++) {
        minm = min(minm, arr[i]);
    }
 
    // Stores the value to each array
    // element should be reduced
    int val = minm;
 
    for (int i = 0; i < n; i++) {
 
        // If an element exists with
        // value (M + 1)
        if (arr[i] == minm + 1) {
            val = minm - 2;
            break;
        }
    }
 
    // Stores the minimum count of
    // substraction of prime numbers
    int cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        int D = arr[i] - val;
 
        // If D is equal to 0
        if (D == 0) {
            continue;
        }
 
        // If D is a prime number
        else if (prime[D] == true) {
 
            // Increase count by 1
            cnt += 1;
        }
 
        // If D is an even number
        else if (D % 2 == 0) {
 
            // Increase count by 2
            cnt += 2;
        }
        else {
 
            // If D - 2 is prime
            if (prime[D - 2] == true) {
 
                // Increase count by 2
                cnt += 2;
            }
 
            // Otherwise, increase
            // count by 3
            else {
                cnt += 3;
            }
        }
    }
 
    return cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 7, 10, 4, 5 };
    int N = 4;
    cout << findOperations(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
static int limit = 100000;
 
// Stores the sieve of prime numbers
static boolean prime[];
 
// Function that performs the Sieve of
// Eratosthenes
static void sieve()
{
    prime = new boolean[limit + 1];
 
    // Initialize all numbers as prime
    Arrays.fill(prime, true);
 
    // Iterate over the range [2, 1000]
    for(int p = 2; p * p <= limit; p++)
    {
         
        // If the current element
        // is a prime number
        if (prime[p] == true)
        {
             
            // Mark all its multiples as false
            for(int i = p * p; i <= limit; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to find the minimum number of
// subtraction of primes numbers required
// to make all array elements the same
static int findOperations(int arr[], int n)
{
     
    // Peform sieve of eratoshthenes
    sieve();
 
    int minm = Integer.MAX_VALUE;
 
    // Find the minimum value
    for(int i = 0; i < n; i++)
    {
        minm = Math.min(minm, arr[i]);
    }
 
    // Stores the value to each array
    // element should be reduced
    int val = minm;
 
    for(int i = 0; i < n; i++)
    {
         
        // If an element exists with
        // value (M + 1)
        if (arr[i] == minm + 1)
        {
            val = minm - 2;
            break;
        }
    }
 
    // Stores the minimum count of
    // substraction of prime numbers
    int cnt = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
        int D = arr[i] - val;
 
        // If D is equal to 0
        if (D == 0)
        {
            continue;
        }
 
        // If D is a prime number
        else if (prime[D] == true)
        {
             
            // Increase count by 1
            cnt += 1;
        }
 
        // If D is an even number
        else if (D % 2 == 0)
        {
             
            // Increase count by 2
            cnt += 2;
        }
        else
        {
             
            // If D - 2 is prime
            if (prime[D - 2] == true)
            {
                 
                // Increase count by 2
                cnt += 2;
            }
 
            // Otherwise, increase
            // count by 3
            else
            {
                cnt += 3;
            }
        }
    }
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 7, 10, 4, 5 };
    int N = 4;
     
    System.out.println(findOperations(arr, N));
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
import sys
 
limit = 100000
 
# Stores the sieve of prime numbers
prime = [True] * (limit + 1)
 
# Function that performs the Sieve of
# Eratosthenes
def sieve():
 
    # Iterate over the range [2, 1000]
    p = 2
    while(p * p <= limit):
 
        # If the current element
        # is a prime number
        if (prime[p] == True):
 
            # Mark all its multiples as false
            for i in range(p * p, limit, p):
                prime[i] = False
         
        p += 1
     
# Function to find the minimum number of
# subtraction of primes numbers required
# to make all array elements the same
def findOperations(arr, n):
     
    # Peform sieve of eratoshthenes
    sieve()
 
    minm = sys.maxsize
 
    # Find the minimum value
    for i in range(n):
        minm = min(minm, arr[i])
     
    # Stores the value to each array
    # element should be reduced
    val = minm
 
    for i in range(n):
 
        # If an element exists with
        # value (M + 1)
        if (arr[i] == minm + 1):
            val = minm - 2
            break
         
    # Stores the minimum count of
    # substraction of prime numbers
    cnt = 0
 
    # Traverse the array
    for i in range(n):
        D = arr[i] - val
 
        # If D is equal to 0
        if (D == 0):
            continue
         
        # If D is a prime number
        elif (prime[D] == True):
 
            # Increase count by 1
            cnt += 1
         
        # If D is an even number
        elif (D % 2 == 0):
 
            # Increase count by 2
            cnt += 2
         
        else:
 
            # If D - 2 is prime
            if (prime[D - 2] == True):
                 
                # Increase count by 2
                cnt += 2
 
            # Otherwise, increase
            # count by 3
            else:
                cnt += 3
 
    return cnt
 
# Driver Code
arr = [ 7, 10, 4, 5 ]
N = 4
 
print(findOperations(arr, N))
 
# This code is contributed by splevel62


C#
// C# program for the above approach
using System;
 
class GFG{
 
static int limit = 100000;
 
// Stores the sieve of prime numbers
static bool[] prime;
 
// Function that performs the Sieve of
// Eratosthenes
static void sieve()
{
    prime = new bool[limit + 1];
 
    // Initialize all numbers as prime
    Array.Fill(prime, true);
 
    // Iterate over the range [2, 1000]
    for(int p = 2; p * p <= limit; p++)
    {
         
        // If the current element
        // is a prime number
        if (prime[p] == true)
        {
             
            // Mark all its multiples as false
            for(int i = p * p; i <= limit; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to find the minimum number of
// subtraction of primes numbers required
// to make all array elements the same
static int findOperations(int[] arr, int n)
{
     
    // Peform sieve of eratoshthenes
    sieve();
 
    int minm = Int32.MaxValue;
 
    // Find the minimum value
    for(int i = 0; i < n; i++)
    {
        minm = Math.Min(minm, arr[i]);
    }
 
    // Stores the value to each array
    // element should be reduced
    int val = minm;
 
    for(int i = 0; i < n; i++)
    {
         
        // If an element exists with
        // value (M + 1)
        if (arr[i] == minm + 1)
        {
            val = minm - 2;
            break;
        }
    }
 
    // Stores the minimum count of
    // substraction of prime numbers
    int cnt = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
        int D = arr[i] - val;
 
        // If D is equal to 0
        if (D == 0)
        {
            continue;
        }
 
        // If D is a prime number
        else if (prime[D] == true)
        {
             
            // Increase count by 1
            cnt += 1;
        }
 
        // If D is an even number
        else if (D % 2 == 0)
        {
             
            // Increase count by 2
            cnt += 2;
        }
        else
        {
             
            // If D - 2 is prime
            if (prime[D - 2] == true)
            {
                 
                // Increase count by 2
                cnt += 2;
            }
 
            // Otherwise, increase
            // count by 3
            else
            {
                cnt += 3;
            }
        }
    }
    return cnt;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 7, 10, 4, 5 };
    int N = 4;
 
    Console.WriteLine(findOperations(arr, N));
}
}
 
// This code is contributed by ukasp


Javascript


输出:
5

时间复杂度: O(N + M * log(log(M))), M是大小
辅助空间: O(M)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live