Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times.
Examples:
Input: arr[] = {541, 763, 321, 716, 143}
Output: Difference = 631, Count of operations = 6
Explanation:
Operation 1: Left shift the digits of arr[1] (= 763). Therefore, arr[1] becomes 637.
Operation 2: Left shift the digits of arr[1] (= 637). Therefore, arr[1] becomes 376.
Operation 3: Left shift the digits of arr[2] (= 321). Therefore, arr[2] becomes 213.
Operation 4: Left shift the digits of arr[2] (= 213). Therefore, arr[2] becomes 132.
Operation 5: Left shift the digits of arr[3] (= 716). Therefore, arr[3] becomes 167.
Operation 6: Left shift the digits of arr[4] (= 143). Therefore, arr[4] becomes 431.
Therefore, Sum of prime array elements = 541 + 167 + 431 = 1139.
Therefore, sum of non-prime array elements = 376 + 132 = 508.
Therefore, difference = 1139 – 508 = 631.Input: {396, 361, 359, 496, 780}
Output: Difference = 236, Count of operations = 4
Explanation:
Operation 1: Left shift the digits of arr[1] (= 361). Therefore, arr[1] becomes 613.
Operation 2: Left shift the digits of arr[2] (= 359). Therefore, arr[2] becomes 593.
Operation 3: Left shift the digits of arr[4] (= 780). Therefore, arr[4] becomes 807.
Operation 4: Left shift the digits of arr[4] (= 807). Therefore, arr[4] becomes 078.
Therefore, required difference = 613 + 593 – 496 – 78 – 396 = 236.
Approach: The given problem can be solved greedily. If it is possible to convert an element into one or more than one prime number, then take the maximum of them. Otherwise, try to minimize the element by using all the possible rotations.
Follow the steps below to solve the problem:
- Initialize two variables, say ans and cost, to store the maximum difference and the minimum number of operations required respectively.
- Traverse the array arr[] using a variable i and perform the following steps:
- Initialize variables maxPrime and minRotation as -1 to store the maximum prime number and minimum number that can be obtained from arr[i] through left rotations.
- Generate all left rotations of the number arr[i].
- If arr[i] is a prime number exceeding maxPrime, then update maxPrime to arr[i] and the cost accordingly.
 
- If the value of maxPrime remains unchanged, find the value of minRotation by similarly generating all the left rotations.
- Add the value of arr[i] to ans.
 
- After completing the above steps, print the value of ans and cost as the result.
Below is the implementation of the above approach:
Java
| // java program for the above approachimportjava.io.*;importjava.lang.*;importjava.util.*; classGFG {     // Function to check if a    // number is prime or not    staticbooleanisPrime(intn)    {         // Base cases        if(n <= 1)            returnfalse;        if(n <= 3)            returntrue;         // Check if the number is        // a multiple of 2 or 3        if(n % 2== 0|| n % 3== 0)            returnfalse;         inti = 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)                returnfalse;            i = i + 6;        }        returntrue;    }     // Function to left shift a number    // to maximize its contribution    staticint[] rotateElement(intn)    {         // Convert the number to string        String strN = Integer.toString(n);         // Stores the maximum prime number        // that can be obtained from n        intmaxPrime = -1;         // Store the required        // number of operations        intcost = 0;         String temp = strN;         // Check for all the left        // rotations of the number        for(inti = 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);        }         intoptEle = 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(inti = 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);        }        returnnewint[] { optEle, cost };    }     // Function to find the maximum sum    // obtained using the given operations    staticvoidgetMaxSum(intarr[])    {         // Store the maximum sum and        // the number of operations        intmaxSum = 0, cost = 0;         // Traverse array elements        for(intx : arr) {             // Get the optimal element and the            // number of operations to obtain it            intret[] = rotateElement(x);             intoptEle = 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    publicstaticvoidmain(String[] args)    {         // Given array arr[]        intarr[] = { 541, 763, 321, 716, 143};         // Function call        getMaxSum(arr);    }} | 
Difference = 631 , Count of operations = 6
Time Complexity: O(N*√X*log(X)), where X is the largest element in the array
Auxiliary Space: O(1)
Please refer complete article on Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times for more details!


 
                                    







