Given an array **arr[]** of size **N** and a number **X**. If any sub array of the array(possibly empty) arr[i], arr[i+1], … can be replaced with arr[i]/x, arr[i+1]/x, …. The task is to find the minimum possible sum of the array which can be obtained.

**Note:** The given operation can only be performed once.

**Examples:**

Input:N = 3, X = 2, arr[] = {1, -2, 3}

Output:0.5

Explanation:

On selecting subarray {3} and replacing it with {1.5}, the array becomes {1, -2, 1.5}, which gives sum = 0.5

Input:N = 5, X = 5, arr[] = {5, 5, 5, 5, 5}

Output:5

Explanation:

On selecting subarray {5, 5, 5, 5, 5} and replacing it with {1, 1, 1, 1, 1}, the sum of the array becomes 5.

**Naive Approach:** A naive approach is to replace all possible positive sub-arrays with the values which we get by dividing it by **X** and compute the sum. But the total number of sub-arrays for any given array is **(N * (N + 1))/2** where N is the size of the array. Therefore, the running time of this algorithm is **O(N ^{2})**.

**Efficient Approach:** This problem can be solved efficiently in O(N) time. On observing carefully, it can be deduced that sum can be made minimum if the subarray to be replaced satisfies the following conditions:

- The subarray should be positive.
- The sum of the subarray should be maximum.

Therefore, by reducing the subarray satisfying the above conditions, the sum can be made minimum. The subarray which satisfies the above conditions can be found by using a slight variation of Kadane’s algorithm.

After finding the maximum sum of the subarray, this can be subtracted from the sum of the array to get the minimum sum. Since the maximum sum of the sub-array is being replaced with the maximum sum / X, this factor can be added to the sum which is found. That is:

Minimum sum = (sumOfArr - subSum) + (subSum/ X) where sumOfArr is the sum of all elements of the array and subSum is the maximum sum of the subarray.

Below is the implementation of the above approach:

## CPP

