# Minimum possible sum of array elements after performing the given operation

Given an array arr[] of positive integers and an integer x, the task is to minimize the sum of elements of the array after performing the given operation at most once. In a single operation, any element from the array can be divided by x (if it is divisible by x) and at the same time, any other element from the array must be multiplied by x.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}, x = 2
Output: 14
Multiply 1 by x i.e. 1 * 2 = 2
Divide 4 by x i.e. 4 / 2 = 2
And the updated sum will be 2 + 2 + 3 + 2 + 5 = 14

Input: arr[] = {5, 5, 5, 5, 6}, x = 3
Output: 26

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: For an optimal solution, x must be multiplied with the smallest element from the array and only the largest element divisible by x must be divided by it. Let sumAfterOperation be the sum of the array elements calculated after performing the operation and sum be the sum of all the elements of the original array then the minimized sum will be min(sum, sumAfterOperation).

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define ll long long int ` ` `  `// Function to return the minimized sum ` `ll minSum(``int` `arr[], ``int` `n, ``int` `x) ` `{ ` `    ``ll sum = 0; ` ` `  `    ``// To store the largest element ` `    ``// from the array which is ` `    ``// divisible by x ` `    ``int` `largestDivisible = -1, minimum = arr; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Sum of array elements before ` `        ``// performing any operation ` `        ``sum += arr[i]; ` ` `  `        ``// If current element is divisible by x ` `        ``// and it is maximum so far ` `        ``if` `(arr[i] % x == 0 && largestDivisible < arr[i]) ` `            ``largestDivisible = arr[i]; ` ` `  `        ``// Update the minimum element ` `        ``if` `(arr[i] < minimum) ` `            ``minimum = arr[i]; ` `    ``} ` ` `  `    ``// If no element can be reduced then there's no point ` `    ``// in performing the operation as we will end up ` `    ``// increasing the sum when an element is multiplied by x ` `    ``if` `(largestDivisible == -1) ` `        ``return` `sum; ` ` `  `    ``// Subtract the chosen elements from the sum ` `    ``// and then add their updated values ` `    ``ll sumAfterOperation = sum - minimum - largestDivisible ` `                           ``+ (x * minimum) + (largestDivisible / x); ` ` `  `    ``// Return the minimized sum ` `    ``return` `min(sum, sumAfterOperation); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 5, 5, 5, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `x = 3; ` `    ``cout << minSum(arr, n, x); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` `     `  `// Function to return the minimized sum ` `static` `int` `minSum(``int` `arr[], ``int` `n, ``int` `x) ` `{ ` `    ``int` `sum = ``0``; ` ` `  `    ``// To store the largest element ` `    ``// from the array which is ` `    ``// divisible by x ` `    ``int` `largestDivisible = -``1``,  ` `        ``minimum = arr[``0``]; ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` ` `  `        ``// Sum of array elements before ` `        ``// performing any operation ` `        ``sum += arr[i]; ` ` `  `        ``// If current element is divisible  ` `        ``// by x and it is maximum so far ` `        ``if` `(arr[i] % x == ``0` `&&  ` `            ``largestDivisible < arr[i]) ` `            ``largestDivisible = arr[i]; ` ` `  `        ``// Update the minimum element ` `        ``if` `(arr[i] < minimum) ` `            ``minimum = arr[i]; ` `    ``} ` ` `  `    ``// If no element can be reduced then  ` `    ``// there's no point in performing the  ` `    ``// operation as we will end up increasing ` `    ``// the sum when an element is multiplied by x ` `    ``if` `(largestDivisible == -``1``) ` `        ``return` `sum; ` ` `  `    ``// Subtract the chosen elements from the  ` `    ``// sum and then add their updated values ` `    ``int` `sumAfterOperation = sum - minimum - largestDivisible +  ` `                            ``(x * minimum) + (largestDivisible / x); ` ` `  `    ``// Return the minimized sum ` `    ``return` `Math.min(sum, sumAfterOperation); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``5``, ``5``, ``5``, ``5``, ``6` `}; ` `    ``int` `n =arr.length; ` `    ``int` `x = ``3``; ` `    ``System.out.println(minSum(arr, n, x)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Code_Mech `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the minimized sum  ` `def` `minSum(arr, n, x):  ` ` `  `    ``Sum` `=` `0` ` `  `    ``# To store the largest element  ` `    ``# from the array which is  ` `    ``# divisible by x  ` `    ``largestDivisible, minimum ``=` `-``1``, arr[``0``]  ` `    ``for` `i ``in` `range``(``0``, n):  ` ` `  `        ``# Sum of array elements before  ` `        ``# performing any operation  ` `        ``Sum` `+``=` `arr[i]  ` ` `  `        ``# If current element is divisible by x  ` `        ``# and it is maximum so far  ` `        ``if``(arr[i] ``%` `x ``=``=` `0` `and`  `           ``largestDivisible < arr[i]):  ` `            ``largestDivisible ``=` `arr[i]  ` ` `  `        ``# Update the minimum element  ` `        ``if` `arr[i] < minimum: ` `            ``minimum ``=` `arr[i]  ` ` `  `    ``# If no element can be reduced then there's  ` `    ``# no point in performing the operation as ` `    ``# we will end up increasing the sum when an  ` `    ``# element is multiplied by x  ` `    ``if` `largestDivisible ``=``=` `-``1``:  ` `        ``return` `Sum` ` `  `    ``# Subtract the chosen elements from the ` `    ``# sum and then add their updated values  ` `    ``sumAfterOperation ``=` `(``Sum` `-` `minimum ``-` `largestDivisible ``+`  `                        ``(x ``*` `minimum) ``+` `(largestDivisible ``/``/` `x))  ` ` `  `    ``# Return the minimized sum  ` `    ``return` `min``(``Sum``, sumAfterOperation)  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` ` `  `    ``arr ``=` `[``5``, ``5``, ``5``, ``5``, ``6``]  ` `    ``n ``=` `len``(arr) ` `    ``x ``=` `3` `    ``print``(minSum(arr, n, x))  ` ` `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the minimized sum ` `static` `int` `minSum(``int``[] arr, ``int` `n, ``int` `x) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// To store the largest element ` `    ``// from the array which is ` `    ``// divisible by x ` `    ``int` `largestDivisible = -1,  ` `        ``minimum = arr; ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` ` `  `        ``// Sum of array elements before ` `        ``// performing any operation ` `        ``sum += arr[i]; ` ` `  `        ``// If current element is divisible  ` `        ``// by x and it is maximum so far ` `        ``if` `(arr[i] % x == 0 &&  ` `            ``largestDivisible < arr[i]) ` `            ``largestDivisible = arr[i]; ` ` `  `        ``// Update the minimum element ` `        ``if` `(arr[i] < minimum) ` `            ``minimum = arr[i]; ` `    ``} ` ` `  `    ``// If no element can be reduced then  ` `    ``// there's no point in performing the  ` `    ``// operation as we will end up increasing ` `    ``// the sum when an element is multiplied by x ` `    ``if` `(largestDivisible == -1) ` `        ``return` `sum; ` ` `  `    ``// Subtract the chosen elements from the  ` `    ``// sum and then add their updated values ` `    ``int` `sumAfterOperation = sum - minimum - largestDivisible +  ` `                            ``(x * minimum) + (largestDivisible / x); ` ` `  `    ``// Return the minimized sum ` `    ``return` `Math.Min(sum, sumAfterOperation); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int``[] arr = { 5, 5, 5, 5, 6 }; ` `    ``int` `n = arr.Length; ` `    ``int` `x = 3; ` `    ``Console.WriteLine(minSum(arr, n, x)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Code_Mech `

## PHP

 ` `

Output:

```26
```

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.