📌  相关文章
📜  为了使所有数组元素相等,需要减去最小质数

📅  最后修改于: 2021-05-19 18:53:31             🧑  作者: 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)

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

  • 初始化一个大小为10 5的数组prime [] ,以使用ieratosthenes筛选器存储每个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)