📌  相关文章
📜  重新排列数组以最大化数组前缀和中的素数

📅  最后修改于: 2021-05-04 14:19:22             🧑  作者: Mango

给定arr []12的数组,任务是重新排列数组,使重新排列的数组的前缀和具有最大质数。请注意,可以有多个答案。

例子:

方法:可以通过两个观察值解决问题,一个是第一个素数为2,其后的所有其他素数都是奇数(所有奇数都不是素数)。因此,只需在第一个位置填充2(如果有的话),然后填充奇数个,然后填充其余的2。在末尾插入唯一的1(如果初始数目为偶数)。

这样做,我们从2开始并通过添加一个1的奇数以一个奇数结束,然后通过在其上加上2,我们从一个奇数跳到另一个使素数概率最大化的奇数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the re-arranged array
void solve(int a[], int n)
{
    int ones = 0, twos = 0;
  
    // Count the number of
    // ones and twos in a[]
    for (int i = 0; i < n; i++) {
  
        // If the array element is 1
        if (a[i] == 1)
            ones++;
  
        // Array element is 2
        else
            twos++;
    }
    int ind = 0;
  
    // If it has at least one 2
    // Fill up first 2
    if (twos)
        a[ind++] = 2;
  
    // Decrease the cnt of
    // ones if even
    bool evenOnes = (ones % 2 == 0) ? true : false;
    if (evenOnes)
        ones -= 1;
  
    // Fill up with odd count of ones
    for (int i = 0; i < ones; i++)
        a[ind++] = 1;
  
    // Fill up with remaining twos
    for (int i = 0; i < twos - 1; i++)
        a[ind++] = 2;
  
    // If even ones, then fill last position
    if (evenOnes)
        a[ind++] = 1;
  
    // Print the rearranged array
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 1, 2, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    solve(a, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG {
  
    // Function to print the re-arranged array
    static void solve(int a[], int n)
    {
        int ones = 0, twos = 0;
  
        // Count the number of
        // ones and twos in a[]
        for (int i = 0; i < n; i++) {
  
            // If the array element is 1
            if (a[i] == 1)
                ones++;
  
            // Array element is 2
            else
                twos++;
        }
        int ind = 0;
  
        // If it has at least one 2
        // Fill up first 2
        if (twos > 0)
            a[ind++] = 2;
  
        // Decrease the cnt of
        // ones if even
        boolean evenOnes = (ones % 2 == 0) ? true : false;
        if (evenOnes)
            ones -= 1;
  
        // Fill up with odd count of ones
        for (int i = 0; i < ones; i++)
            a[ind++] = 1;
  
        // Fill up with remaining twos
        for (int i = 0; i < twos - 1; i++)
            a[ind++] = 2;
  
        // If even ones, then fill last position
        if (evenOnes)
            a[ind++] = 1;
  
        // Print the rearranged array
        for (int i = 0; i < n; i++)
            System.out.print(a[i] + " ");
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int a[] = { 1, 2, 1, 2, 1 };
        int n = a.length;
        solve(a, n);
    }
}
  
// This code is contributed by ajit.


Python
# Python3 implementation of the approach
  
# Function to print the re-arranged array
def solve(a, n):
  
    ones, twos = 0, 0
  
    # Count the number of
    # ones and twos in a[]
    for i in range(n):
  
        # If the array element is 1
        if (a[i] == 1):
            ones+=1
  
        # Array element is 2
        else:
            twos+=1
  
    ind = 0
  
    # If it has at least one 2
    # Fill up first 2
    if (twos):
        a[ind] = 2
        ind+=1
  
    # Decrease the cnt of
    # ones if even
    if ones % 2 == 0:
        evenOnes = True
    else:
        evenOnes = False
  
  
    if (evenOnes):
        ones -= 1
  
    # Fill up with odd count of ones
    for i in range(ones):
        a[ind] = 1
        ind += 1
  
  
    # Fill up with remaining twos
    for i in range(twos-1):
        a[ind] = 2
        ind += 1
  
    # If even ones, then fill last position
    if (evenOnes):
        a[ind] = 1
        ind += 1
  
    # Print the rearranged array
    for i in range(n):
        print(a[i],end = " ")
  
# Driver code
  
a = [1, 2, 1, 2, 1]
n = len(a)
solve(a, n)
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
    // Function to print the re-arranged array
    static void solve(int []a, int n)
    {
        int ones = 0, twos = 0;
  
        // Count the number of
        // ones and twos in a[]
        for (int i = 0; i < n; i++)
        {
  
            // If the array element is 1
            if (a[i] == 1)
                ones++;
  
            // Array element is 2
            else
                twos++;
        }
        int ind = 0;
  
        // If it has at least one 2
        // Fill up first 2
        if (twos > 0)
            a[ind++] = 2;
  
        // Decrease the cnt of
        // ones if even
        bool evenOnes = (ones % 2 == 0) ? true : false;
        if (evenOnes)
            ones -= 1;
  
        // Fill up with odd count of ones
        for (int i = 0; i < ones; i++)
            a[ind++] = 1;
  
        // Fill up with remaining twos
        for (int i = 0; i < twos - 1; i++)
            a[ind++] = 2;
  
        // If even ones, then fill last position
        if (evenOnes)
            a[ind++] = 1;
  
        // Print the rearranged array
        for (int i = 0; i < n; i++)
            Console.Write(a[i] + " ");
    }
  
    // Driver code
    static public void Main ()
    {
        int []a = { 1, 2, 1, 2, 1 };
        int n = a.Length;
        solve(a, n);
    }
}
  
// This code is contributed by Tushil.


PHP


输出:
2 1 1 1 2