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++
#include <bits/stdc++.h>
using namespace std;
void findArrayWithMaxProduct( int arr[],
int N)
{
for ( int i = 0; i < N; i++)
if (arr[i] >= 0) {
arr[i] = -arr[i] - 1;
}
if (N % 2 == 1) {
int max_element = -1;
int index = -1;
for ( int i = 0; i < N; i++)
if ( abs (arr[i]) > max_element) {
max_element = abs (arr[i]);
index = i;
}
arr[index] = -arr[index] - 1;
}
for ( int i = 0; i < N; i++)
printf ( "%d " , arr[i]);
}
int main()
{
int arr[] = { -3, 0, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
findArrayWithMaxProduct(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findArrayWithMaxProduct( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
if (arr[i] >= 0 )
{
arr[i] = -arr[i] - 1 ;
}
if (N % 2 == 1 )
{
int max_element = - 1 ;
int index = - 1 ;
for ( int i = 0 ; i < N; i++)
if (Math.abs(arr[i]) > max_element) {
max_element = Math.abs(arr[i]);
index = i;
}
arr[index] = -arr[index] - 1 ;
}
for ( int i = 0 ; i < N; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { - 3 , 0 , 1 };
int N = arr.length;
findArrayWithMaxProduct(arr, N);
}
}
|
Python3
def findArrayWithMaxProduct(arr, N):
for i in range (N):
if (arr[i] > = 0 ):
arr[i] = - arr[i] - 1
if (N % 2 = = 1 ):
max_element = - 1
index = - 1
for i in range (N):
if ( abs (arr[i]) > max_element):
max_element = abs (arr[i])
index = i
arr[index] = - arr[index] - 1
for i in arr:
print (i, end = " " )
if __name__ = = '__main__' :
arr = [ - 3 , 0 , 1 ]
N = len (arr)
findArrayWithMaxProduct(arr, N)
|
C#
using System;
class GFG
{
static void findArrayWithMaxProduct( int [] arr, int N)
{
for ( int i = 0; i < N; i++)
if (arr[i] >= 0)
{
arr[i] = -arr[i] - 1;
}
if (N % 2 == 1)
{
int max_element = -1;
int index = -1;
for ( int i = 0; i < N; i++)
if (Math.Abs(arr[i]) > max_element) {
max_element = Math.Abs(arr[i]);
index = i;
}
arr[index] = -arr[index] - 1;
}
for ( int i = 0; i < N; i++)
Console.Write(arr[i] + " " );
}
static public void Main()
{
int [] arr = { -3, 0, 1 };
int N = arr.Length;
findArrayWithMaxProduct(arr, N);
}
}
|
Javascript
<script>
function findArrayWithMaxProduct(arr, N)
{
for ( var i = 0; i < N; i++)
if (arr[i] >= 0) {
arr[i] = -arr[i] - 1;
}
if (N % 2 === 1)
{
var max_element = -1;
var index = -1;
for ( var i = 0; i < N; i++)
if (Math.abs(arr[i]) > max_element)
{
max_element = Math.abs(arr[i]);
index = i;
}
arr[index] = -arr[index] - 1;
}
for ( var i = 0; i < N; i++) document.write(arr[i] + " " );
}
var arr = [-3, 0, 1];
var N = arr.length;
findArrayWithMaxProduct(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...