📌  相关文章
📜  Java程序通过数字左移最小次数来最大化素数和非素数数组元素之和之间的差异

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

Java程序通过数字左移最小次数来最大化素数和非素数数组元素之和之间的差异

给定一个大小为N的数组arr[] ,任务是通过将数组元素的数字左移最少 1 来找到数组中存在的素数之和与非素数之和之间的最大差次数。  

例子:

方法:给定的问题可以贪婪地解决。如果可以将一个元素转换为一个或多个素数,则取其中的最大值。否则,尝试通过使用所有可能的旋转来最小化元素。
请按照以下步骤解决问题:

  • 初始化两个变量,比如anscost,分别存储所需的最大差异和最小操作数。
  • 使用变量i遍历数组arr[]并执行以下步骤:
    • 将变量maxPrimeminRotation初始化为-1以存储可以从arr[i]通过左旋转获得的最大质数和最小数。
    • 生成数字arr[i]的所有左旋转。
      • 如果arr[i]是超过maxPrime的素数,则将maxPrime更新为arr[i]和相应的成本
    • 如果maxPrime的值保持不变,则通过类似地生成所有左旋转来找到minRotation的值。
    • arr[i]的值添加到ans
  • 完成以上步骤后,打印anscost的值作为结果。

下面是上述方法的实现:

Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
  
class GFG {
  
    // Function to check if a
    // number is prime or not
    static boolean isPrime(int n)
    {
  
        // Base cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
  
        // Check if the number is
        // a multiple of 2 or 3
        if (n % 2 == 0 || n % 3 == 0)
            return false;
  
        int i = 5;
  
        // Iterate until square root of n
        while (i * i <= n) {
  
            // If n is divisible by both i and i + 2
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
            i = i + 6;
        }
        return true;
    }
  
    // Function to left shift a number
    // to maximize its contribution
    static int[] rotateElement(int n)
    {
  
        // Convert the number to string
        String strN = Integer.toString(n);
  
        // Stores the maximum prime number
        // that can be obtained from n
        int maxPrime = -1;
  
        // Store the required
        // number of operations
        int cost = 0;
  
        String temp = strN;
  
        // Check for all the left
        // rotations of the number
        for (int i = 0; i < strN.length(); i++) {
  
            // If the number is prime, then
            // take the maximum among them
            if (isPrime(Integer.parseInt(temp))
                && Integer.parseInt(temp) > maxPrime) {
                maxPrime = Integer.parseInt(temp);
                cost = i;
            }
  
            // Left rotation
            temp = temp.substring(1) + temp.charAt(0);
        }
  
        int optEle = maxPrime;
  
        // If no prime number can be obtained
        if (optEle == -1) {
            optEle = Integer.MAX_VALUE;
            temp = strN;
  
            // Check all the left
            // rotations of the number
            for (int i = 0; i < strN.length(); i++) {
  
                // Take the minimum element
                if (Integer.parseInt(temp) < optEle) {
                    optEle = Integer.parseInt(temp);
                    cost = i;
                }
  
                // Left rotation
                temp = temp.substring(1) + temp.charAt(0);
            }
            optEle *= (-1);
        }
        return new int[] { optEle, cost };
    }
  
    // Function to find the maximum sum
    // obtained using the given operations
    static void getMaxSum(int arr[])
    {
  
        // Store the maximum sum and
        // the number of operations
        int maxSum = 0, cost = 0;
  
        // Traverse array elements
        for (int x : arr) {
  
            // Get the optimal element and the
            // number of operations to obtain it
            int ret[] = rotateElement(x);
  
            int optEle = ret[0], optCost = ret[1];
  
            // Increment the maximum difference
            // and number of operations required
            maxSum += optEle;
            cost += optCost;
        }
  
        // Print the result
        System.out.println("Difference = " + maxSum + " , "
                           + "Count of operations = "
                           + cost);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Given array arr[]
        int arr[] = { 541, 763, 321, 716, 143 };
  
        // Function call
        getMaxSum(arr);
    }
}


输出:
Difference = 631 , Count of operations = 6

时间复杂度: O(N*√X*log(X)),其中X数组中的最大元素
辅助空间: O(1)

有关更多详细信息,请参阅完整的文章通过将数字左移最小次数来最大化素数和非素数数组元素之和之间的差异!