Maximize array product by changing any array element arr[i] to (-1)*arr[i] - 1 any number of times
Last Updated :
13 Jan, 2022
Given an array arr[] consisting of N integers, the task is to modify the array elements by changing each array element arr[i] to (-1)*arr[i] - 1 any number of times such that the product of the array is maximized.
Examples:
Input: arr[] = {-3, 0, 1}
Output: 2 -1 -2
Explanation:
Performing the following operations on the array elements maximizes the product:
Applying the operation on arr[0] once, arr[0] = (- 1) * (- 3) - 1 = 2.
Applying the operation on arr[1] once, arr[1] = 0 - 1 = -1.
Applying the operation on arr[2] once, arr[2] = -1 - 1 = -2.
The modified array is {2, -1, -2} with product 4, which is maximum possible.
Input: arr[] = {-9, 2, 0, 1, -5, 3, 16}
Output: -9 -3 -1 -2 -5 -4 16
Approach: The given problem can be solved based on the following observations:
- For the product to be maximum, the absolute value of elements should be higher as the operation decreases the absolute value of negative elements. Therefore, the given operations should only be applied to positive values.
- If the number of elements is odd, then perform an operation on the highest absolute valued element as the product of an odd number of negative numbers is negative. Therefore, change one positive number for the product to be positive and maximum.
- Perform the operation on a negative number decreases its absolute value. Therefore, find the element with the greatest absolute value and apply the operation on it once as that would decrease the product by the least.
Follow the steps below to solve the problem:
- Traverse the given array arr[] and for each element arr[i], check if arr[i] ? 0 or not. If found to be true, then update arr[i] = (-1)*arr[i] - 1. Otherwise, continue.
- After the array traversal, check if the value of N is odd or not. If found to be true, then find the element with the greatest absolute value and apply the operation to it once.
- After completing the above steps, print the modified array arr[].
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find array with maximum
// product by changing array
// elements to (-1)arr[i] - 1
void findArrayWithMaxProduct(int arr[],
int N)
{
// Traverse the array
for (int i = 0; i < N; i++)
// Applying the operation on
// all the positive elements
if (arr[i] >= 0) {
arr[i] = -arr[i] - 1;
}
if (N % 2 == 1) {
// Stores maximum element in array
int max_element = -1;
// Stores index of maximum element
int index = -1;
for (int i = 0; i < N; i++)
// Check if current element
// is greater than the
// maximum element
if (abs(arr[i]) > max_element) {
max_element = abs(arr[i]);
// Find index of the
// maximum element
index = i;
}
// Perform the operation on the
// maximum element
arr[index] = -arr[index] - 1;
}
// Print the elements of the array
for (int i = 0; i < N; i++)
printf("%d ", arr[i]);
}
// Driver Code
int main()
{
int arr[] = { -3, 0, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
findArrayWithMaxProduct(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find array with maximum
// product by changing array
// elements to (-1)arr[i] - 1
static void findArrayWithMaxProduct(int arr[], int N)
{
// Traverse the array
for (int i = 0; i < N; i++)
// Applying the operation on
// all the positive elements
if (arr[i] >= 0)
{
arr[i] = -arr[i] - 1;
}
if (N % 2 == 1)
{
// Stores maximum element in array
int max_element = -1;
// Stores index of maximum element
int index = -1;
for (int i = 0; i < N; i++)
// Check if current element
// is greater than the
// maximum element
if (Math.abs(arr[i]) > max_element) {
max_element = Math.abs(arr[i]);
// Find index of the
// maximum element
index = i;
}
// Perform the operation on the
// maximum element
arr[index] = -arr[index] - 1;
}
// Print the elements of the array
for (int i = 0; i < N; i++)
System.out.print(arr[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { -3, 0, 1 };
int N = arr.length;
// Function Call
findArrayWithMaxProduct(arr, N);
}
}
// This code is contributed by jithin.
Python3
# Python3 program for the above approach
# Function to find array with maximum
# product by changing array
# elements to (-1)arr[i] - 1
def findArrayWithMaxProduct(arr, N):
# Traverse the array
for i in range(N):
# Applying the operation on
# all the positive elements
if (arr[i] >= 0):
arr[i] = -arr[i] - 1
if (N % 2 == 1):
# Stores maximum element in array
max_element = -1
# Stores index of maximum element
index = -1
for i in range(N):
# Check if current element
# is greater than the
# maximum element
if (abs(arr[i]) > max_element):
max_element = abs(arr[i])
# Find index of the
# maximum element
index = i
# Perform the operation on the
# maximum element
arr[index] = -arr[index] - 1
# Print elements of the array
for i in arr:
print(i, end = " ")
# Driver Code
if __name__ == '__main__':
arr = [-3, 0, 1]
N = len(arr)
# Function Call
findArrayWithMaxProduct(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find array with maximum
// product by changing array
// elements to (-1)arr[i] - 1
static void findArrayWithMaxProduct(int[] arr, int N)
{
// Traverse the array
for (int i = 0; i < N; i++)
// Applying the operation on
// all the positive elements
if (arr[i] >= 0)
{
arr[i] = -arr[i] - 1;
}
if (N % 2 == 1)
{
// Stores maximum element in array
int max_element = -1;
// Stores index of maximum element
int index = -1;
for (int i = 0; i < N; i++)
// Check if current element
// is greater than the
// maximum element
if (Math.Abs(arr[i]) > max_element) {
max_element = Math.Abs(arr[i]);
// Find index of the
// maximum element
index = i;
}
// Perform the operation on the
// maximum element
arr[index] = -arr[index] - 1;
}
// Print the elements of the array
for (int i = 0; i < N; i++)
Console.Write(arr[i] + " ");
}
// Driver Code
static public void Main()
{
int[] arr = { -3, 0, 1 };
int N = arr.Length;
// Function Call
findArrayWithMaxProduct(arr, N);
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// JavaScript program for the above approach
// Function to find array with maximum
// product by changing array
// elements to (-1)arr[i] - 1
function findArrayWithMaxProduct(arr, N)
{
// Traverse the array
for (var i = 0; i < N; i++)
// Applying the operation on
// all the positive elements
if (arr[i] >= 0) {
arr[i] = -arr[i] - 1;
}
if (N % 2 === 1)
{
// Stores maximum element in array
var max_element = -1;
// Stores index of maximum element
var index = -1;
for (var i = 0; i < N; i++)
// Check if current element
// is greater than the
// maximum element
if (Math.abs(arr[i]) > max_element)
{
max_element = Math.abs(arr[i]);
// Find index of the
// maximum element
index = i;
}
// Perform the operation on the
// maximum element
arr[index] = -arr[index] - 1;
}
// Print the elements of the array
for (var i = 0; i < N; i++) document.write(arr[i] + " ");
}
// Driver Code
var arr = [-3, 0, 1];
var N = arr.length;
// Function Call
findArrayWithMaxProduct(arr, N);
// This code is contributed by rdtank.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize sum by choosing a subsegment from array [l, r] and convert arr[i] to (Mâarr[i]) at most once Given an array, arr[] consisting of N positive integers and a positive integer M, the task is to maximize the sum of the array after performing at most one operation. In one operation, choose a subsegment from the array [l, r] and convert arr[i] to M - arr[i] where l?i?r. Examples: Input: arr[] = {2
6 min read
Minimize operations to make all array elements -1 by changing maximums of K-size subarray to -1 Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum of operations required to make all the array elements -1 such that in each operation, choose a subarray of size K and change all the maximum element in the subarray to -1. Examples: Input: arr[] = {18, 11
8 min read
Maximize product of two closest numbers of other array for every element in given array Given arrays arr1[] of size M and arr2[] of size N having length at least 2, the task is for every element in arr1[], maximize the product of two elements in arr2[] which are closest to the element in arr1[]. The closest elements must be present on distinct indices. Example: Input: arr1 = [5, 10, 17
15 min read
Maximize Array sum after changing sign of any elements for exactly M times Given an array arr[] of size N and an integer M, the task is to find the maximum sum of the array after changing the sign of any elements in the array for exactly M times. It is allowed to change the sign of the same element multiple times. Examples: Input: arr[ ] = {-3, 7, -1, -5, -3}, M = 4Output:
6 min read
Maximum value of (arr[i] * arr[j]) + (arr[j] - arr[i])) possible for any pair in an array Given an array arr[] consisting of N integers, the task is to find the maximum possible value of the expression (arr[i] * arr[j]) + (arr[j] - arr[i])) for any pair (i, j), such that i ? j and 0 ? (i, j) < N. Examples: Input: arr[] = {-2, -8, 0, 1, 2, 3, 4}Output: 22Explanation:For the pair (-8, -
6 min read