`// C++ program to find the minimum possible sum ` `// of the array elements after performing ` `// the given operation ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum sum ` `// of the sub array ` `double` `maxSubArraySum(` `double` `a[], ` `int` `size) ` `{ ` ` ` ` ` `// max_so_far represents the maximum sum ` ` ` `// found till now and max_ending_here ` ` ` `// represents the maximum sum ending at ` ` ` `// a specific index ` ` ` `double` `max_so_far = INT_MIN, ` ` ` `max_ending_here = 0; ` ` ` ` ` `// Iterating through the array to find ` ` ` `// the maximum sum of the subarray ` ` ` `for` `(` `int` `i = 0; i < size; i++) { ` ` ` `max_ending_here = max_ending_here + a[i]; ` ` ` `if` `(max_so_far < max_ending_here) ` ` ` `max_so_far = max_ending_here; ` ` ` ` ` `// If the maximum sum ending at a ` ` ` `// specific index becomes less than 0, ` ` ` `// then making it equal to 0. ` ` ` `if` `(max_ending_here < 0) ` ` ` `max_ending_here = 0; ` ` ` `} ` ` ` `return` `max_so_far; ` `} ` ` ` `// Function to find the minimum possible sum ` `// of the array elements after performing ` `// the given operation ` `double` `minPossibleSum(` `double` `a[], ` `int` `n, ` `double` `x) ` `{ ` ` ` `double` `mxSum = maxSubArraySum(a, n); ` ` ` `double` `sum = 0; ` ` ` ` ` `// Finding the sum of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Computing the minimum sum of the array ` ` ` `sum = sum - mxSum + mxSum / x; ` ` ` ` ` `cout << setprecision(2) << sum << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 3; ` ` ` `double` `X = 2; ` ` ` `double` `A[N] = { 1, -2, 3 }; ` ` ` `minPossibleSum(A, N, X); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the minimum possible sum ` `// of the array elements after performing ` `// the given operation ` `class` `GFG{ ` ` ` `// Function to find the maximum sum ` `// of the sub array ` `static` `double` `maxSubArraySum(` `double` `a[], ` `int` `size) ` `{ ` ` ` ` ` `// max_so_far represents the maximum sum ` ` ` `// found till now and max_ending_here ` ` ` `// represents the maximum sum ending at ` ` ` `// a specific index ` ` ` `double` `max_so_far = Integer.MIN_VALUE, ` ` ` `max_ending_here = ` `0` `; ` ` ` ` ` `// Iterating through the array to find ` ` ` `// the maximum sum of the subarray ` ` ` `for` `(` `int` `i = ` `0` `; i < size; i++) { ` ` ` `max_ending_here = max_ending_here + a[i]; ` ` ` `if` `(max_so_far < max_ending_here) ` ` ` `max_so_far = max_ending_here; ` ` ` ` ` `// If the maximum sum ending at a ` ` ` `// specific index becomes less than 0, ` ` ` `// then making it equal to 0. ` ` ` `if` `(max_ending_here < ` `0` `) ` ` ` `max_ending_here = ` `0` `; ` ` ` `} ` ` ` `return` `max_so_far; ` `} ` ` ` `// Function to find the minimum possible sum ` `// of the array elements after performing ` `// the given operation ` `static` `void` `minPossibleSum(` `double` `a[], ` `int` `n, ` `double` `x) ` `{ ` ` ` `double` `mxSum = maxSubArraySum(a, n); ` ` ` `double` `sum = ` `0` `; ` ` ` ` ` `// Finding the sum of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Computing the minimum sum of the array ` ` ` `sum = sum - mxSum + mxSum / x; ` ` ` ` ` `System.out.print(sum +` `"\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` `double` `X = ` `2` `; ` ` ` `double` `A[] = { ` `1` `, -` `2` `, ` `3` `}; ` ` ` `minPossibleSum(A, N, X); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the minimum possible sum ` `# of the array elements after performing ` `# the given operation ` ` ` `# Function to find the maximum sum ` `# of the sub array ` `def` `maxSubArraySum(a, size): ` ` ` ` ` `# max_so_far represents the maximum sum ` ` ` `# found till now and max_ending_here ` ` ` `# represents the maximum sum ending at ` ` ` `# a specific index ` ` ` `max_so_far ` `=` `-` `10` `*` `*` `9` ` ` `max_ending_here ` `=` `0` ` ` ` ` `# Iterating through the array to find ` ` ` `# the maximum sum of the subarray ` ` ` `for` `i ` `in` `range` `(size): ` ` ` `max_ending_here ` `=` `max_ending_here ` `+` `a[i] ` ` ` `if` `(max_so_far < max_ending_here): ` ` ` `max_so_far ` `=` `max_ending_here ` ` ` ` ` `# If the maximum sum ending at a ` ` ` `# specific index becomes less than 0, ` ` ` `# then making it equal to 0. ` ` ` `if` `(max_ending_here < ` `0` `): ` ` ` `max_ending_here ` `=` `0` ` ` `return` `max_so_far ` ` ` `# Function to find the minimum possible sum ` `# of the array elements after performing ` `# the given operation ` `def` `minPossibleSum(a,n, x): ` ` ` `mxSum ` `=` `maxSubArraySum(a, n) ` ` ` `sum` `=` `0` ` ` ` ` `# Finding the sum of the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `sum` `+` `=` `a[i] ` ` ` ` ` `# Computing the minimum sum of the array ` ` ` `sum` `=` `sum` `-` `mxSum ` `+` `mxSum ` `/` `x ` ` ` `print` `(` `round` `(` `sum` `,` `2` `)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `3` ` ` `X ` `=` `2` ` ` `A` `=` `[` `1` `, ` `-` `2` `, ` `3` `] ` ` ` `minPossibleSum(A, N, X) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the minimum possible sum ` `// of the array elements after performing ` `// the given operation ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum sum ` `// of the sub array ` `static` `double` `maxSubArraySum(` `double` `[] a, ` `int` `size) ` `{ ` ` ` ` ` `// max_so_far represents the maximum sum ` ` ` `// found till now and max_ending_here ` ` ` `// represents the maximum sum ending at ` ` ` `// a specific index ` ` ` `double` `max_so_far = Int32.MinValue, ` ` ` `max_ending_here = 0; ` ` ` ` ` `// Iterating through the array to find ` ` ` `// the maximum sum of the subarray ` ` ` `for` `(` `int` `i = 0; i < size; i++) { ` ` ` `max_ending_here = max_ending_here + a[i]; ` ` ` `if` `(max_so_far < max_ending_here) ` ` ` `max_so_far = max_ending_here; ` ` ` ` ` `// If the maximum sum ending at a ` ` ` `// specific index becomes less than 0, ` ` ` `// then making it equal to 0. ` ` ` `if` `(max_ending_here < 0) ` ` ` `max_ending_here = 0; ` ` ` `} ` ` ` `return` `max_so_far; ` `} ` ` ` `// Function to find the minimum possible sum ` `// of the array elements after performing ` `// the given operation ` `static` `void` `minPossibleSum(` `double` `[] a, ` `int` `n, ` `double` `x) ` `{ ` ` ` `double` `mxSum = maxSubArraySum(a, n); ` ` ` `double` `sum = 0; ` ` ` ` ` `// Finding the sum of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Computing the minimum sum of the array ` ` ` `sum = sum - mxSum + mxSum / x; ` ` ` ` ` `Console.Write(sum +` `"\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 3; ` ` ` `double` `X = 2; ` ` ` `double` `[] A = { 1, -2, 3 }; ` ` ` `minPossibleSum(A, N, X); ` `} ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

0.5

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum possible sum of array elements after performing the given operation
- Sum of the updated array after performing the given operation
- Sub-string that contains all lowercase alphabets after performing the given operation
- Find the index which is the last to be reduced to zero after performing a given operation
- Maximum sum of all elements of array after performing given operations
- Number of elements from the array which are reachable after performing given operations on D
- Maximize count of distinct elements possible in an Array from the given operation
- Number of array elements derivable from D after performing certain operations
- Maximum count of equal numbers in an array after performing given operations
- Find the modified array after performing k operations of given type
- Find the final sequence of the array after performing given operations
- Maximum number of unique values in the array after performing given operations
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Minimum possible number with the given operation
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Maximum inversions in a sequence of 1 to N after performing given operations at most K times
- Check if original Array is retained after performing XOR with M exactly K times
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element

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.