There is an array consisting of (2 * n – 1) integers. We can change sign of exactly n elements in the array. In other words, we can select exactly n array elements, and multiply each of them by -1. Find the maximum sum of the array.
Input : arr = 50 50 50 Output : 150 There is no need to change anything. The sum of elements equals 150 which is maximum. Input : arr = -1 -100 -1 Output : 100 Change the sign of the first two elements. Sum of the elements equal to 100.
Step 1:- Iterate the loop for 2*n-1 times and repeat the steps 2, 3 and 4.
Step 2:- Calculate no. of negative numbers (neg).
Step 3:- Calculate the sum (sum) of the array by taking absolute values of the numbers.
Step 4:- Find the minimum number of the array by taking absolute values of the numbers (min).
Step 5:- Check if the no. of negative numbers is odd and the value of n (given) is even then subtract two times m from the sum and this will be max_sum of the array else, the value of sum will be the max_sum of the array.
Below is the implementation of the above approach:
- Minimizing array sum by applying XOR operation on all elements of the array
- Maximizing the elements with a[i+1] > a[i]
- Maximum possible array sum after performing the given operation
- Sum of the updated array after performing the given operation
- Make the array non-decreasing with the given operation
- Reduce the array to a single integer with the given operation
- Minimum possible sum of array elements after performing the given operation
- Make all elements of an array equal with the given operation
- Constant time range add operation on an array
- Maximizing Unique Pairs from two arrays
- Minimum cost to equal all elements of array using two operation
- Minimum operation to make all elements equal in array
- Check if the last element of array is even or odd after performing a operation p times
- Find the number of different numbers in the array after applying the given operation q times
- Minimum number of subtract operation to make an array decreasing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : vt_m